sonyflake
sonyflake copied to clipboard
time.Sleep can block VERY long time, when drift the clock backwards
in this scenario, NextID() could block very long time.
- in a high load:
(sf.sequence + 1) & maskSequence == 0
- 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?
By the slew mode of NTP service, you can prevent the system clock from stepping backward.
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.