Struct http_rate::Quota

source ·
pub struct Quota { /* private fields */ }
Expand description

A rate-limiting quota.

Quotas are expressed in a positive number of “cells” (the maximum number of positive decisions / allowed items until the rate limiter needs to replenish) and the amount of time for the rate limiter to replenish a single cell.

Neither the number of cells nor the replenishment unit of time may be zero.

Burst sizes

There are multiple ways of expressing the same quota: a quota given as Quota::per_second(1) allows, on average, the same number of cells through as a quota given as Quota::per_minute(60). However, the quota of Quota::per_minute(60) has a burst size of 60 cells, meaning it is possible to accommodate 60 cells in one go, after which the equivalent of a minute of inactivity is required for the burst allowance to be fully restored.

Burst size gets really important when you construct a rate limiter that should allow multiple elements through at one time (using RateLimiter.check_n and its related functions): Only at most as many cells can be let through in one call as are given as the burst size.

In other words, the burst size is the maximum number of cells that the rate limiter will ever allow through without replenishing them.

Implementations§

source§

impl Quota

source

pub fn per_second<B>(max_burst: B) -> Self
where B: TryInto<NonZeroU32>, B::Error: Debug,

Construct a quota for a number of cells per second. The given number of cells is also assumed to be the maximum burst size.

Panics
  • When max_burst is zero.
source

pub fn per_minute<B>(max_burst: B) -> Self
where B: TryInto<NonZeroU32>, B::Error: Debug,

Construct a quota for a number of cells per 60-second period. The given number of cells is also assumed to be the maximum burst size.

Panics
  • When max_burst is zero.
source

pub fn per_hour<B>(max_burst: B) -> Self
where B: TryInto<NonZeroU32>, B::Error: Debug,

Construct a quota for a number of cells per 60-minute (3600-second) period. The given number of cells is also assumed to be the maximum burst size.

Panics
  • When max_burst is zero.
source

pub fn with_period(replenish_1_per: Duration) -> Option<Quota>

Construct a quota that replenishes one cell in a given interval.

This constructor is meant to replace ::new, in cases where a longer refresh period than 1 cell/hour is necessary.

If the time interval is zero, returns None.

source

pub fn allow_burst<B>(self, max_burst: B) -> Self
where B: TryInto<NonZeroU32>, B::Error: Debug,

Adjusts the maximum burst size for a quota to construct a rate limiter with a capacity for at most the given number of cells.

Panics
  • When max_burst is zero.

Trait Implementations§

source§

impl Clone for Quota

source§

fn clone(&self) -> Quota

Returns a copy 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 Debug for Quota

source§

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

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

impl PartialEq for Quota

source§

fn eq(&self, other: &Quota) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Quota

source§

impl Eq for Quota

source§

impl StructuralEq for Quota

source§

impl StructuralPartialEq for Quota

Auto Trait Implementations§

§

impl RefUnwindSafe for Quota

§

impl Send for Quota

§

impl Sync for Quota

§

impl Unpin for Quota

§

impl UnwindSafe for Quota

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> 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,

§

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>,

§

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>,

§

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.