pub struct RateLimiter;Expand description
Static registry for named rate limiters
Register named limiters with closures that receive the request and return dynamic rate limits. Closures are evaluated per-request, enabling limits based on authentication state, user tier, or request properties.
§Example
ⓘ
use ferro::middleware::{RateLimiter, Limit};
// Register in bootstrap
RateLimiter::define("api", |req| {
Limit::per_minute(60)
});
// Dynamic limits based on auth
RateLimiter::define("api", |req| {
match req.header("X-API-Key") {
Some(_) => Limit::per_minute(120),
None => Limit::per_minute(30),
}
});
// Multiple limits
RateLimiter::define("login", |req| {
vec![
Limit::per_minute(500),
Limit::per_minute(5).by("per-ip".to_string()),
]
});Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn define<F, T>(name: &str, f: F)
pub fn define<F, T>(name: &str, f: F)
Register a named rate limiter
The closure receives &Request and returns a Limit or Vec<Limit>.
Sourcepub fn resolve(name: &str, req: &Request) -> Option<Vec<Limit>>
pub fn resolve(name: &str, req: &Request) -> Option<Vec<Limit>>
Resolve a named limiter for a given request
Returns None if the named limiter is not registered.
Sourcepub fn per_second(max: u32) -> Limit
pub fn per_second(max: u32) -> Limit
Create an inline limit of N requests per second
Sourcepub fn per_minute(max: u32) -> Limit
pub fn per_minute(max: u32) -> Limit
Create an inline limit of N requests per minute
Auto Trait Implementations§
impl Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl UnwindSafe for RateLimiter
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: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more