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.
impl TokenBucketLimiter
Core lock-free operations for TokenBucketLimiter.
Sourcepub fn new(config: TokenBucketConfig) -> Self
pub fn new(config: TokenBucketConfig) -> Self
Creates a new full TokenBucketLimiter from a validated TokenBucketConfig.
§Arguments
TokenBucketConfig- Validated token-bucket configuration.
Sourcepub fn with_initial_tokens(
config: TokenBucketConfig,
initial_tokens: u64,
) -> Self
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.
Sourcepub fn with_clock(config: TokenBucketConfig, clock: MonotonicClock) -> Self
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.
Sourcepub fn per_second(
capacity: u64,
tokens_per_sec: u64,
) -> Result<Self, RateLimitError>
pub fn per_second( capacity: u64, tokens_per_sec: u64, ) -> Result<Self, RateLimitError>
Sourcepub fn get_config(&self) -> &TokenBucketConfig
pub fn get_config(&self) -> &TokenBucketConfig
Sourcepub fn get_clock(&self) -> &MonotonicClock
pub fn get_clock(&self) -> &MonotonicClock
Sourcepub fn check(&self) -> RateLimitDecision
pub fn check(&self) -> RateLimitDecision
Sourcepub fn check_n(&self, cost: u64) -> RateLimitDecision
pub fn check_n(&self, cost: u64) -> RateLimitDecision
Sourcepub fn check_at_ns(&self, now_ns: u64) -> RateLimitDecision
pub fn check_at_ns(&self, now_ns: u64) -> RateLimitDecision
Sourcepub fn check_n_at_ns(&self, now_ns: u64, cost: u64) -> RateLimitDecision
pub fn check_n_at_ns(&self, now_ns: u64, cost: u64) -> RateLimitDecision
Sourcepub fn peek(&self) -> RateLimitDecision
pub fn peek(&self) -> RateLimitDecision
Sourcepub fn peek_at_ns(&self, now_ns: u64) -> RateLimitDecision
pub fn peek_at_ns(&self, now_ns: u64) -> RateLimitDecision
Sourcepub fn refund_at_ns(&self, now_ns: u64, tokens: u64) -> u64
pub fn refund_at_ns(&self, now_ns: u64, tokens: u64) -> u64
Sourcepub fn reset(&self)
pub fn reset(&self)
Resets the bucket back to full capacity at the current monotonic time.
§Arguments
&Self- The limiter instance.
Sourcepub fn reset_at_ns(&self, now_ns: u64)
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§
Auto Trait Implementations§
impl !Freeze for TokenBucketLimiter
impl Forget for TokenBucketLimiter
impl Move for TokenBucketLimiter
impl RefUnwindSafe for TokenBucketLimiter
impl Send for TokenBucketLimiter
impl Sync for TokenBucketLimiter
impl Unpin for TokenBucketLimiter
impl UnsafeUnpin for TokenBucketLimiterwhere
Align8<u64>: UnsafeUnpin,
impl UnwindSafe for TokenBucketLimiterwhere
Align8<u64>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more