pub struct TokenBucket { /* private fields */ }Expand description
Token bucket for rate limiting
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn refill_rate(&self) -> u64
pub fn refill_rate(&self) -> u64
Get refill rate (tokens per second)
Sourcepub fn consume(&mut self, requested: u64) -> u64
pub fn consume(&mut self, requested: u64) -> u64
Try to consume tokens Returns the number of tokens actually consumed
Sourcepub fn try_consume(&mut self, amount: u64) -> bool
pub fn try_consume(&mut self, amount: u64) -> bool
Try to consume exactly the requested amount Returns true if successful, false if not enough tokens
Sourcepub fn wait_time(&mut self, amount: u64) -> Duration
pub fn wait_time(&mut self, amount: u64) -> Duration
Wait until enough tokens are available (simulated) Returns the duration to wait
Sourcepub fn set_refill_rate(&mut self, rate: u64)
pub fn set_refill_rate(&mut self, rate: u64)
Update refill rate
Sourcepub fn set_capacity(&mut self, capacity: u64)
pub fn set_capacity(&mut self, capacity: u64)
Update capacity
Sourcepub fn has_tokens(&mut self, amount: u64) -> bool
pub fn has_tokens(&mut self, amount: u64) -> bool
Check if bucket has enough tokens
Sourcepub fn fill_level(&mut self) -> f64
pub fn fill_level(&mut self) -> f64
Get fill percentage (0.0 to 1.0)
Trait Implementations§
Source§impl Clone for TokenBucket
impl Clone for TokenBucket
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