pub struct RateLimiter { /* private fields */ }Expand description
A token-bucket rate limiter.
Limits the rate of operations to rate per interval. When the
rate is exceeded, further requests are denied until the next window.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new_rate(rate: u64) -> Self
pub fn new_rate(rate: u64) -> Self
Create a new rate limiter allowing rate operations per second.
Sourcepub fn new_rate_interval(rate: u64, interval: Duration) -> Self
pub fn new_rate_interval(rate: u64, interval: Duration) -> Self
Create a new rate limiter with a custom rate and interval.
Sourcepub async fn check(&self) -> bool
pub async fn check(&self) -> bool
Check whether an operation is allowed right now.
Returns true if within the rate limit, false if the bucket is exhausted.
Sourcepub async fn time_until_available(&self) -> Duration
pub async fn time_until_available(&self) -> Duration
Returns the duration until the next token becomes available.
Returns Duration::ZERO if tokens are immediately available,
Duration::MAX if the rate is zero.
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