pub struct MemoryRateLimiter { /* private fields */ }Expand description
In-memory token-bucket limiter — Phase 9 default substrate.
Per-key independent buckets. Each bucket holds up to capacity
tokens; one token is consumed per allow call. Buckets refill at
capacity / window tokens per second.
Algorithm: lazy refill on access — no background timer needed.
Bucket state is (tokens_remaining, last_refill_instant). On every
allow, compute elapsed since last_refill, add proportional
tokens (capped at capacity so a long-idle bucket doesn’t bank
more than its instantaneous quota), then attempt consume.
Capacity semantics: how many failures tolerated in a refill
window. For “audit emission rate-limiting” with capacity=10,
window=60s: each (client, kid) bucket admits 10 audit events
per minute, then refuses until the next refill epoch. Bursts of 10
admit instantly; sustained pressure drops to 1-per-6s steady state.
Implementations§
Source§impl MemoryRateLimiter
impl MemoryRateLimiter
Sourcepub fn new(capacity: u32, window: Duration) -> MemoryRateLimiter
pub fn new(capacity: u32, window: Duration) -> MemoryRateLimiter
Construct a limiter with capacity tokens per window.
assert! invariants — these are configuration bugs that
should fail loudly at startup, not silently at first failure
(per feedback_audit_grilled_decisions “easy path” check).
Both arguments are user-supplied at builder time, so this
runs once per verifier construction, not on the hot path.
§Panics
- if
capacity == 0(every call would refuse) - if
windowis zero (refill rate undefined)
pub fn with_clock(self, clock: Arc<dyn Clock>) -> MemoryRateLimiter
Trait Implementations§
Source§impl Debug for MemoryRateLimiter
impl Debug for MemoryRateLimiter
Source§impl Default for MemoryRateLimiter
impl Default for MemoryRateLimiter
Source§fn default() -> MemoryRateLimiter
fn default() -> MemoryRateLimiter
Default: 10 admits per 60s per key. Reasonable for audit emission throttling — one event every 6s per source under sustained pressure; bursts of 10 admit instantly.