pub struct RateLimiter { /* private fields */ }Expand description
A sliding-window per-key rate limiter.
Each call to RateLimiter::check records a timestamp for the given key
and returns true when the number of calls within the current window is
still below max_requests. Returns false once the limit is exceeded.
Thread-safe: the internal state is behind a Mutex so it can be shared
across threads via global or wrapped in an Arc.
§Example
use rust_web_server::rate_limit::RateLimiter;
let limiter = RateLimiter::new(100, 60); // 100 req / 60 s
if limiter.check("192.168.1.1") {
// process request
} else {
// return 429 Too Many Requests
}Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(max_requests: u32, window_secs: u64) -> Self
pub fn new(max_requests: u32, window_secs: u64) -> Self
Create a new limiter allowing max_requests per window_secs-second window.
Sourcepub fn set_limits(&self, max_requests: u32, window_secs: u64)
pub fn set_limits(&self, max_requests: u32, window_secs: u64)
Update the limits on a live limiter without restarting.
Changes take effect on the next call to [check] or [remaining].
Called automatically by crate::config_reload::reload on SIGHUP.
Sourcepub fn check(&self, key: &str) -> bool
pub fn check(&self, key: &str) -> bool
Returns true if key (typically a client IP) is within the rate limit,
or false if the limit has been exceeded.
A permitted call is always recorded so it counts toward future limits.
Sourcepub fn retry_after(&self, _key: &str) -> u64
pub fn retry_after(&self, _key: &str) -> u64
Seconds a rejected key should wait before trying again.
The window length, a safe upper bound: the oldest request in the window may expire sooner, so a client that waits this long is never rejected again for the same burst — it may only wait longer than strictly needed.
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
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: 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> ⓘ
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> ⓘ
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