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.
Auto Trait Implementations§
impl<I> Freeze for TokenBucket<I>where
I: Freeze,
impl<I> RefUnwindSafe for TokenBucket<I>where
I: RefUnwindSafe,
impl<I> Send for TokenBucket<I>where
I: Send,
impl<I> Sync for TokenBucket<I>where
I: Sync,
impl<I> Unpin for TokenBucket<I>where
I: Unpin,
impl<I> UnwindSafe for TokenBucket<I>where
I: UnwindSafe,
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
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