pub struct RateLimiter { /* private fields */ }Expand description
Counts attempts per key and window against any Cache.
Implementations§
Source§impl RateLimiter
impl RateLimiter
pub fn new(store: Arc<dyn Cache>) -> Self
Sourcepub fn with_driver(store: impl Cache) -> Self
pub fn with_driver(store: impl Cache) -> Self
Build a limiter over a driver held by value.
Sourcepub async fn attempt(
&self,
key: &str,
limit: u64,
window: Duration,
) -> Result<RateLimit>
pub async fn attempt( &self, key: &str, limit: u64, window: Duration, ) -> Result<RateLimit>
Record one attempt against key and report where it stands.
The counter is incremented even when the limit is already exceeded. That is deliberate: a client that keeps hammering after a 429 keeps its window alive, which is exactly the behaviour you want from a throttle.
Sourcepub async fn too_many(
&self,
key: &str,
limit: u64,
window: Duration,
) -> Result<bool>
pub async fn too_many( &self, key: &str, limit: u64, window: Duration, ) -> Result<bool>
Whether the key has already exhausted its window, without spending an attempt on the question.
Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Source§fn clone(&self) -> RateLimiter
fn clone(&self) -> RateLimiter
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for RateLimiter
impl !UnwindSafe for RateLimiter
impl Freeze for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
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