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.
impl TokenBucketCell
Lock-free atomic token-bucket operations on TokenBucketCell.
Sourcepub fn full(config: &TokenBucketConfig) -> Self
pub fn full(config: &TokenBucketConfig) -> Self
Creates a TokenBucketCell starting full according to config.
§Arguments
&TokenBucketConfig- 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 TokenBucketCell starting with initial_tokens clamped to config.get_capacity().
§Arguments
&TokenBucketConfig- Token-bucket configuration.u64- Starting number of available tokens.
Sourcepub fn reset_at_ns(&self, config: &TokenBucketConfig, now_ns: u64)
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.
Sourcepub fn is_idle_at_ns(&self, config: &TokenBucketConfig, now_ns: u64) -> bool
pub fn is_idle_at_ns(&self, config: &TokenBucketConfig, now_ns: u64) -> bool
Sourcepub fn peek_at_ns(
&self,
config: &TokenBucketConfig,
now_ns: u64,
) -> RateLimitDecision
pub fn peek_at_ns( &self, config: &TokenBucketConfig, now_ns: u64, ) -> RateLimitDecision
Sourcepub fn check_n_at_ns(
&self,
config: &TokenBucketConfig,
now_ns: u64,
cost: u64,
) -> RateLimitDecision
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.
Sourcepub fn refund_at_ns(
&self,
config: &TokenBucketConfig,
now_ns: u64,
tokens: u64,
) -> u64
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§
Auto Trait Implementations§
impl !Freeze for TokenBucketCell
impl Forget for TokenBucketCell
impl Move for TokenBucketCell
impl RefUnwindSafe for TokenBucketCell
impl Send for TokenBucketCell
impl Sync for TokenBucketCell
impl Unpin for TokenBucketCell
impl UnsafeUnpin for TokenBucketCellwhere
Align8<u64>: UnsafeUnpin,
impl UnwindSafe for TokenBucketCellwhere
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