pub struct KeyedRateLimiter<K: Hash + Eq> { /* private fields */ }Expand description
Allows limit requests per per and key, as a burst, refilling one
request every per / limit (generic cell rate algorithm: one timestamp
per key). The keys are sharded, so concurrent requests from different
keys rarely wait for each other.
It counts in this process: each instance of the app keeps its own counts.
The per-IP limit and route throttles
(RouteDef::throttle) count like this,
and in a shared RateLimitStore too when http.rate_limit_store names
one.
use std::time::Duration;
use navree_http::KeyedRateLimiter;
let limiter = KeyedRateLimiter::new(2, Duration::from_secs(60));
assert!(limiter.check(&"client-a").is_ok());
assert!(limiter.check(&"client-a").is_ok());
let limited = limiter.check(&"client-a").unwrap_err();
assert!(limited.retry_after > Duration::ZERO);
assert!(limiter.check(&"client-b").is_ok());Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<K> !RefUnwindSafe for KeyedRateLimiter<K>
impl<K> Forget for KeyedRateLimiter<K>
impl<K> Freeze for KeyedRateLimiter<K>
impl<K> Move for KeyedRateLimiter<K>
impl<K> Send for KeyedRateLimiter<K>
impl<K> Sync for KeyedRateLimiter<K>
impl<K> Unpin for KeyedRateLimiter<K>
impl<K> UnsafeUnpin for KeyedRateLimiter<K>where
DashMap<K, Slot>: UnsafeUnpin,
impl<K> UnwindSafe for KeyedRateLimiter<K>where
DashMap<K, Slot>: 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