pub struct TokenBucket { /* private fields */ }
Expand description
TokenBucket provides a lower level interface to rate limiting with a configurable capacity, refill-rate and initial burst.
Implementations§
Source§impl TokenBucket
impl TokenBucket
Sourcepub fn new(size: u64, one_time_burst: u64, complete_refill_time_ms: u64) -> Self
pub fn new(size: u64, one_time_burst: u64, complete_refill_time_ms: u64) -> Self
Creates a TokenBucket
wrapped in an Option
.
TokenBucket created is of size
total capacity and takes complete_refill_time_ms
milliseconds to go from zero tokens to total capacity. The one_time_burst
is initial
extra credit on top of total capacity, that does not replenish and which can be used
for an initial burst of data.
If the size
or the complete refill time
are zero, then None
is returned.
Sourcepub fn reduce(&mut self, tokens: u64) -> BucketReduction
pub fn reduce(&mut self, tokens: u64) -> BucketReduction
Attempts to consume tokens
from the bucket and returns whether the action succeeded.
Sourcepub fn force_replenish(&mut self, tokens: u64)
pub fn force_replenish(&mut self, tokens: u64)
“Manually” adds tokens to bucket.
Sourcepub fn one_time_burst(&self) -> u64
pub fn one_time_burst(&self) -> u64
Returns the remaining one time burst budget.
Sourcepub fn refill_time_ms(&self) -> u64
pub fn refill_time_ms(&self) -> u64
Returns the time in milliseconds required to to completely fill the bucket.
Sourcepub fn budget(&self) -> u64
pub fn budget(&self) -> u64
Returns the current budget (one time burst allowance notwithstanding).
Sourcepub fn initial_one_time_burst(&self) -> u64
pub fn initial_one_time_burst(&self) -> u64
Returns the initially configured one time burst budget.
Trait Implementations§
Source§impl Clone for TokenBucket
impl Clone for TokenBucket
Source§fn clone(&self) -> TokenBucket
fn clone(&self) -> TokenBucket
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more