Struct ratelimit_meter::algorithms::leaky_bucket::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 = DirectRateLimiter::<LeakyBucket>::per_second(nonzero!(2u32));
assert_eq!(Ok(()), lb.check());
Trait Implementations
sourceimpl Algorithm for LeakyBucket
impl Algorithm for LeakyBucket
type BucketState = State
type BucketState = State
type NegativeDecision = TooEarly
type NegativeDecision = TooEarly
NonConformance
, to ease
handling of how long to wait. Read moresourcefn construct(
capacity: NonZeroU32,
cell_weight: NonZeroU32,
per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
fn construct(
capacity: NonZeroU32,
cell_weight: NonZeroU32,
per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
capacity
is the number of cells to allow, weighing
cell_weight
, every per_time_unit
. Read moresourcefn test_n_and_update(
&self,
state: &Self::BucketState,
n: u32,
t0: Instant
) -> Result<(), NegativeMultiDecision<TooEarly>>
fn test_n_and_update(
&self,
state: &Self::BucketState,
n: u32,
t0: Instant
) -> Result<(), NegativeMultiDecision<TooEarly>>
n
cells can be accommodated in the rate limiter at
the instant at
and updates the rate-limiter state to account
for the weight of the cells and updates the ratelimiter state. Read moresourcefn test_and_update(
&self,
state: &Self::BucketState,
at: Instant
) -> Result<(), Self::NegativeDecision>
fn test_and_update(
&self,
state: &Self::BucketState,
at: Instant
) -> Result<(), Self::NegativeDecision>
at
and updates the rate-limiter state to
account for the weight of the cell. Read moresourceimpl 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 more