bwmarrin / snowflake

A simple to use Go (golang) package to generate or parse Twitter snowflake IDs
BSD 2-Clause "Simplified" License
2.98k stars 371 forks source link

When time goes back the `Generate` will get repeated ID. #20

Closed bysir-zl closed 5 years ago

bysir-zl commented 5 years ago

In line 141: if now == n.time {, I think it has a little problem:

If the time on the machine falls back(now<n.time), duplicate IDS will be generated.

The right way should be like sonyflke (https://github.com/sony/sonyflake/blob/master/sonyflake.go#L96)


This project has the most stars on github. I hope it's perfect. Haha.

bwmarrin commented 5 years ago

Maybe take a look at the details in this PR :)

https://github.com/bwmarrin/snowflake/pull/18

In Go, 1.9 and later, time package uses a monotonic clock for time-measuring operations.