-
Notifications
You must be signed in to change notification settings - Fork 380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Monotonic clock #18
Conversation
* Fix for default Epoch to use monotonic clock. * Using common case optimization in the time pkg.
I love this :) I'm wondering if you would mind helping me and changing it slightly so to avoid breaking the API? I'm thinking that maybe we can leave Epoch as an int64. Create an epoch variable in the node struct that is the right type, then in the NewNode func take the int64 epoch value and apply it to the node.epoch value. Then on all Generate() code you can reference the node.epoch value. This also lets us not add an additional global value curTime. Then the Time func will take a bit of changing but it's a deprecated func that I intend to replace as a member func off of the node struct. Does that make sense? |
Yes, it makes sense. I also like this better as it hides this ugly hack: |
Great! |
Just to add this in here, as an interesting note. I ran the benchmarks before and after this change and there's some interesting changes. Before:
After:
I'm amazed that BenchmarkGenerateMaxSequence dropped so much while BenchmarkGenerate went up. I'll dig into this more later - or you're welcome to explore it as well. Once thing here is now BenchmarkGenerate takes over 243ns/op which means it cannot (on my computer) create the 4096 ID's per ms that the standard snowflake format supports. |
Yes, I think the comparison logic in the if statement with my change was less efficient. I've update it to how it was before and here are the results, much better now :) Before (v0.1.0):
After (fix in #19) :
It is faster in my implementation in
|
Please check pull request #19 |
that's awesome. Thanks! |
Starting from Go 1.9, the standard time package transparently uses Monotonic Clocks when available. Let's use that for generating ids to safeguard against wall clock backwards movement which could be caused by time drifts or leap seconds.
This PR addresses the aforementioned issue which may lead to collisions.