Struct ratelimit_meter::LeakyBucket
source · pub struct LeakyBucket { /* private fields */ }
Expand description
Implements the industry-standard leaky bucket rate-limiting as-a-meter. The bucket keeps a “fill height”, pretending to drip steadily (which reduces the fill height), and increases the fill height with every cell that is found conforming. If cells would make the bucket overflow, they count as non-conforming.
Drip implementation
Instead of having a background task update the bucket’s fill
level, this implementation re-computes the fill level of the
bucket on every call to check
and related
methods.
Wait time calculation
If the cell does not fit, this implementation computes the minimum wait time until the cell can be accommodated. This minimum wait time does not account for thundering herd effects or other problems in concurrent resource acquisition, so users of this library must take care to apply positive jitter to these wait times.
Example
let mut lb: LeakyBucket = LeakyBucket::per_second(NonZeroU32::new(2).unwrap());
assert_eq!(Ok(()), lb.check());
Implementations
sourceimpl LeakyBucket
impl LeakyBucket
sourcepub fn new(capacity: NonZeroU32, per_duration: Duration) -> LeakyBucket
pub fn new(capacity: NonZeroU32, per_duration: Duration) -> LeakyBucket
Constructs and returns a leaky-bucket rate-limiter allowing as many cells on average as the given capacity per time duration.
Example
let now = Instant::now();
let day = Duration::from_secs(86400);
let mut lb = LeakyBucket::new(NonZeroU32::new(1).unwrap(), day); // 1 per day
assert!(lb.check_at(now).is_ok());
assert!(!lb.check_at(now + day/2).is_ok()); // Can't do it half a day later
assert!(lb.check_at(now + day).is_ok()); // Have to wait a day
// ...and then, a day after that.
assert!(lb.check_at(now + day * 2).is_ok());
sourcepub fn per_second(capacity: NonZeroU32) -> LeakyBucket
pub fn per_second(capacity: NonZeroU32) -> LeakyBucket
Constructs and returns a leaky-bucket rate-limiter allowing on
average capacity
/1s cells.
Trait Implementations
sourceimpl Clone for LeakyBucket
impl Clone for LeakyBucket
sourcefn clone(&self) -> LeakyBucket
fn clone(&self) -> LeakyBucket
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl Debug for LeakyBucket
impl Debug for LeakyBucket
sourceimpl Decider for LeakyBucket
impl Decider for LeakyBucket
sourceimpl MultiDecider for LeakyBucket
impl MultiDecider for LeakyBucket
sourcefn check_n_at(&mut self, n: u32, at: Instant) -> Result<(), NegativeMultiDecision>
fn check_n_at(&mut self, n: u32, at: Instant) -> Result<(), NegativeMultiDecision>
n
cells can be accommodated at the given time
stamp. If (and only if) all cells in the batch can be
accomodated, the MultiDecider
updates the internal state to
account for all cells and returns Ok(())
. Read moresourcefn check_n(&mut self, n: u32) -> Result<(), NegativeMultiDecision>
fn check_n(&mut self, n: u32) -> Result<(), NegativeMultiDecision>
n
cells can be accommodated at the current time
(Instant::now()
), using check_n_at
Read more