Struct ratelimit_meter::GCRA
source · pub struct GCRA { /* private fields */ }
Expand description
Implements the virtual scheduling description of the Generic Cell Rate Algorithm, attributed to ITU-T in recommendation I.371 Traffic control and congestion control in B-ISDN; from Wikipedia.
While algorithms like leaky-bucket rate limiters allow cells to be distributed across time in any way, GCRA is a rate-limiting and traffic-shaping algorithm. It mandates that a minimum amount of time passes between cells being measured. For example, if your API mandates that only 20 requests can be made per second, GCRA will ensure that each request is at least 50ms apart from the previous request. This makes GCRA suitable for shaping traffic in networking and telecom equipment (it was initially made for asynchronous transfer mode networks), or for outgoing workloads on consumers of attention, e.g. distributing outgoing emails across a day.
In a blatant side-stepping of the above traffic-shaping criteria,
this implementation of GCRA comes with an extension that allows
measuring multiple cells at once, assuming that if a pause of
n*(the minimum time between cells)
has passed, we can allow a
single big batch of n
cells through. This assumption may not be
correct for your application, but if you depend on GCRA’s
traffic-shaping properties, it’s better to not use the _n
suffixed check functions.
Example
In this example, we construct a rate-limiter with the GCR algorithm that can accommodate 20 cells per second. This translates to the GCRA parameters τ=1s, T=50ms (that’s 1s / 20 cells).
let mut limiter = per_second::<GCRA>(NonZeroU32::new(20).unwrap());
let now = Instant::now();
let ms = Duration::from_millis(1);
assert_eq!(Ok(()), limiter.check_at(now)); // the first cell is free
for i in 0..20 {
// Spam a lot:
assert!(limiter.check_at(now).is_ok(), "at {}", i);
}
// We have exceeded the bucket capacity:
assert!(limiter.check_at(now).is_err());
// After a sufficient time period, cells are allowed again:
assert_eq!(Ok(()), limiter.check_at(now + ms*50));
Implementations
sourceimpl GCRA
impl GCRA
sourcepub fn with_parameters<T: Into<Option<Instant>>>(
t: Duration,
tau: Duration,
tat: T
) -> GCRA
pub fn with_parameters<T: Into<Option<Instant>>>(
t: Duration,
tau: Duration,
tat: T
) -> GCRA
Constructs a GCRA rate-limiter with the parameters T (the minimum amount of time that single cells are spaced apart), tau (τ, the number of cells that fit into this buffer), and an optional t_at (the earliest instant that the rate-limiter would accept another cell).
Trait Implementations
sourceimpl Decider for GCRA
impl Decider for GCRA
sourceimpl From<(Duration, Duration, Option<Instant>)> for GCRA
impl From<(Duration, Duration, Option<Instant>)> for GCRA
Allows converting the parameters returned from
[Into<(Duration, Duration, Option<Instant>)>
](#impl-Into<(Duration, Duration, Option
sourceimpl<'a> Into<(Duration, Duration, Option<Instant>)> for &'a GCRA
impl<'a> Into<(Duration, Duration, Option<Instant>)> for &'a GCRA
Allows converting a GCRA into a tuple containing its T (the
minimum amount of time that single cells are spaced apart), tau
(τ, the number of cells that fit into this buffer), and an
optional t_at
(the earliest instant that the rate-limiter would
accept another cell).
These parameters can be used with
.with_parameters
to persist and
construct a copy of the GCRA rate-limiter.
sourceimpl MultiDecider for GCRA
impl MultiDecider for GCRA
This crate’s GCRA implementation also allows checking multiple
cells at once, assuming that (counter the traffic-shaping
properties of GCRA) if a sufficiently long pause (n*t
) has
occurred between cells, the algorithm can accommodate n
cells.
As this assumption does not necessarily hold in all circumstances, users of this trait on GCRA limiters should ensure that this is ok.
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