pub struct InMemoryRateLimitStore { /* private fields */ }Expand description
In-memory rate limit store.
Uses bounded HashMaps protected by Mutexes for thread-safe access.
Each algorithm retains at most max_keys distinct keys. When a map is full,
stale entries are reclaimed no more than once per second; if a map
remains full, unseen keys fail closed instead of evicting live buckets.
Suitable for single-process deployments. For distributed systems,
implement a custom store using Redis or similar.
Implementations§
Source§impl InMemoryRateLimitStore
impl InMemoryRateLimitStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new in-memory store with DEFAULT_RATE_LIMIT_MAX_KEYS per algorithm.
Sourcepub fn with_max_keys(max_keys: usize) -> Self
pub fn with_max_keys(max_keys: usize) -> Self
Create an in-memory store with an explicit per-algorithm key bound.
A bound of zero fails closed for every keyed request.
Sourcepub fn check(
&self,
key: &str,
algorithm: RateLimitAlgorithm,
max_requests: u64,
window: Duration,
) -> RateLimitResult
pub fn check( &self, key: &str, algorithm: RateLimitAlgorithm, max_requests: u64, window: Duration, ) -> RateLimitResult
Check and consume a request against the rate limit.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for InMemoryRateLimitStore
impl !RefUnwindSafe for InMemoryRateLimitStore
impl Send for InMemoryRateLimitStore
impl Sync for InMemoryRateLimitStore
impl Unpin for InMemoryRateLimitStore
impl UnsafeUnpin for InMemoryRateLimitStore
impl UnwindSafe for InMemoryRateLimitStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).