pub struct RateLimitingMiddleware { /* private fields */ }Expand description
Rate limiting middleware using token bucket algorithm.
Uses a token bucket algorithm by default, allowing for burst traffic while maintaining a sustainable long-term rate.
§Example
use fastmcp_server::rate_limiting::RateLimitingMiddleware;
// Allow 10 requests per second with bursts up to 20
let rate_limiter = RateLimitingMiddleware::new(10.0)
.burst_capacity(20);Implementations§
Source§impl RateLimitingMiddleware
impl RateLimitingMiddleware
Sourcepub fn new(max_requests_per_second: f64) -> Self
pub fn new(max_requests_per_second: f64) -> Self
Creates a new rate limiting middleware with the specified rate.
§Arguments
max_requests_per_second- Sustained requests per second allowed
Burst capacity defaults to 2x the sustained rate.
Sourcepub fn burst_capacity(self, capacity: usize) -> Self
pub fn burst_capacity(self, capacity: usize) -> Self
Sets the burst capacity (maximum tokens in the bucket).
Sourcepub fn client_id_extractor<F>(self, extractor: F) -> Self
pub fn client_id_extractor<F>(self, extractor: F) -> Self
Sets a custom function to extract client ID from the request context.
Identifiers longer than 4096 bytes are rejected. Accepted identifiers are immediately reduced to fixed-width SHA-256 partition keys; their raw values are neither retained nor included in rate-limit errors or debug output. At most 4095 distinct keys receive dedicated partitions. At the cap, a least-recently-used partition idle for at least 60 seconds is reclaimed only after its limiter has naturally reset; otherwise new keys share one overflow partition.
If not set, all clients share the default identity while each method retains an independent rate-limit partition.