Struct diem_rate_limiter::rate_limit::Bucket
source · pub struct Bucket { /* private fields */ }
Expand description
A token bucket object that keeps track of everything related to a key
This can be used as a standalone rate limiter; however, to make it more useful
it should be wrapped in an Arc
and a Mutex
to be shared across threads.
Implementations§
source§impl Bucket
impl Bucket
pub fn new( label: String, log_info: String, key: String, initial: usize, size: usize, rate: usize, metrics: Option<HistogramVec> ) -> Self
sourcepub fn open(label: String) -> Self
pub fn open(label: String) -> Self
A fully open rate limiter, to allow for ignoring rate limiting for tests
sourcepub fn acquire_all_tokens(
&mut self,
requested: usize
) -> Result<(), Option<Instant>>
pub fn acquire_all_tokens( &mut self, requested: usize ) -> Result<(), Option<Instant>>
Determine if an entire batch can be passed through
This is important for message based rate limiting, where the whole message has
to make it through, or else it must be rejected. A result of None
means it cannot
ever be allowed through, as it’s bigger than the size of the bucket.
sourcepub fn acquire_tokens(&mut self, requested: usize) -> Result<usize, Instant>
pub fn acquire_tokens(&mut self, requested: usize) -> Result<usize, Instant>
Returns usize
of tokens allowed. May be less than requested.
For best effort, caller should return unused tokens with add_tokens
sourcepub fn time_of_next_refill(&self) -> Instant
pub fn time_of_next_refill(&self) -> Instant
Tells us when the next refill is
sourcepub fn time_of_tokens_needed(&self, requested: usize) -> Option<Instant>
pub fn time_of_tokens_needed(&self, requested: usize) -> Option<Instant>
Tells us when an entire batch will make it through. Useful for Async work to wait until
all tokens are ready. Returns None
if it is never possible.
sourcepub fn return_tokens(&mut self, new_tokens: usize)
pub fn return_tokens(&mut self, new_tokens: usize)
Returns tokens that were unused