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§
- HLC
- An Hybric Logical Clock generating
Timestamp
s - HLCBuilder
- The builder of
HLC
. - ID
- An identifier for an HLC (MAX_SIZE bytes maximum).
This struct has a constant memory size (holding internally a
NonZeroU8
), allowing allocations on the stack for better performances. - NTP64
- A NTP 64-bits format as specified in RFC-5909
- ParseID
Error - ParseNT
P64Error - Parse
Timestamp Error - Size
Error - Timestamp
- A timestamp made of a
NTP64
and acrate::HLC
’s unique identifier.
Constants§
Functions§
- system_
time_ clock - A physical clock relying on std::time::SystemTime::now().
- zero_
clock - 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 anHLC
ifHLCBuilder::with_clock()
is not called. Notice that this means that theHLC
will use incremental timestamps starting from 0.