Struct GCRA

Source
pub struct GCRA<P: Reference = <DefaultClock as Clock>::Instant> { /* 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.

§A note about batch decisions

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 = DirectRateLimiter::<GCRA>::per_second(nonzero!(20u32));
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));

Trait Implementations§

Source§

impl<P: Reference> Algorithm<P> for GCRA<P>

Source§

fn test_and_update( &self, state: &Self::BucketState, t0: P, ) -> Result<(), Self::NegativeDecision>

Tests if a single cell can be accommodated by the rate-limiter and updates the state, if so.

Source§

fn test_n_and_update( &self, state: &Self::BucketState, n: u32, t0: P, ) -> Result<(), NegativeMultiDecision<Self::NegativeDecision>>

Tests if n cells can be accommodated by the rate-limiter and updates rate limiter state iff they can be.

As this method is an extension of GCRA (using multiplication), it is likely not as fast (and not as obviously “right”) as the single-cell variant.

Source§

type BucketState = State<P>

The state of a single rate limiting bucket. Read more
Source§

type NegativeDecision = NotUntil<P>

The type returned when a rate limiting decision for a single cell is negative. Each rate limiting algorithm can decide to return the type that suits it best, but most algorithms’ decisions also implement NonConformance, to ease handling of how long to wait.
Source§

fn construct( capacity: NonZeroU32, cell_weight: NonZeroU32, per_time_unit: Duration, ) -> Result<Self, InconsistentCapacity>

Constructs a rate limiter with the given parameters: capacity is the number of cells to allow, weighing cell_weight, every per_time_unit.
Source§

impl<P: Clone + Reference> Clone for GCRA<P>

Source§

fn clone(&self) -> GCRA<P>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P: Debug + Reference> Debug for GCRA<P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<P: Reference> RateLimitState<GCRA<P>, P> for State<P>

Source§

fn last_touched(&self, params: &GCRA<P>) -> Option<P>

Returns the last time instant that the state had any relevance (i.e. the rate limiter would behave exactly as if it was a new rate limiter after this time). Read more

Auto Trait Implementations§

§

impl<P> Freeze for GCRA<P>

§

impl<P> RefUnwindSafe for GCRA<P>
where P: RefUnwindSafe,

§

impl<P> Send for GCRA<P>

§

impl<P> Sync for GCRA<P>

§

impl<P> Unpin for GCRA<P>
where P: Unpin,

§

impl<P> UnwindSafe for GCRA<P>
where P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.