Skip to content

Commit

Permalink
fix long run time when the system time was manually set to go back
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-tennis committed Dec 6, 2023
1 parent ca40192 commit 8d19e77
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion snowflake.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,17 @@ func (sf *SnowFlake) NextId() uint64 {
// new timeCycle'time must greater than old
if now < curTime {
println(fmt.Sprintf("now(%v) < curTime(%v))", now, curTime))
continue
// 有可能手动设置了系统时间,导致时间后退了
// It is possible that the system time was manually set, causing the time to go back
if curTime-now <= int64(time.Second/time.Millisecond) {
// 如果时间回退在1秒钟之内,则等待时间追上
// If the time goes back less than 1 second, sleep for awhile
time.Sleep(time.Duration(curTime-now))
continue
}
// 时间回退超过1秒钟,则继续运行,防止"死循环",但是后面生成的id不排除有重复值
// If the time goes back more than 1 second, continue running to prevent a "dead loop",
// but the id generated later may not unique
}
newTimeCycle := &timeCycle{
sequence: 0,
Expand Down

0 comments on commit 8d19e77

Please sign in to comment.