Understand the timesync protocol and algorithm
masterProtocol
timesync uses the JSON-RPC 2.0 protocol.
- Request:
{"jsonrpc": "2.0", "id": "12345", "method": "timesync"} - Response:
{"jsonrpc": "2.0", "id": "12345", "result": 1423151204595}
Algorithm
The synchronization algorithm follows these steps:
- Client sends a timestamped request.
- Server responds with its current time.
- Client calculates latency (half of the round-trip time) and the clock delta.
- The first result is used immediately to get into the correct 'ballpark'.
- The client repeats this multiple times (default 5) to collect samples.
- Samples are sorted by latency; outliers (above ~1 standard deviation from the median) are discarded.
- The final offset is the arithmetic mean of the remaining samples.