pub struct TokenBucket { /* private fields */ }Expand description
Token bucket rate limiter.
Implements the token bucket algorithm where tokens are added at a fixed rate up to a maximum capacity. Each request consumes one token.
The rate is a fraction of a token per second, not a whole number. A limit of 3 requests a minute is 0.05 tokens a second; when the rate was carried as an integer that rounded to 0, the bucket never refilled, and a client that spent its burst was locked out until the process restarted.
The count and the time it was last brought up to date live under one lock. They used to be two atomics updated by separate compare-and-swaps, which let two concurrent requests both credit the same elapsed time.
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn new(capacity: u64, refill_rate: f64) -> Self
pub fn new(capacity: u64, refill_rate: f64) -> Self
Create a new token bucket, full.
§Arguments
capacity- Maximum tokens (burst size)refill_rate- Tokens added per second; may be fractional
Sourcepub fn try_acquire(&self) -> bool
pub fn try_acquire(&self) -> bool
Try to consume a token.
Returns true if a token was available and consumed, false otherwise.
Sourcepub fn try_acquire_n(&self, n: u64) -> bool
pub fn try_acquire_n(&self, n: u64) -> bool
Try to consume N tokens.
Returns true if N tokens were available and consumed, false otherwise.
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<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more