pub struct FixedWindowRateLimiter { /* private fields */ }Expand description
Fixed window rate limiter
Divides time into fixed-size windows; each key has an independent counter within each window.
Requests are rejected when the counter exceeds max_requests.
§Boundary burst
The fixed window algorithm has the boundary burst problem: if max_requests requests pass
in the last 1 second before the window ends, and another max_requests requests pass in the
first 1 second of the new window, then 2 * max_requests requests pass within 2 seconds.
For smoother rate limiting, use SlidingWindowRateLimiter or TokenBucketRateLimiter.
Implementations§
Source§impl FixedWindowRateLimiter
impl FixedWindowRateLimiter
Sourcepub fn new(max_requests: u64, window_size: Duration) -> Self
pub fn new(max_requests: u64, window_size: Duration) -> Self
Creates a fixed window rate limiter
max_requests: maximum number of requests allowed within each windowwindow_size: window size (e.g., 60 seconds)
Sourcepub fn with_max_keys(self, max_keys: usize) -> Self
pub fn with_max_keys(self, max_keys: usize) -> Self
Configures the maximum number of keys (OOM protection)
pub fn max_requests(&self) -> u64
pub fn window_size(&self) -> Duration
pub fn max_keys(&self) -> usize
pub fn key_count(&self) -> usize
Trait Implementations§
Source§impl RateLimiter for FixedWindowRateLimiter
impl RateLimiter for FixedWindowRateLimiter
fn acquire(&self, key: &str) -> Result<RateLimitResult, RateLimitError>
fn try_acquire(&self, key: &str) -> Result<RateLimitResult, RateLimitError>
fn reset(&self, key: &str) -> Result<(), RateLimitError>
Auto Trait Implementations§
impl Freeze for FixedWindowRateLimiter
impl RefUnwindSafe for FixedWindowRateLimiter
impl Send for FixedWindowRateLimiter
impl Sync for FixedWindowRateLimiter
impl Unpin for FixedWindowRateLimiter
impl UnsafeUnpin for FixedWindowRateLimiter
impl UnwindSafe for FixedWindowRateLimiter
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