pub struct RateLimitMiddleware { /* private fields */ }Expand description
Rate limiting middleware.
Tracks request rates per key and returns 429 Too Many Requests when a client exceeds the configured limit.
§Example
ⓘ
use fastapi_core::middleware::{RateLimitMiddleware, RateLimitAlgorithm, IpKeyExtractor};
use std::time::Duration;
let rate_limiter = RateLimitMiddleware::builder()
.requests(100)
.per(Duration::from_secs(60))
.algorithm(RateLimitAlgorithm::TokenBucket)
.key_extractor(IpKeyExtractor)
.build();
let app = App::builder()
.middleware(rate_limiter)
.build();Implementations§
Source§impl RateLimitMiddleware
impl RateLimitMiddleware
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new rate limiter with default settings (100 requests/minute, token bucket, IP-based).
Sourcepub fn builder() -> RateLimitBuilder
pub fn builder() -> RateLimitBuilder
Create a builder for configuring the rate limiter.
Trait Implementations§
Source§impl Default for RateLimitMiddleware
impl Default for RateLimitMiddleware
Source§impl Middleware for RateLimitMiddleware
impl Middleware for RateLimitMiddleware
Source§fn before<'a>(
&'a self,
_ctx: &'a RequestContext,
req: &'a mut Request,
) -> BoxFuture<'a, ControlFlow>
fn before<'a>( &'a self, _ctx: &'a RequestContext, req: &'a mut Request, ) -> BoxFuture<'a, ControlFlow>
Called before the handler executes. Read more
Auto Trait Implementations§
impl Freeze for RateLimitMiddleware
impl !RefUnwindSafe for RateLimitMiddleware
impl Send for RateLimitMiddleware
impl Sync for RateLimitMiddleware
impl Unpin for RateLimitMiddleware
impl !UnwindSafe for RateLimitMiddleware
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).