diff --git a/model/t_uid/t_uid.go b/model/t_uid/t_uid.go index e9d0bc6..146790c 100644 --- a/model/t_uid/t_uid.go +++ b/model/t_uid/t_uid.go @@ -18,6 +18,7 @@ type t_uid struct { F_range_length int `gorm:"column:F_range_length"` // ID范围的长度 F_range_owner string `gorm:"column:F_range_owner"` // ID范围的拥有者,一般是机器id F_create_time time.Time `gorm:"column:F_create_time"` // 记录创建时间 + F_modify_time time.Time `gorm:"column:F_modify_time"` // 记录更新时间 } type typeInfo struct { @@ -46,11 +47,15 @@ func (id *t_uid) TableName() string { } func (id *t_uid) BeforeCreate(db *gorm.DB) error { - id.F_create_time = time.Now() + if id.F_create_time.IsZero() { + id.F_create_time = time.Now() + id.F_modify_time = time.Now() + } return nil } func (id *t_uid) BeforeUpdate(db *gorm.DB) (err error) { + id.F_modify_time = time.Now() return nil } diff --git a/uid.go b/uid.go index cb1c305..274d3a4 100644 --- a/uid.go +++ b/uid.go @@ -206,7 +206,21 @@ func (idg *idCreator) createNewRange(prefix string) error { func (idg *idCreator) getCurrentRange(prefix string) (r *Range) { // Step 0. get ranges queue by prefix. - var queue, ok = idg.Ranges[prefix] + var ( + trycount = 0 + queue *rangeQueue = nil + ok = false + ) + for trycount = 0; !ok && trycount < 3; trycount++ { + if queue, ok = idg.Ranges[prefix]; !ok { + idg.createNewRange(prefix) + } + } + if !ok { + panic("maybe something bug, cannot found range queue after createNewRange") + } + + queue, ok = idg.Ranges[prefix] if !ok { idg.createNewRange(prefix) }