ServerClockOffset

class ServerClockOffset(localTimeMs: () -> Long = { System.currentTimeMillis() }, maxRttMs: Long = DEFAULT_MAX_RTT_MS, maxOffsetMs: Long = DEFAULT_MAX_OFFSET_MS)

Tracks the offset between the local device clock and the server clock using NTP-style estimation from WebSocket health check round-trips.

The algorithm keeps only the sample with the lowest observed RTT, since a smaller round-trip means less room for network asymmetry to distort the measurement. Under the assumption that clock skew is constant for the duration of a session, the estimate monotonically improves over time.

Thread-safe: single-field writes use Volatile for visibility; compound read-modify-write sequences are guarded by lock for atomicity.

Parameters

localTimeMs

Clock source for the local device time (injectable for tests).

maxRttMs

Upper bound on plausible RTT. Samples exceeding this are discarded as stale or mismatched. Defaults to the health check cycle interval (MONITOR_INTERVAL + HEALTH_CHECK_INTERVAL = 11 000 ms).

maxOffsetMs

Upper bound on the absolute value of the computed clock offset. If the derived offset exceeds this threshold the sample is considered unreliable (e.g. a stale / static server timestamp in a test environment) and the offset is reset to zero so that estimatedServerTime falls back to the raw local time. Defaults to 1 hour, which is already far beyond any real-world NTP drift.

Constructors

Link copied to clipboard
constructor(localTimeMs: () -> Long = { System.currentTimeMillis() }, maxRttMs: Long = DEFAULT_MAX_RTT_MS, maxOffsetMs: Long = DEFAULT_MAX_OFFSET_MS)

Functions

Link copied to clipboard

Returns the current time adjusted to the server timescale.