Skip to main content

TokenBucket

Struct TokenBucket 

Source
pub struct TokenBucket<I> { /* private fields */ }
Expand description

A token bucket.

Calculations are performed at microsecond resolution. You likely want to call refill() each time you want to access or perform an operation on the token bucket.

This is partially inspired by tor’s token_bucket_ctr_t, but the implementation is quite a bit different. We use larger values here (for example u64), and we aim to avoid drift when refills occur at times that aren’t exactly in period with the refill rate.

It’s possible that we could relax these requirements to reduce memory usage and computation complexity, but that optimization should probably only be made if/when needed since it would make the code more difficult to reason about, and possibly more complex.

Implementations§

Source§

impl<I: TokenBucketInstant> TokenBucket<I>

Source

pub fn new(config: &TokenBucketConfig, now: I) -> Self

A new TokenBucket with a given rate in tokens/second and a max token limit.

The bucket will initially be full. The value max is commonly referred to as the “burst”.

Source

pub fn is_empty(&self) -> bool

Are there no tokens in the bucket?

Source

pub fn max(&self) -> u64

The maximum number of tokens that this bucket can hold.

Source

pub fn drain( &mut self, count: u64, ) -> Result<BecameEmpty, InsufficientTokensError>

Remove count tokens from the bucket.

Source

pub fn claim( &mut self, count: u64, ) -> Result<ClaimedTokens<'_, I>, InsufficientTokensError>

Claim a number of tokens.

The claim will be held by the returned ClaimedTokens, and committed when dropped.

Note: You probably want to call refill() before this.

Source

pub fn adjust(&mut self, now: I, config: &TokenBucketConfig)

Adjust the refill rate and max tokens of the bucket.

The token bucket is refilled up to now before changing the rate.

If the new max is smaller than the existing number of tokens, the number of tokens will be reduced to the new max.

A rate and/or max of 0 is allowed.

Source

pub fn tokens_available_at( &self, tokens: u64, ) -> Result<I, NeverEnoughTokensError>

An estimated time at which the bucket will have tokens available.

It is not guaranteed that tokens will be available at the returned time.

If there are already enough tokens available, a time in the past may be returned.

A value of None implies “never”, for example if the refill rate is 0, the bucket max is too small, or the time is too large to be represented as an I.

Source

pub fn refill(&mut self, now: I) -> BecameNonEmpty

Refill the bucket.

Trait Implementations§

Source§

impl<I: Debug> Debug for TokenBucket<I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> UnsafeUnpin for TokenBucket<I>
where I: UnsafeUnpin,

§

impl<I> UnwindSafe for TokenBucket<I>
where I: 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more