pub struct SlidingWindow { /* private fields */ }Expand description
A sliding window rate limiter.
Tracks request timestamps within a sliding window and enforces a maximum number of requests within that window.
Implementations§
Source§impl SlidingWindow
impl SlidingWindow
Sourcepub fn new(window: Duration, max_requests: u32) -> Self
pub fn new(window: Duration, max_requests: u32) -> Self
Create a new sliding window rate limiter.
Sourcepub fn try_acquire(&mut self) -> Result<(), Duration>
pub fn try_acquire(&mut self) -> Result<(), Duration>
Try to acquire a permit.
Returns Ok(()) if allowed, Err(wait_time) if rate limited.
Sourcepub fn would_allow(&self) -> bool
pub fn would_allow(&self) -> bool
Check if a request would be allowed without consuming a permit.
Sourcepub fn time_until_available(&self) -> Option<Duration>
pub fn time_until_available(&self) -> Option<Duration>
Get the time until the next permit is available.
Returns None if a permit is available now.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SlidingWindow
impl RefUnwindSafe for SlidingWindow
impl Send for SlidingWindow
impl Sync for SlidingWindow
impl Unpin 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
Mutably borrows from an owned value. Read more