pub struct RateLimitMiddleware { /* private fields */ }Expand description
The rate limiting middleware.
This middleware tracks rate limits and either delays or rejects requests based on the configured routes.
§Thread Safety
RateLimitMiddleware is Send + Sync and can be safely shared across
threads and async tasks. The internal state uses lock-free atomic operations
(via DashMap and atomic integers) to ensure correct behavior under
concurrent access. When cloned, clones share the same rate limit state,
so limits are enforced across all clones.
Implementations§
Source§impl RateLimitMiddleware
impl RateLimitMiddleware
Sourcepub fn builder() -> RateLimitBuilder
pub fn builder() -> RateLimitBuilder
Create a new builder for configuring the middleware.
Sourcepub fn cleanup(&self)
pub fn cleanup(&self)
Remove stale rate limit state entries that haven’t been accessed recently.
An entry is considered stale when its theoretical arrival time (TAT) has recovered past twice the limit window, meaning the burst capacity has been fully recovered for an extended period.
This method should be called periodically in long-running applications to prevent unbounded memory growth from accumulated state entries.
§Example
use route_ratelimit::RateLimitMiddleware;
use std::time::Duration;
let middleware = RateLimitMiddleware::builder()
.route(|r| r.limit(100, Duration::from_secs(10)))
.build();
// Call periodically to clean up stale entries
middleware.cleanup();Sourcepub fn state_count(&self) -> usize
pub fn state_count(&self) -> usize
Returns the number of active rate limit state entries.
This can be useful for monitoring memory usage.
Trait Implementations§
Source§impl Clone for RateLimitMiddleware
impl Clone for RateLimitMiddleware
Source§fn clone(&self) -> RateLimitMiddleware
fn clone(&self) -> RateLimitMiddleware
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RateLimitMiddleware
impl Debug for RateLimitMiddleware
Source§impl Default for RateLimitMiddleware
impl Default for RateLimitMiddleware
Source§fn default() -> Self
fn default() -> Self
Create a middleware with no routes configured.
All requests will pass through without any rate limiting.
Use RateLimitMiddleware::builder() to configure routes.
Source§impl Middleware for RateLimitMiddleware
impl Middleware for RateLimitMiddleware
Source§fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = MiddlewareResult<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn handle<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
req: Request,
extensions: &'life1 mut Extensions,
next: Next<'life2>,
) -> Pin<Box<dyn Future<Output = MiddlewareResult<Response>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
next.run(req, extensions). Read more