pub struct RateLimiter { /* private fields */ }Expand description
Fixed-window rate limiter, usable as middleware
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(limit: u32, window: Duration) -> Self
pub fn new(limit: u32, window: Duration) -> Self
Allow limit requests per window for each client IP
Sourcepub fn key_by<F>(self, f: F) -> Self
pub fn key_by<F>(self, f: F) -> Self
Choose the bucket per request, e.g. an API key header. Returning None
exempts the request from limiting.
Sourcepub fn trust_forwarded_for(self) -> Self
pub fn trust_forwarded_for(self) -> Self
Use the first X-Forwarded-For address, falling back to the peer IP.
Only enable this behind a reverse proxy that sets the header: otherwise clients choose their own bucket and can evade the limit.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for RateLimiter
impl !RefUnwindSafe for RateLimiter
impl !UnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin 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