The Clock Went Backwards
This generator has run in production for a year. An NTP step correction moves the host clock 400 ms into the past.
func (g *Gen) Next() int64 {
g.mu.Lock()
defer g.mu.Unlock()
now := time.Now().UnixMilli()
if now == g.lastMs {
g.seq = (g.seq + 1) & 0xFFF
if g.seq == 0 {
for now <= g.lastMs {
now = time.Now().UnixMilli()
}
}
} else {
g.seq = 0
}
g.lastMs = now
return ((now - epoch) << 22) | (g.machineID << 12) | g.seq
}
What breaks, and what is the fix?
Sign in to answer questions and track your progress
Sign In