Skip to main content

TokenBucketCell

Struct TokenBucketCell 

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

Lock-free single-AtomicU64 token-bucket state cell packing (last_step, tokens).

Implementations§

Source§

impl TokenBucketCell

Lock-free atomic token-bucket operations on TokenBucketCell.

Source

pub fn full(config: &TokenBucketConfig) -> Self

Creates a TokenBucketCell starting full according to config.

§Arguments
  • &TokenBucketConfig - Token-bucket configuration.
Source

pub fn with_initial_tokens( config: &TokenBucketConfig, initial_tokens: u64, ) -> Self

Creates a TokenBucketCell starting with initial_tokens clamped to config.get_capacity().

§Arguments
  • &TokenBucketConfig - Token-bucket configuration.
  • u64 - Starting number of available tokens.
Source

pub fn reset_at_ns(&self, config: &TokenBucketConfig, now_ns: u64)

Resets the bucket to full capacity anchored at now_ns.

§Arguments
  • &Self - The token-bucket cell.
  • &TokenBucketConfig - Token-bucket configuration.
  • u64 - Current monotonic timestamp in nanoseconds.
Source

pub fn is_idle_at_ns(&self, config: &TokenBucketConfig, now_ns: u64) -> bool

Returns whether the bucket is completely full at now_ns.

§Arguments
  • &Self - The token-bucket cell.
  • &TokenBucketConfig - Token-bucket configuration.
  • u64 - Current monotonic timestamp in nanoseconds.
§Returns
  • bool - true when available tokens equal configured capacity.
Source

pub fn peek_at_ns( &self, config: &TokenBucketConfig, now_ns: u64, ) -> RateLimitDecision

Inspects current token availability at now_ns without consuming tokens.

§Arguments
  • &Self - The token-bucket cell.
  • &TokenBucketConfig - Token-bucket configuration.
  • u64 - Current monotonic timestamp in nanoseconds.
§Returns
  • RateLimitDecision - Non-mutating availability snapshot.
Source

pub fn check_n_at_ns( &self, config: &TokenBucketConfig, now_ns: u64, cost: u64, ) -> RateLimitDecision

Atomically checks and consumes cost tokens at monotonic timestamp now_ns.

§Arguments
  • &Self - The token-bucket cell.
  • &TokenBucketConfig - Token-bucket configuration.
  • u64 - Current monotonic timestamp in nanoseconds.
  • u64 - Number of tokens to consume.
§Returns
  • RateLimitDecision - Admission decision and nanosecond timings.
Source

pub fn refund_at_ns( &self, config: &TokenBucketConfig, now_ns: u64, tokens: u64, ) -> u64

Atomically adds up to tokens back into the bucket, saturating at capacity.

§Arguments
  • &Self - The token-bucket cell.
  • &TokenBucketConfig - Token-bucket configuration.
  • u64 - Current monotonic timestamp in nanoseconds.
  • u64 - Number of tokens to refund.
§Returns
  • u64 - Available token count after refunding.

Trait Implementations§

Source§

impl Debug for TokenBucketCell

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

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.