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) -> FixedWindowRateLimiter
pub fn new(max_requests: u64, window_size: Duration) -> FixedWindowRateLimiter
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) -> FixedWindowRateLimiter
pub fn with_max_keys(self, max_keys: usize) -> FixedWindowRateLimiter
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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