pub struct RateLimitConfig {
pub max_requests: u64,
pub window: Duration,
pub algorithm: RateLimitAlgorithm,
pub include_headers: bool,
pub retry_message: String,
}Expand description
Configuration for the rate limiting middleware.
Controls request rate limits using token bucket or sliding window algorithms. When the limit is exceeded, a 429 Too Many Requests response is returned.
§Defaults
| Setting | Default |
|---|---|
max_requests | 100 |
window | 60s |
algorithm | TokenBucket |
include_headers | true |
retry_message | “Rate limit exceeded. Please retry later.” |
§Response Headers (when include_headers is true)
X-RateLimit-Limit: Maximum requests per windowX-RateLimit-Remaining: Remaining requests in current windowX-RateLimit-Reset: Seconds until window resetsRetry-After: Seconds to wait (only on 429 responses)
§Example
ⓘ
use fastapi_core::middleware::{RateLimitBuilder, RateLimitAlgorithm};
let rate_limit = RateLimitBuilder::new()
.max_requests(1000)
.window_secs(3600) // 1000 req/hour
.algorithm(RateLimitAlgorithm::SlidingWindow)
.build();Fields§
§max_requests: u64Maximum number of requests allowed per window.
window: DurationTime window for the rate limit.
algorithm: RateLimitAlgorithmThe algorithm to use.
include_headers: boolWhether to include rate limit headers in responses.
retry_message: StringCustom message for 429 responses.
Trait Implementations§
Source§impl Clone for RateLimitConfig
impl Clone for RateLimitConfig
Source§fn clone(&self) -> RateLimitConfig
fn clone(&self) -> RateLimitConfig
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for RateLimitConfig
impl RefUnwindSafe for RateLimitConfig
impl Send for RateLimitConfig
impl Sync for RateLimitConfig
impl Unpin for RateLimitConfig
impl UnwindSafe for RateLimitConfig
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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).