pub struct HybridLogicalClock { /* private fields */ }Expand description
Hybrid Logical Clock for generating causally consistent timestamps.
Thread-safe via atomic operations. Each node should have one HLC instance.
§Example
use lnc_core::HybridLogicalClock;
let hlc = HybridLogicalClock::new(1);
let ts1 = hlc.now();
let ts2 = hlc.now();
assert!(ts1 < ts2);Implementations§
Source§impl HybridLogicalClock
impl HybridLogicalClock
Sourcepub const MAX_DRIFT_WARNING_MS: u64 = 100
pub const MAX_DRIFT_WARNING_MS: u64 = 100
Maximum drift allowed before warning (milliseconds).
Sourcepub const MAX_DRIFT_CRITICAL_MS: u64 = 1000
pub const MAX_DRIFT_CRITICAL_MS: u64 = 1000
Maximum drift allowed before critical alert (milliseconds).
Sourcepub fn now(&self) -> HlcTimestamp
pub fn now(&self) -> HlcTimestamp
Generate a new HLC timestamp for a local event.
This method is lock-free and thread-safe via CAS operations.
§Algorithm
- If wall clock > last physical: use wall clock, reset logical to 0
- Otherwise: keep physical, increment logical
- CAS to ensure monotonicity under concurrent access
Sourcepub fn receive(&self, remote_ts: HlcTimestamp) -> HlcTimestamp
pub fn receive(&self, remote_ts: HlcTimestamp) -> HlcTimestamp
Update HLC upon receiving a message with a remote timestamp.
Returns a new timestamp that is greater than both the local clock and the remote timestamp, ensuring causal ordering.
§Algorithm
- If wall clock > max(local, remote): use wall clock, reset logical
- If local > remote: use local physical, increment local logical
- If remote > local: use remote physical, increment remote logical
- If equal physical: use that physical, increment max logical
Sourcepub fn current(&self) -> HlcTimestamp
pub fn current(&self) -> HlcTimestamp
Get the current HLC timestamp without advancing it.
Useful for reading the last known time without generating a new event.
Sourcepub fn drift_ms(&self) -> i64
pub fn drift_ms(&self) -> i64
Calculate the drift between HLC physical time and wall clock.
Returns positive value if HLC is ahead of wall clock.
Sourcepub fn health(&self) -> ClockHealth
pub fn health(&self) -> ClockHealth
Check if the clock drift is within acceptable bounds.