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
impl TokenBucket
Sourcepub fn new(capacity: u32, refill_per_second: f64) -> Self
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.
Sourcepub fn refill_per_second(&self) -> f64
pub fn refill_per_second(&self) -> f64
Refill rate in tokens per second (reconstructed from internal fixed-point storage; subject to small rounding).
Sourcepub fn available(&self) -> u32
pub fn available(&self) -> u32
Current available tokens (approximate snapshot — observation has no retry semantics; treat as advisory).
Sourcepub fn try_acquire(&self, n: u32) -> Result<()>
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.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for TokenBucket
impl RefUnwindSafe for TokenBucket
impl Send for TokenBucket
impl Sync for TokenBucket
impl Unpin for TokenBucket
impl UnsafeUnpin for TokenBucket
impl UnwindSafe for TokenBucket
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