pub struct TokenBucket { /* private fields */ }Expand description
Single-instance token bucket rate limiter.
Tokens refill continuously at rate_per_second up to the burst
ceiling. A call to TokenBucket::try_acquire consumes one token;
returns true on success and false when the bucket is empty.
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn new(rate_per_second: f64, burst: u32) -> TokenBucket
pub fn new(rate_per_second: f64, burst: u32) -> TokenBucket
Create a bucket with rate_per_second refill rate and burst
capacity. Starts full. rate_per_second is clamped to >= 0.0; a
zero rate means the bucket never refills (only the initial burst
is available).
Sourcepub fn with_clock(
rate_per_second: f64,
burst: u32,
clock: Arc<dyn Clock>,
) -> TokenBucket
pub fn with_clock( rate_per_second: f64, burst: u32, clock: Arc<dyn Clock>, ) -> TokenBucket
Create a bucket with a custom clock.
Sourcepub fn rate_per_second(&self) -> f64
pub fn rate_per_second(&self) -> f64
Configured refill rate (tokens per second).
Sourcepub fn try_acquire(&self) -> bool
pub fn try_acquire(&self) -> bool
Attempt to consume one token. Returns true if a token was available
(and consumed); false otherwise.
Sourcepub fn try_acquire_n(&self, n: f64) -> bool
pub fn try_acquire_n(&self, n: f64) -> bool
Attempt to consume n tokens.
Auto Trait Implementations§
impl !Freeze for TokenBucket
impl !RefUnwindSafe for TokenBucket
impl Send for TokenBucket
impl Sync for TokenBucket
impl Unpin for TokenBucket
impl UnsafeUnpin for TokenBucket
impl !UnwindSafe for TokenBucket
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