pub struct SlidingWindow { /* private fields */ }Expand description
Sliding window rate limiter
Tracks events over a rolling time window, allowing precise rate limiting over time periods like “100 queries per minute”.
Implementations§
Source§impl SlidingWindow
impl SlidingWindow
Sourcepub fn new(window_size: Duration, max_events: u32) -> Self
pub fn new(window_size: Duration, max_events: u32) -> Self
Create a new sliding window
§Arguments
window_size- Duration of the sliding windowmax_events- Maximum events allowed within the window
Sourcepub fn per_second(max_events: u32) -> Self
pub fn per_second(max_events: u32) -> Self
Create a sliding window for events per second
Sourcepub fn per_minute(max_events: u32) -> Self
pub fn per_minute(max_events: u32) -> Self
Create a sliding window for events per minute
Sourcepub fn try_record(&self) -> Result<(), SlidingWindowExceeded>
pub fn try_record(&self) -> Result<(), SlidingWindowExceeded>
Try to record an event
Returns Ok(()) if event was recorded, Err with wait time if limit exceeded.
Sourcepub fn try_record_n(&self, count: u32) -> Result<(), SlidingWindowExceeded>
pub fn try_record_n(&self, count: u32) -> Result<(), SlidingWindowExceeded>
Try to record multiple events
Sourcepub fn record_blocking(
&self,
timeout: Duration,
) -> Result<(), SlidingWindowExceeded>
pub fn record_blocking( &self, timeout: Duration, ) -> Result<(), SlidingWindowExceeded>
Record an event, blocking until allowed (with timeout)
Sourcepub fn current_count(&self) -> u32
pub fn current_count(&self) -> u32
Get current event count in window
Sourcepub fn remaining_capacity(&self) -> u32
pub fn remaining_capacity(&self) -> u32
Get remaining capacity
Sourcepub fn window_size(&self) -> Duration
pub fn window_size(&self) -> Duration
Get window size
Sourcepub fn max_events(&self) -> u32
pub fn max_events(&self) -> u32
Get max events
Sourcepub fn utilization(&self) -> f64
pub fn utilization(&self) -> f64
Get utilization ratio (0.0 - 1.0)
Sourcepub fn total_events(&self) -> u64
pub fn total_events(&self) -> u64
Get total events processed
Sourcepub fn rejected_events(&self) -> u64
pub fn rejected_events(&self) -> u64
Get total events rejected
Sourcepub fn rejection_rate(&self) -> f64
pub fn rejection_rate(&self) -> f64
Get rejection rate (0.0 - 1.0)
Sourcepub fn current_rate(&self) -> f64
pub fn current_rate(&self) -> f64
Get event rate (events per second)
Sourcepub fn time_until_available(&self) -> Duration
pub fn time_until_available(&self) -> Duration
Estimate time until an event can be recorded
Sourcepub fn set_max_events(&mut self, max_events: u32)
pub fn set_max_events(&mut self, max_events: u32)
Update max events (for dynamic limits)
Sourcepub fn set_window_size(&mut self, window_size: Duration)
pub fn set_window_size(&mut self, window_size: Duration)
Update window size (for dynamic limits)
Trait Implementations§
Source§impl Clone for SlidingWindow
impl Clone for SlidingWindow
Auto Trait Implementations§
impl !Freeze for SlidingWindow
impl !RefUnwindSafe for SlidingWindow
impl Send for SlidingWindow
impl Sync for SlidingWindow
impl Unpin for SlidingWindow
impl UnsafeUnpin for SlidingWindow
impl UnwindSafe for SlidingWindow
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
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: 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>
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>
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