pub struct TokenBucket<I> { /* private fields */ }
Expand description
A token bucket used for rate limiting.
TokenBucket
implements rate limiting by “filling” a bucket with “tokens”
at a constant rate, and allowing tokens to be consumed from the bucket until
it is empty. This guarantees that a consumer may only maintain a rate of
consumption faster than the rate of refilling for a bounded amount of time
before they will catch up and find the bucket empty.
Note that the bucket has a maximum size beyond which no new tokens will be added. This prevents a long quiet period from building up a large backlog of tokens which can then be used in an intense and sustained burst.
This implementation does not require any background threads or timers to
operate; it refills the bucket during calls to try_take
, so no extra
infrastructure is required to use it.
Implementations§
Source§impl<I> TokenBucket<I>
impl<I> TokenBucket<I>
Sourcepub fn new(tokens_per_second: u64) -> TokenBucket<I>
pub fn new(tokens_per_second: u64) -> TokenBucket<I>
Constructs a new TokenBucket
and initializes it with one second’s
worth of tokens.
§Panics
new
panics if tokens_per_second
is greater than 2^56 - 1.
Source§impl<I: Instant> TokenBucket<I>
impl<I: Instant> TokenBucket<I>
Sourcepub fn try_take<BC: InstantContext<Instant = I>>(
&mut self,
bindings_ctx: &BC,
) -> bool
pub fn try_take<BC: InstantContext<Instant = I>>( &mut self, bindings_ctx: &BC, ) -> bool
Attempt to take a token from the bucket.
try_take
attempts to take a token from the bucket. If the bucket is
currently empty, then no token is available to be taken, and try_take
return false.