Skip to main content

krabka_hlc/
error.rs

1//! The error type that every fallible hybrid logical clock operation returns.
2
3use thiserror::Error;
4
5use crate::stamp::HLC_ENCODED_LEN;
6
7/// Why a hybrid logical clock operation failed.
8///
9/// No variant holds the input bytes that failed. A peer controls the value of
10/// the `krabka.hlc` header, and a message that keeps those bytes would put
11/// hostile input into the log field that shows the error. The variants carry
12/// lengths, counts, and time deltas only.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
14#[non_exhaustive]
15pub enum HlcError {
16    /// The header value is not exactly [`HLC_ENCODED_LEN`] bytes.
17    #[error("hlc header value must be {HLC_ENCODED_LEN} bytes, got {0}")]
18    Length(usize),
19
20    /// The node id is wider than the 4 bytes the layout gives it.
21    ///
22    /// `NodeId` holds a `u64`, and Kafka carries the same value as a 4-byte
23    /// wire field. A node id above `u32::MAX` has no place in the layout, so
24    /// the encode fails instead of a silent truncation.
25    #[error("node id {0} does not fit the 4-byte node-id field")]
26    NodeIdTooLarge(u64),
27
28    /// The peer's wall time is further ahead of the local physical clock than
29    /// the configured maximum offset, so the clock refused the stamp.
30    #[error(
31        "peer wall time is {ahead_micros} microseconds ahead of the local physical clock, \
32         over the {max_micros} microsecond maximum offset"
33    )]
34    PeerTooFarAhead {
35        /// How far the peer's wall time is ahead of the local physical clock.
36        ahead_micros: i64,
37        /// The configured maximum offset.
38        max_micros: i64,
39    },
40
41    /// The logical counter reached `u32::MAX` inside one microsecond.
42    ///
43    /// The counter separates events that share a wall-time microsecond. It
44    /// never wraps, because a wrap would put a later event before an earlier
45    /// one.
46    #[error("hlc counter reached u32::MAX inside one microsecond")]
47    CounterOverflow,
48}