pub struct RateLimiter { /* private fields */ }Expand description
Token-bucket rate limiter. Cheap to clone; the inner state is Arc<Mutex<_>>.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(rate_per_second: f64, burst: u32) -> Self
pub fn new(rate_per_second: f64, burst: u32) -> Self
Build a limiter that produces rate_per_second tokens, capped at burst.
§Panics
Panics if rate_per_second is not positive or burst is zero.
Sourcepub async fn acquire_n(&self, n: u32)
pub async fn acquire_n(&self, n: u32)
Wait until n tokens are available, then consume them.
§Panics
Panics if n exceeds the configured burst (it could never be granted).
Sourcepub async fn try_acquire(&self) -> bool
pub async fn try_acquire(&self) -> bool
Try to consume one token without waiting. Returns true on success.
Sourcepub async fn try_acquire_n(&self, n: u32) -> bool
pub async fn try_acquire_n(&self, n: u32) -> bool
Try to consume n tokens without waiting. Returns true on success.
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 Freeze for RateLimiter
impl !RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl !UnwindSafe 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