pub struct HlcClock<C: PhysicalClock = SystemClock> { /* private fields */ }Expand description
Hybrid Logical Clock state machine.
Two core operations:
now() always returns a strictly increasing timestamp.
update() advances the internal clock state without generating a new timestamp.
Timestamps are only generated by local events; received timestamps only advance
the clock.
Implementations§
Source§impl HlcClock<SystemClock>
impl HlcClock<SystemClock>
Source§impl<C: PhysicalClock> HlcClock<C>
impl<C: PhysicalClock> HlcClock<C>
Sourcepub fn with_clock(clock: C) -> Self
pub fn with_clock(clock: C) -> Self
Create an HLC clock with a custom physical clock source.
Sourcepub fn set_max_drift_ns(&mut self, max_drift_ns: i64)
pub fn set_max_drift_ns(&mut self, max_drift_ns: i64)
Set the maximum allowed drift in nanoseconds.
Sourcepub fn set_last(&mut self, ts: HlcTimestamp)
pub fn set_last(&mut self, ts: HlcTimestamp)
Seed the clock with a previously persisted timestamp.
Call on startup to restore monotonicity after a restart.
The next now() will return a timestamp strictly greater than ts.
Sourcepub fn now(&mut self) -> Result<HlcTimestamp, ClockError>
pub fn now(&mut self) -> Result<HlcTimestamp, ClockError>
Generate a timestamp for a local event.
Guarantees:
- Monotonically increasing (never returns a value <= previous)
- Wall time tracks physical clock within
max_drift - Returns
ClockError::CounterOverflowif counter saturates - Returns
ClockError::ClockDriftExceededif wall time is too far ahead of physical time
Sourcepub fn update(&mut self, remote: HlcTimestamp) -> Result<(), ClockError>
pub fn update(&mut self, remote: HlcTimestamp) -> Result<(), ClockError>
Merge a remote timestamp into local clock state.
Advances the internal state so that subsequent now() calls
will produce timestamps strictly greater than the remote.
Does NOT generate a new timestamp.
Returns ClockError::ClockDriftExceeded if the remote wall time
is more than max_drift ahead of the local physical clock.
Sourcepub fn last_timestamp(&self) -> HlcTimestamp
pub fn last_timestamp(&self) -> HlcTimestamp
Returns the last generated or merged timestamp.
Sourcepub fn physical_clock(&self) -> &C
pub fn physical_clock(&self) -> &C
Returns a reference to the underlying physical clock.