pub struct SlidingWindowRateLimitingMiddleware { /* private fields */ }Expand description
Rate limiting middleware using sliding window algorithm.
Uses a sliding window approach which provides more precise rate limiting but uses more memory to track individual request timestamps.
§Example
use fastmcp_server::rate_limiting::SlidingWindowRateLimitingMiddleware;
// Allow 100 requests per minute
let rate_limiter = SlidingWindowRateLimitingMiddleware::new(100, 60);Implementations§
Source§impl SlidingWindowRateLimitingMiddleware
impl SlidingWindowRateLimitingMiddleware
Sourcepub fn new(max_requests: usize, window_seconds: u64) -> Self
pub fn new(max_requests: usize, window_seconds: u64) -> Self
Creates a new sliding window rate limiting middleware.
§Arguments
max_requests- Maximum requests allowed in the time windowwindow_seconds- Time window duration in seconds
Sourcepub fn per_minute(max_requests: usize, window_minutes: u64) -> Self
pub fn per_minute(max_requests: usize, window_minutes: u64) -> Self
Creates a sliding window rate limiter with minutes-based window.
§Arguments
max_requests- Maximum requests allowed in the time windowwindow_minutes- Time window duration in minutes
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.