token_bucket/fixed_window/struct.rs
1use super::*;
2
3/// Immutable configuration for a lock-free atomic fixed-window rate limiter.
4#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
5pub struct FixedWindowConfig {
6 /// Maximum units permitted per fixed window (`1..=u32::MAX`).
7 pub(crate) limit: u64,
8 /// Fixed window duration in nanoseconds (`>= 1`).
9 pub(crate) window_ns: u64,
10 /// Precomputed reciprocal divider for `window_ns`.
11 pub(crate) window_div: FastDivU64,
12 /// Bit width reserved for the request counter (`1..=32`).
13 pub(crate) count_bits: u32,
14 /// Bitmask extracting the request counter from the packed state word.
15 pub(crate) count_mask: u64,
16 /// Bitmask extracting the wrapped window index from the packed state word.
17 pub(crate) window_mask: u64,
18 /// Half-ring threshold for monotonic modular window comparison.
19 pub(crate) window_half: u64,
20}
21
22/// Lock-free single-`AtomicU64` fixed-window state cell packing `(window_id, count)`.
23#[derive(Debug)]
24pub struct FixedWindowCell {
25 /// Packed `(window_id << count_bits) | count` atomic state word.
26 pub(crate) packed: AtomicU64,
27}
28
29/// Lock-free atomic fixed-window rate limiter with an embedded monotonic clock.
30#[derive(Debug)]
31pub struct FixedWindowLimiter {
32 /// Validated fixed-window configuration.
33 pub(crate) config: FixedWindowConfig,
34 /// Monotonic nanosecond clock anchored at limiter creation.
35 pub(crate) clock: MonotonicClock,
36 /// Atomic packed window and counter cell.
37 pub(crate) cell: FixedWindowCell,
38}