Skip to main content

TokenBucketLimiter

Struct TokenBucketLimiter 

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

Lock-free atomic token-bucket rate limiter with an embedded monotonic clock.

Implementations§

Source§

impl TokenBucketLimiter

Core lock-free operations for TokenBucketLimiter.

Source

pub fn new(config: TokenBucketConfig) -> Self

Creates a new full TokenBucketLimiter from a validated TokenBucketConfig.

§Arguments
  • TokenBucketConfig - Validated token-bucket configuration.
Source

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

Creates a TokenBucketLimiter starting with initial_tokens clamped to capacity.

§Arguments
  • TokenBucketConfig - Validated token-bucket configuration.
  • u64 - Initial token count.
Source

pub fn with_clock(config: TokenBucketConfig, clock: MonotonicClock) -> Self

Creates a TokenBucketLimiter anchored at a caller-supplied MonotonicClock.

§Arguments
  • TokenBucketConfig - Validated token-bucket configuration.
  • MonotonicClock - Monotonic clock origin.
Source

pub fn per_second( capacity: u64, tokens_per_sec: u64, ) -> Result<Self, RateLimitError>

Creates a full TokenBucketLimiter refilling tokens_per_sec tokens every second.

§Arguments
  • u64 - Maximum bucket capacity (1..=u32::MAX).
  • u64 - Tokens added per second (>= 1).
§Returns
  • Result<Self, RateLimitError> - Constructed limiter or configuration error.
Source

pub fn get_config(&self) -> &TokenBucketConfig

Returns the token-bucket configuration.

§Arguments
  • &Self - The limiter instance.
§Returns
  • &TokenBucketConfig - Reference to the configuration.
Source

pub fn get_clock(&self) -> &MonotonicClock

Returns the embedded monotonic clock.

§Arguments
  • &Self - The limiter instance.
§Returns
  • &MonotonicClock - Reference to the clock.
Source

pub fn check(&self) -> RateLimitDecision

Checks and consumes 1 token using the embedded monotonic clock.

§Arguments
  • &Self - The limiter instance.
§Returns
  • RateLimitDecision - Admission decision and nanosecond timings.
Source

pub fn check_n(&self, cost: u64) -> RateLimitDecision

Checks and consumes cost tokens using the embedded monotonic clock.

§Arguments
  • &Self - The limiter instance.
  • u64 - Number of tokens to consume.
§Returns
  • RateLimitDecision - Admission decision and nanosecond timings.
Source

pub fn check_at_ns(&self, now_ns: u64) -> RateLimitDecision

Checks and consumes 1 token at an explicit monotonic timestamp in nanoseconds.

§Arguments
  • &Self - The limiter instance.
  • u64 - Monotonic timestamp in nanoseconds.
§Returns
  • RateLimitDecision - Admission decision and nanosecond timings.
Source

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

Checks and consumes cost tokens at an explicit monotonic timestamp in nanoseconds.

§Arguments
  • &Self - The limiter instance.
  • u64 - Monotonic timestamp in nanoseconds.
  • u64 - Number of tokens to consume.
§Returns
  • RateLimitDecision - Admission decision and nanosecond timings.
Source

pub fn peek(&self) -> RateLimitDecision

Inspects current availability without consuming tokens.

§Arguments
  • &Self - The limiter instance.
§Returns
  • RateLimitDecision - Non-mutating availability snapshot.
Source

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

Inspects availability at an explicit monotonic timestamp without consuming tokens.

§Arguments
  • &Self - The limiter instance.
  • u64 - Monotonic timestamp in nanoseconds.
§Returns
  • RateLimitDecision - Non-mutating availability snapshot.
Source

pub fn refund(&self, tokens: u64) -> u64

Refunds tokens back into the bucket using the embedded monotonic clock.

§Arguments
  • &Self - The limiter instance.
  • u64 - Number of tokens to add back.
§Returns
  • u64 - Available token count after refunding.
Source

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

Refunds tokens back into the bucket at an explicit monotonic timestamp.

§Arguments
  • &Self - The limiter instance.
  • u64 - Monotonic timestamp in nanoseconds.
  • u64 - Number of tokens to add back.
§Returns
  • u64 - Available token count after refunding.
Source

pub fn reset(&self)

Resets the bucket back to full capacity at the current monotonic time.

§Arguments
  • &Self - The limiter instance.
Source

pub fn reset_at_ns(&self, now_ns: u64)

Resets the bucket back to full capacity at an explicit monotonic timestamp.

§Arguments
  • &Self - The limiter instance.
  • u64 - Monotonic timestamp in nanoseconds.

Trait Implementations§

Source§

impl Debug for TokenBucketLimiter

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.