Sleepless Nights

Some people can’t sleep because they’re excited or anxious about the next day. Other people can’t sleep because they’re sad or glad or guilty or proud, or because the birds outside are addicted to methamphetamines and never quit chirping. But here’s why I can’t sleep tonight:

What do you think would be involved in getting NTP (or a reasonable facsimile thereof) written in JavaScript and using HTTP? And how accurate do you think it would be? Not a lame thing where you take the time from some random webpage and claim that as current time, but an implementation of Marzullo’s algorithm in JavaScript for sourcing time from multiple servers, a means for calculating round-trip latency, a means for tracking clock drift between requests, and something like a little Sinatra app to just serve and set no-cache headers? (Actually, time.to_f isn’t terribly precise, though I doubt you’ll be troubled by this in the real world.) How accurate could you get it? I doubt you could rival NTP, but could you get to the millisecond level? Within 10 milliseconds? Surely within 100 milliseconds?

If you’re willing to sacrifice sub-second accuracy in favor of not needing a list of special “time webservers”, HTTP headers indicate the time, and any webserver worth its salt should be synced to NTP. Though it seems many aren’t, so I wouldn’t count on this for anything remotely important. Plus it requires you to be able to parse HTTP headers that many frameworks might not expose.

Leave a Reply

Your email address will not be published. Required fields are marked *