This commit is contained in:
bryanqiu 2022-12-02 17:55:47 +08:00
parent 79e49306ef
commit c1b56c8472
2 changed files with 21 additions and 2 deletions

View File

@ -18,6 +18,7 @@ type t_uid struct {
F_range_length int `gorm:"column:F_range_length"` // ID范围的长度 F_range_length int `gorm:"column:F_range_length"` // ID范围的长度
F_range_owner string `gorm:"column:F_range_owner"` // ID范围的拥有者一般是机器id F_range_owner string `gorm:"column:F_range_owner"` // ID范围的拥有者一般是机器id
F_create_time time.Time `gorm:"column:F_create_time"` // 记录创建时间 F_create_time time.Time `gorm:"column:F_create_time"` // 记录创建时间
F_modify_time time.Time `gorm:"column:F_modify_time"` // 记录更新时间
} }
type typeInfo struct { type typeInfo struct {
@ -46,11 +47,15 @@ func (id *t_uid) TableName() string {
} }
func (id *t_uid) BeforeCreate(db *gorm.DB) error { func (id *t_uid) BeforeCreate(db *gorm.DB) error {
if id.F_create_time.IsZero() {
id.F_create_time = time.Now() id.F_create_time = time.Now()
id.F_modify_time = time.Now()
}
return nil return nil
} }
func (id *t_uid) BeforeUpdate(db *gorm.DB) (err error) { func (id *t_uid) BeforeUpdate(db *gorm.DB) (err error) {
id.F_modify_time = time.Now()
return nil return nil
} }

16
uid.go
View File

@ -206,7 +206,21 @@ func (idg *idCreator) createNewRange(prefix string) error {
func (idg *idCreator) getCurrentRange(prefix string) (r *Range) { func (idg *idCreator) getCurrentRange(prefix string) (r *Range) {
// Step 0. get ranges queue by prefix. // 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 { if !ok {
idg.createNewRange(prefix) idg.createNewRange(prefix)
} }