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.
Trait Implementations§
Source§impl Middleware for SlidingWindowRateLimitingMiddleware
impl Middleware for SlidingWindowRateLimitingMiddleware
Source§fn on_request(
&self,
ctx: &McpContext,
request: &JsonRpcRequest,
) -> McpResult<MiddlewareDecision>
fn on_request( &self, ctx: &McpContext, request: &JsonRpcRequest, ) -> McpResult<MiddlewareDecision>
Invoked before routing the request. Read more
Source§fn on_response(
&self,
_ctx: &McpContext,
_request: &JsonRpcRequest,
response: Value,
) -> McpResult<Value>
fn on_response( &self, _ctx: &McpContext, _request: &JsonRpcRequest, response: Value, ) -> McpResult<Value>
Invoked after a successful handler result is produced. Read more
Source§fn on_error(
&self,
_ctx: &McpContext,
_request: &JsonRpcRequest,
error: McpError,
) -> McpError
fn on_error( &self, _ctx: &McpContext, _request: &JsonRpcRequest, error: McpError, ) -> McpError
Invoked when a handler or middleware returns an error. Read more
Auto Trait Implementations§
impl !Freeze for SlidingWindowRateLimitingMiddleware
impl !RefUnwindSafe for SlidingWindowRateLimitingMiddleware
impl Send for SlidingWindowRateLimitingMiddleware
impl Sync for SlidingWindowRateLimitingMiddleware
impl Unpin for SlidingWindowRateLimitingMiddleware
impl !UnwindSafe for SlidingWindowRateLimitingMiddleware
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
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).