sonyflake icon indicating copy to clipboard operation
sonyflake copied to clipboard

time.Sleep can block VERY long time, when drift the clock backwards

Open errpunk opened this issue 1 year ago • 2 comments

in this scenario, NextID() could block very long time.

  1. in a high load: (sf.sequence + 1) & maskSequence == 0
  2. in the same time, system clock drift backwards, say 8 hours

this will block the function for 8 hour.

can we use some kind of Monotonic time or add a new function with timeout-control to avoid this?

errpunk avatar Mar 13 '23 09:03 errpunk

By the slew mode of NTP service, you can prevent the system clock from stepping backward.

YoshiyukiMineo avatar May 03 '23 16:05 YoshiyukiMineo

Thank you for your response. The NTP slew mode is indeed a good solution, but it cannot be used in the following scenarios:

  • Some edge devices that are offline, frequently unreachable, and unmonitored for long periods of time, such as edge servers and IoT devices.
  • Devices that cannot use the NTP protocol due to security requirements.

My use case happens to be some remote, unattended edge devices that can only be blocked when problems occur.

The Go language's built-in time.Time type uses the mechanism of monotonic time to avoid time drift when calculating time differences. My colleague will provide a PR to demonstrate this solution. I hope you can take some time to review it.

errpunk avatar May 06 '23 07:05 errpunk