dominictarr / monotonic-timestamp

MIT License
44 stars 8 forks source link

Time flowing backwards #6

Open ricardobeat opened 11 years ago

ricardobeat commented 11 years ago

Clocks might go backwards. By simply storing the latest timestamp we can detect that and do something.

Twitter's snowflake returns an error - not good in JS since it's very likely that whatever client will shove the error.toString() as a key anyway.

In unik I decided to hold back the generation by 1ms, thrashing CPU in the process.

Is there any other solution besides making the API async?

dominictarr commented 11 years ago

So, what about just remember the last valid time, and increment it if it Date.now() is the same?

It doesn't matter if the time is correct, just that it always flows forward.

That won't work across restarts, but can also a thing where network connections do a handshake where they both pick a sensible time.