Skip to main content

Module clock

Module clock 

Source
Expand description

Hybrid Logical Clock (HLC) for causal ordering.

HLC combines physical time with a logical counter to provide:

  • Monotonic timestamps even when the physical clock goes backward
  • Causal ordering without full vector clocks
  • Fixed size (12 bytes) regardless of the number of nodes

This is ideal for edge/IoT where clocks are not perfectly synchronized.

§Example

use crdt_kit::clock::{HybridClock, HybridTimestamp};

let mut clock = HybridClock::new(1); // node_id = 1

// Generate a timestamp for a local event
let ts1 = clock.now();
let ts2 = clock.now();
assert!(ts2 > ts1);

// Receive a timestamp from a remote node
let remote_ts = HybridTimestamp { physical: ts2.physical + 1000, logical: 0, node_id: 2 };
let ts3 = clock.receive(&remote_ts);
assert!(ts3 > remote_ts);

Structs§

HybridClock
A Hybrid Logical Clock instance for a single node.
HybridTimestamp
A timestamp from a Hybrid Logical Clock.