pub struct AsyncLimiter<C: Clock + Clone = SystemClock> { /* private fields */ }Expand description
An async-friendly wrapper around a RateLimiter.
Holds a limiter and adds until_ready /
until_ready_n, which await until the key is admitted
rather than returning a denial. The synchronous check /
check_n pass straight through.
§Examples
use rate_net::{AsyncLimiter, RateLimiter};
let limiter = AsyncLimiter::new(RateLimiter::per_second(100));
// Non-blocking: returns immediately, allow or deny.
let _ = limiter.check("user:42");
// Awaiting: returns once the key is within its limit.
limiter.until_ready("user:42").await;Implementations§
Source§impl<C: Clock + Clone> AsyncLimiter<C>
impl<C: Clock + Clone> AsyncLimiter<C>
Sourcepub fn new(inner: RateLimiter<C>) -> Self
pub fn new(inner: RateLimiter<C>) -> Self
Wraps a limiter for async use.
Sourcepub fn inner(&self) -> &RateLimiter<C>
pub fn inner(&self) -> &RateLimiter<C>
Borrows the wrapped limiter.
Sourcepub fn into_inner(self) -> RateLimiter<C>
pub fn into_inner(self) -> RateLimiter<C>
Unwraps back into the limiter.
Sourcepub fn check(&self, key: impl Into<Key>) -> Decision
pub fn check(&self, key: impl Into<Key>) -> Decision
Checks a single unit against key without awaiting — a straight
pass-through to RateLimiter::check.
Sourcepub fn check_n(&self, key: impl Into<Key>, n: u32) -> Decision
pub fn check_n(&self, key: impl Into<Key>, n: u32) -> Decision
Checks n units against key without awaiting — a straight pass-through
to RateLimiter::check_n.
Sourcepub async fn until_ready(&self, key: impl Into<Key>)
pub async fn until_ready(&self, key: impl Into<Key>)
Awaits until a single unit is admitted for key.
Equivalent to until_ready_n(key, 1).
Sourcepub async fn until_ready_n(&self, key: impl Into<Key>, n: u32)
pub async fn until_ready_n(&self, key: impl Into<Key>, n: u32)
Awaits until n units are admitted for key, sleeping for the reported
retry_after after each denial.
Returns immediately if n exceeds what the limit can ever grant (a
retry_after of Duration::MAX): no amount of waiting would help, so
it gives up rather than sleep forever.