pub struct RateLimiter { /* private fields */ }Expand description
Per-IP rate limiter using sliding window algorithm.
Uses DashMap for lock-free concurrent access, critical for high-RPS WAF scenarios.
Bounded to [MAX_ENTRIES] to prevent memory exhaustion under DDoS.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(limit_per_minute: u32) -> Self
pub fn new(limit_per_minute: u32) -> Self
Creates a new rate limiter with the specified limit per minute.
Sourcepub fn with_window(limit: u32, window: Duration) -> Self
pub fn with_window(limit: u32, window: Duration) -> Self
Creates a new rate limiter with custom window duration.
Sourcepub fn check_and_increment(&self, ip: &str) -> bool
pub fn check_and_increment(&self, ip: &str) -> bool
Checks if the IP is within rate limit and increments counter.
Returns true if the request is allowed, false if rate-limited.
Enforces [MAX_ENTRIES] capacity bound, evicting expired entries when full.
Sourcepub fn cleanup(&self)
pub fn cleanup(&self)
Cleans up stale entries older than 2x the window duration.
Call this periodically from a background task to prevent unbounded memory growth.
Sourcepub fn stats(&self) -> RateLimiterStats
pub fn stats(&self) -> RateLimiterStats
Returns statistics about the rate limiter.
Sourcepub fn max_entries(&self) -> usize
pub fn max_entries(&self) -> usize
Returns the maximum number of tracked IPs.
Auto Trait Implementations§
impl !Freeze for RateLimiter
impl !RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl UnwindSafe for RateLimiter
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
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more