pub struct RateLimiter { /* private fields */ }Expand description
The sliding-window rate limiter over one shared in-memory store.
Build it once at startup with RateLimiter::new (fail closed on a
non-positive limit or window) and share the handle across requests; the
internal store is a mutex-guarded LRU, safe for concurrent services.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(config: RateLimitConfig) -> Result<Self, RateLimitConfigError>
pub fn new(config: RateLimitConfig) -> Result<Self, RateLimitConfigError>
Validate the config and build the limiter.
§Errors
Fails closed with a RateLimitConfigError when rate_limit or
rate_limit_window is zero (the reference rejects both with
ge=1); nothing but RateLimitConfig::default-shaped values is
substituted silently.
Sourcepub fn with_config_and_clock(
config: RateLimitConfig,
clock: Clock,
) -> Result<Self, RateLimitConfigError>
pub fn with_config_and_clock( config: RateLimitConfig, clock: Clock, ) -> Result<Self, RateLimitConfigError>
Validate the config and build the limiter over an injected clock: the
combined seam new and with_clock
cover separately. Production builds use RateLimiter::new; adapters
and stage tests that drive window sliding with a fake clock under a
production-shaped config use this one.
§Errors
Same fail-closed behavior as RateLimiter::new: a zero rate_limit
or rate_limit_window is a RateLimitConfigError.
Sourcepub fn with_clock(clock: Clock) -> Self
pub fn with_clock(clock: Clock) -> Self
Swap the wall clock. Test seam: production builds use the system clock; deterministic window-sliding coverage injects a fake.
Sourcepub const fn config(&self) -> &RateLimitConfig
pub const fn config(&self) -> &RateLimitConfig
The validated config the limiter decides under.
Sourcepub fn check(&self, ip: IpAddr, endpoint: Option<&str>) -> RateLimitDecision
pub fn check(&self, ip: IpAddr, endpoint: Option<&str>) -> RateLimitDecision
Record one request for ip and decide it.
endpoint selects the scope: None is the global per-IP window,
Some(path) the per-endpoint window keyed by (ip, path). A
disabled limiter allows without recording. The returned decision’s
count includes the request just recorded, so a block reports the
crossing count exactly as the references log it.
Sourcepub fn reset(&self)
pub fn reset(&self)
Drop every window, every recorded request included (reset in the
references; test harnesses use it to isolate cases).
Sourcepub fn tracked_windows(&self) -> usize
pub fn tracked_windows(&self) -> usize
How many windows the store currently tracks (test/observability seam for the LRU cap).
Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
A clone shares the window store and the clock, the references’ singleton semantics: requests recorded through any handle land in the same sliding windows.
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more