Skip to main content

TokenBucket

Struct TokenBucket 

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

A sans-IO token bucket: spend units at a bounded Rate with a configurable burst capacity.

The bucket never reads a clock: the caller provides now as monotonic nanoseconds from any epoch, as long as it is consistent across calls. This keeps the bucket no_std, deterministic to test and embeddable in other sans-IO state machines.

Internally the level is stored scaled by the period (in nanoseconds), so refills are exact integer arithmetic with zero drift.

§Examples

use rama_utils::rate::{Acquire, Rate, TokenBucket};

let mut bucket = TokenBucket::from_rate(Rate::per_sec(2));
assert_eq!(bucket.try_acquire(0, 1), Acquire::Granted);
assert_eq!(bucket.try_acquire(0, 1), Acquire::Granted);
// bucket is empty: the next unit refills after half a period
assert_eq!(bucket.try_acquire(0, 1), Acquire::RetryAt(500_000_000));
assert_eq!(bucket.try_acquire(500_000_000, 1), Acquire::Granted);

Implementations§

Source§

impl TokenBucket

Source

pub fn new(rate: Rate, burst: u64) -> Self

Create a new TokenBucket with the given Rate and burst capacity (the maximum units spendable at once).

The bucket starts full.

§Panics

Panics if burst is zero.

Source

pub fn from_rate(rate: Rate) -> Self

Create a new TokenBucket with a burst capacity of one period worth of units (rate.units()): a plain smooth rate.

Source

pub const fn rate(&self) -> Rate

The configured Rate.

Source

pub const fn burst(&self) -> u64

The configured burst capacity.

Source

pub fn try_acquire(&mut self, now: u64, n: u64) -> Acquire

Try to spend n units at monotonic instant now (nanoseconds, caller’s clock).

  • Acquire::Granted: the units were spent.
  • Acquire::RetryAt: nothing was spent; retrying with the returned instant (and an otherwise untouched bucket) is guaranteed to be granted.
  • Acquire::Never: n exceeds the burst capacity and can never be granted as a single acquisition.

n == 0 is always granted. A now earlier than a previously provided instant is treated as no time having passed.

Source

pub fn refund(&mut self, n: u64)

Give back up to n previously spent units, saturating at the burst capacity.

Intended for callers that reserve budget upfront and end up using less (e.g. a short write).

Source

pub fn available(&mut self, now: u64) -> u64

The number of whole units available at monotonic instant now (nanoseconds, caller’s clock).

Trait Implementations§

Source§

impl Clone for TokenBucket

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TokenBucket

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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<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