token-bucket 11.0.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 lock-free atomic fixed-window rate limiter.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct FixedWindowConfig {
    /// Maximum units permitted per fixed window (`1..=u32::MAX`).
    pub(crate) limit: u64,
    /// Fixed window duration in nanoseconds (`>= 1`).
    pub(crate) window_ns: u64,
    /// Precomputed reciprocal divider for `window_ns`.
    pub(crate) window_div: FastDivU64,
    /// Bit width reserved for the request counter (`1..=32`).
    pub(crate) count_bits: u32,
    /// Bitmask extracting the request counter from the packed state word.
    pub(crate) count_mask: u64,
    /// Bitmask extracting the wrapped window index from the packed state word.
    pub(crate) window_mask: u64,
    /// Half-ring threshold for monotonic modular window comparison.
    pub(crate) window_half: u64,
}

/// Lock-free single-`AtomicU64` fixed-window state cell packing `(window_id, count)`.
#[derive(Debug)]
pub struct FixedWindowCell {
    /// Packed `(window_id << count_bits) | count` atomic state word.
    pub(crate) packed: AtomicU64,
}

/// Lock-free atomic fixed-window rate limiter with an embedded monotonic clock.
#[derive(Debug)]
pub struct FixedWindowLimiter {
    /// Validated fixed-window configuration.
    pub(crate) config: FixedWindowConfig,
    /// Monotonic nanosecond clock anchored at limiter creation.
    pub(crate) clock: MonotonicClock,
    /// Atomic packed window and counter cell.
    pub(crate) cell: FixedWindowCell,
}