TokenBucket

Struct TokenBucket 

Source
pub struct TokenBucket<T>
where T: Fn() -> Duration,
{ /* private fields */ }
Expand description

Token bucket -type rate limiter

A token bucket limiter can be illustrated as a being filled with tokens at a constant rate, while consumes will remove tokens from the bucket.

This leads to a soft limiter where occasional burstiness is allowed since as long as the bucket holds tokens those can be consumed at an unlimited rate. Ultimately the bucket size is what defined the burstiness.

Implementations§

Source§

impl<T> TokenBucket<T>
where T: Fn() -> Duration,

Source

pub fn new_with_time_provider( rate_per_s: u64, capacity: u64, time_provider: T, ) -> Self

Initialize a new token bucket utilizing the given timer

§Arguments
  • rate_per_sec - how many consumes should be allowed per second on average
  • capacity - bucket capacity to dictate the burstiness of this limiter
  • time_provider_t - closure that returns a monotonically nondecreasing timestamp as Duration from some fixed epoch in the past

If you are developing for a std target, you probably wish to use token_bucket

Trait Implementations§

Source§

impl<T> Limiter for TokenBucket<T>
where T: Fn() -> Duration,

Source§

fn try_consume(&mut self, tokens: u64) -> LimiterResult

Try to consume tokens Read more
Source§

fn try_consume_one(&mut self) -> LimiterResult

Try to consume a single token Read more

Auto Trait Implementations§

§

impl<T> Freeze for TokenBucket<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for TokenBucket<T>
where T: RefUnwindSafe,

§

impl<T> Send for TokenBucket<T>
where T: Send,

§

impl<T> Sync for TokenBucket<T>
where T: Sync,

§

impl<T> Unpin for TokenBucket<T>
where T: Unpin,

§

impl<T> UnwindSafe for TokenBucket<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.