Skip to main content

RateLimiter

Struct RateLimiter 

Source
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

Source

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.

Source

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.

Source

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.

Source

pub const fn config(&self) -> &RateLimitConfig

The validated config the limiter decides under.

Source

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.

Source

pub fn reset(&self)

Drop every window, every recorded request included (reset in the references; test harnesses use it to isolate cases).

Source

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

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for RateLimiter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.