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.
If not set, all clients share a single rate limit (global limiting).
Trait Implementations§
Source§impl Debug for RateLimitingMiddleware
impl Debug for RateLimitingMiddleware
Source§impl Middleware for RateLimitingMiddleware
impl Middleware for RateLimitingMiddleware
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 RateLimitingMiddleware
impl !RefUnwindSafe for RateLimitingMiddleware
impl Send for RateLimitingMiddleware
impl Sync for RateLimitingMiddleware
impl Unpin for RateLimitingMiddleware
impl !UnwindSafe for RateLimitingMiddleware
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).