Skip to main content

TokenBucket

Struct TokenBucket 

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

Strict-admission token bucket with atomic-CAS acquire semantics.

Cache-line aligned to prevent false sharing in concurrent admission pipelines.

Implementations§

Source§

impl TokenBucket

Source

pub fn new(capacity: u32, refill_per_second: f64) -> Self

Build a new bucket.

  • capacity — maximum tokens the bucket can hold (burst size).
  • refill_per_second — sustained refill rate, tokens per second.

The bucket starts full (tokens = capacity). refill_per_second of 0.0 produces a static-capacity bucket (no refill); negative or non-finite values are coerced to 0.0.

Source

pub fn capacity(&self) -> u32

Bucket capacity in whole tokens.

Source

pub fn refill_per_second(&self) -> f64

Refill rate in tokens per second (reconstructed from internal fixed-point storage; subject to small rounding).

Source

pub fn available(&self) -> u32

Current available tokens (approximate snapshot — observation has no retry semantics; treat as advisory).

Source

pub fn try_acquire(&self, n: u32) -> Result<()>

Attempt to acquire n tokens. Returns Ok(()) on success, or Err(MetricsError::WouldBlock) when fewer than n tokens are available even after refill.

n == 0 always succeeds without modifying state.

Source

pub fn acquire(&self, n: u32) -> bool

Acquire-or-don’t variant that swallows the error and returns a bool.

Source

pub fn reset(&self)

Reset the bucket to full capacity.

Trait Implementations§

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