Skip to main content

HlcClock

Struct HlcClock 

Source
pub struct HlcClock<C: PhysicalClock = SystemClock> { /* private fields */ }
Expand description

Hybrid Logical Clock state machine.

Two core operations:

  • now() — generate a timestamp for a local event
  • update() — merge a remote timestamp into local state

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>

Source

pub fn new() -> Self

Create an HLC clock with the system clock and default max drift (5s).

Source§

impl<C: PhysicalClock> HlcClock<C>

Source

pub fn with_clock(clock: C) -> Self

Create an HLC clock with a custom physical clock source.

Source

pub fn set_max_drift_ns(&mut self, max_drift_ns: i64)

Set the maximum allowed drift in nanoseconds.

Source

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.

Source

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::CounterOverflow if counter saturates
  • Returns ClockError::ClockDriftExceeded if wall time is too far ahead of physical time
Source

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.

Source

pub fn last_timestamp(&self) -> HlcTimestamp

Returns the last generated or merged timestamp.

Source

pub fn physical_clock(&self) -> &C

Returns a reference to the underlying physical clock.

Trait Implementations§

Source§

impl Default for HlcClock<SystemClock>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<C> Freeze for HlcClock<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for HlcClock<C>
where C: RefUnwindSafe,

§

impl<C> Send for HlcClock<C>

§

impl<C> Sync for HlcClock<C>
where C: Sync,

§

impl<C> Unpin for HlcClock<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for HlcClock<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for HlcClock<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V