Skip to main content

token_bucket/gcra/
struct.rs

1use super::*;
2
3/// Immutable configuration for a GCRA (Generic Cell Rate Algorithm) limiter.
4#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
5pub struct GcraConfig {
6    /// Emission interval `T` in nanoseconds per admitted unit.
7    pub(crate) emission_interval_ns: u64,
8    /// Maximum burst capacity `B` in request units.
9    pub(crate) burst: u64,
10    /// Precomputed burst window `B * T` in nanoseconds.
11    pub(crate) burst_offset_ns: u128,
12    /// Precomputed reciprocal divider for `emission_interval_ns`.
13    pub(crate) interval_div: FastDivU64,
14}
15
16/// Lock-free single-`AtomicU64` GCRA state cell usable standalone or inside sharded maps.
17#[derive(Debug)]
18pub struct GcraCell {
19    /// Theoretical Arrival Time (TAT) in monotonic nanoseconds.
20    pub(crate) tat_ns: AtomicU64,
21}
22
23/// Lock-free atomic GCRA rate limiter with an embedded monotonic nanosecond clock.
24#[derive(Debug)]
25pub struct GcraLimiter {
26    /// Validated GCRA rate and burst configuration.
27    pub(crate) config: GcraConfig,
28    /// Monotonic nanosecond clock anchored at limiter creation.
29    pub(crate) clock: MonotonicClock,
30    /// Atomic Theoretical Arrival Time cell.
31    pub(crate) cell: GcraCell,
32}