pub struct KeyedRateLimiter<K> { /* private fields */ }Expand description
Per-key rate limiter using a sliding window algorithm.
Each key (e.g., trading pair) has its own rate limit tracking. Useful for endpoints like order book that have per-pair limits.
Implementations§
Source§impl<K> KeyedRateLimiter<K>
impl<K> KeyedRateLimiter<K>
Sourcepub fn new(window: Duration, max_requests: u32) -> Self
pub fn new(window: Duration, max_requests: u32) -> Self
Create a new per-key rate limiter.
§Arguments
window- The sliding window durationmax_requests- Maximum number of requests allowed per window
Sourcepub fn try_acquire(&mut self, key: K) -> Result<(), Duration>
pub fn try_acquire(&mut self, key: K) -> Result<(), Duration>
Try to acquire a permit for the given key.
Returns Ok(()) if the request is allowed, or Err(wait_time) if
the rate limit has been exceeded and you need to wait.
Sourcepub fn would_allow(&self, key: &K) -> bool
pub fn would_allow(&self, key: &K) -> bool
Check if a request for the given key would be allowed without consuming a permit.
Sourcepub fn time_until_available(&self, key: &K) -> Option<Duration>
pub fn time_until_available(&self, key: &K) -> Option<Duration>
Get the time until the next permit is available for a key.
Sourcepub fn cleanup(&mut self)
pub fn cleanup(&mut self)
Clean up limiters that haven’t been used recently.
Removes limiters where all requests have expired from the window.
Sourcepub fn tracked_keys(&self) -> usize
pub fn tracked_keys(&self) -> usize
Get the number of keys being tracked.
Trait Implementations§
Source§impl<K: Debug> Debug for KeyedRateLimiter<K>
impl<K: Debug> Debug for KeyedRateLimiter<K>
Auto Trait Implementations§
impl<K> Freeze for KeyedRateLimiter<K>
impl<K> RefUnwindSafe for KeyedRateLimiter<K>where
K: RefUnwindSafe,
impl<K> Send for KeyedRateLimiter<K>where
K: Send,
impl<K> Sync for KeyedRateLimiter<K>where
K: Sync,
impl<K> Unpin for KeyedRateLimiter<K>where
K: Unpin,
impl<K> UnwindSafe for KeyedRateLimiter<K>where
K: UnwindSafe,
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