Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
runtime: use CLOCK_BOOTTIME for nanotime() #25248
Go's timers appear to use nanotime(). nanotime() at the moment uses CLOCK_MONOTONIC. This is all good and well, until you're trying to do networking on a mobile device. Imagine a protocol that is supposed to do something upon the expiration of a timer with a certain bit of wiggle room. If it doesn't after the wiggle room, availability is affected. If a smartphone goes to sleep, when it wakes back up, if it leaves off counting where it left off, the timer's expiration will happen way too late. Instead, when it wakes back up, if the timer has already expired, it should be immediately expired.
Fortunately, Linux offers CLOCK_BOOTTIME, which according to the man page is identical to CLOCK_MONOTONIC, except it takes into account suspend. (It isn't susceptible to the administrator fiddling the clock like CLOCK_REALTIME is, which obviously would not be what we want.) Check out the clock_gettime man page for descriptions.
The patch for this is straightforward, but I figure I should post about it here first to see if you've already thought about this.