token-bucket 11.2.0

Zero-dependency, lock-free atomic GCRA, Fixed-Window, Token-Bucket, and 64-way sharded bounded keyed rate-limiting engine.
Documentation
use super::*;

/// Immutable configuration for a GCRA (Generic Cell Rate Algorithm) limiter.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct GcraConfig {
    /// Emission interval `T` in nanoseconds per admitted unit.
    pub(crate) emission_interval_ns: u64,
    /// Maximum burst capacity `B` in request units.
    pub(crate) burst: u64,
    /// Precomputed burst window `B * T` in nanoseconds.
    pub(crate) burst_offset_ns: u128,
    /// Precomputed reciprocal divider for `emission_interval_ns`.
    pub(crate) interval_div: FastDivU64,
}

/// Lock-free single-`AtomicU64` GCRA state cell usable standalone or inside sharded maps.
#[derive(Debug)]
pub struct GcraCell {
    /// Theoretical Arrival Time (TAT) in monotonic nanoseconds.
    pub(crate) tat_ns: AtomicU64,
}

/// Lock-free atomic GCRA rate limiter with an embedded monotonic nanosecond clock.
#[derive(Debug)]
pub struct GcraLimiter {
    /// Validated GCRA rate and burst configuration.
    pub(crate) config: GcraConfig,
    /// Monotonic nanosecond clock anchored at limiter creation.
    pub(crate) clock: MonotonicClock,
    /// Atomic Theoretical Arrival Time cell.
    pub(crate) cell: GcraCell,
}