pub struct TokenBucket<T>{ /* private fields */ }Expand description
Token bucket -type rate limiter
A token bucket limiter can be illustrated as a being filled with tokens at a constant rate, while consumes will remove tokens from the bucket.
This leads to a soft limiter where occasional burstiness is allowed since as long as the bucket holds tokens those can be consumed at an unlimited rate. Ultimately the bucket size is what defined the burstiness.
Implementations§
Source§impl<T> TokenBucket<T>
impl<T> TokenBucket<T>
Sourcepub fn new_with_time_provider(
rate_per_s: u64,
capacity: u64,
time_provider: T,
) -> Self
pub fn new_with_time_provider( rate_per_s: u64, capacity: u64, time_provider: T, ) -> Self
Initialize a new token bucket utilizing the given timer
§Arguments
rate_per_sec- how many consumes should be allowed per second on averagecapacity- bucket capacity to dictate the burstiness of this limitertime_provider_t- closure that returns a monotonically nondecreasing timestamp asDurationfrom some fixed epoch in the past
If you are developing for a std target, you probably wish to use token_bucket
Trait Implementations§
Source§impl<T> Limiter for TokenBucket<T>
impl<T> Limiter for TokenBucket<T>
Source§fn try_consume(&mut self, tokens: u64) -> LimiterResult
fn try_consume(&mut self, tokens: u64) -> LimiterResult
Try to consume tokens Read more
Source§fn try_consume_one(&mut self) -> LimiterResult
fn try_consume_one(&mut self) -> LimiterResult
Try to consume a single token Read more
Auto Trait Implementations§
impl<T> Freeze for TokenBucket<T>where
T: Freeze,
impl<T> RefUnwindSafe for TokenBucket<T>where
T: RefUnwindSafe,
impl<T> Send for TokenBucket<T>where
T: Send,
impl<T> Sync for TokenBucket<T>where
T: Sync,
impl<T> Unpin for TokenBucket<T>where
T: Unpin,
impl<T> UnwindSafe for TokenBucket<T>where
T: 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