Crate uhlc

source ·
Expand description

A Unique Hybrid Logical Clock.

This library is an implementation of an Hybrid Logical Clock (HLC) associated to a unique identifier. Thus, it is able to generate timestamps that are unique across a distributed system, without the need of a centralized time source.

§Quick Start

use uhlc::HLC;

// create an HLC with a generated random ID and relying on SystemTime::now()
let hlc = HLC::default();

// generate timestamps
let ts1 = hlc.new_timestamp();
let ts2 = hlc.new_timestamp();
assert!(ts2 > ts1);

// update the HLC with a timestamp incoming from another HLC
// (typically remote, but not in this example...)
let hlc2 = HLC::default();
let other_ts = hlc2.new_timestamp();

if ! hlc.update_with_timestamp(&other_ts).is_ok() {
    println!(r#"The incoming timestamp would make this HLC
             to drift too much. You should refuse it!"#);
}

let ts3 = hlc.new_timestamp();
assert!(ts3 > ts2);
assert!(ts3 > other_ts);

Structs§

Constants§

Functions§

  • A physical clock relying on std::time::SystemTime::now().
  • A dummy clock that returns a NTP64 initialized with the value 0. Suitable to use in no_std environments where std::time::{SystemTime, UNIX_EPOCH} are not available. If the feature std is disabled, that’s the default clock used by an HLC if HLCBuilder::with_clock() is not called. Notice that this means that the HLC will use incremental timestamps starting from 0.