pub struct RateLimiter<S: RateLimitStore = InMemoryRateLimitStore> { /* private fields */ }Expand description
Tower layer that rate-limits requests using a token bucket algorithm.
Requests that exceed the configured rate are delayed (not rejected), providing backpressure to clients while still eventually serving every request.
The rate limit key is derived from each request by a user-provided
function. Use global or per_host
for common strategies, or keyed for custom keying
(e.g., per API key).
For multi-window limiting, stack multiple layers:
§Examples
use std::time::Duration;
use noxy::{Proxy, middleware::RateLimiter};
let proxy = Proxy::builder()
.ca_pem_files("ca-cert.pem", "ca-key.pem")?
.layer(RateLimiter::global(30, Duration::from_secs(1)))
.layer(RateLimiter::keyed(100, Duration::from_secs(1), |req| {
req.headers()
.get("x-api-key")
.and_then(|v| v.to_str().ok())
.unwrap_or("anonymous")
.to_string()
}))
.build()?;Implementations§
Source§impl<S: RateLimitStore> RateLimiter<S>
impl<S: RateLimitStore> RateLimiter<S>
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn keyed(
count: u64,
window: Duration,
key_fn: impl Fn(&Request<Body>) -> String + Send + Sync + 'static,
) -> Self
pub fn keyed( count: u64, window: Duration, key_fn: impl Fn(&Request<Body>) -> String + Send + Sync + 'static, ) -> Self
Rate-limit with a custom key function. Each distinct key gets its own
token bucket. count requests are allowed per window duration.
Sourcepub fn global(count: u64, window: Duration) -> Self
pub fn global(count: u64, window: Duration) -> Self
Rate-limit globally across all hosts with a single shared bucket.
count requests are allowed per window duration.
Sourcepub fn per_host(count: u64, window: Duration) -> Self
pub fn per_host(count: u64, window: Duration) -> Self
Rate-limit per unique hostname. Each host gets its own token bucket.
count requests are allowed per window duration.
Sourcepub fn burst(self, burst: u64) -> Self
pub fn burst(self, burst: u64) -> Self
Set the maximum burst size (max accumulated tokens). Defaults to
count.