token-bucket 10.11.0

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

/// Compact 16-byte per-key atomic state stored inside a `KeyedRateLimiter` shard.
#[derive(Debug)]
pub(crate) struct KeyEntry {
    /// Algorithm-specific lock-free atomic state word.
    pub(crate) state: AtomicU64,
    /// Last monotonic access timestamp in nanoseconds for bounded LRU eviction.
    pub(crate) last_seen_ns: AtomicU64,
}

/// Cache-line-aligned shard protecting a partition of keyed rate-limit entries.
#[repr(align(64))]
#[derive(Debug)]
pub(crate) struct KeyedShard<K> {
    /// Read-mostly partition map keyed by `K` using zero-dependency `FastBuildHasher`.
    pub(crate) map: RwLock<HashMap<K, KeyEntry, FastBuildHasher>>,
}

/// 64-way cache-line-aligned sharded, bounded, lock-free-CAS keyed rate limiter.
#[derive(Debug)]
pub struct KeyedRateLimiter<K> {
    /// Shared algorithm policy applied uniformly to all keys.
    pub(crate) policy: RateLimitPolicy,
    /// Monotonic nanosecond clock shared across all shards.
    pub(crate) clock: MonotonicClock,
    /// Seeded zero-dependency hasher shared across shard selection and maps.
    pub(crate) hasher: FastBuildHasher,
    /// Requested maximum key capacity across the limiter.
    pub(crate) max_entries: usize,
    /// Hard upper bound enforced per individual shard.
    pub(crate) per_shard_capacity: usize,
    /// Fixed array of 64 cache-line-aligned shards.
    pub(crate) shards: Box<[KeyedShard<K>]>,
}