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)
.
The quota of Quota::per_minute(60)
has a burst size of 60 cells, meaning it is
possible to accomodate 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
impl Quota
pub fn rate_per_second(max_burst: u32) -> PyResult<Self>
pub fn rate_per_minute(max_burst: u32) -> PyResult<Self>
pub fn rate_per_hour(max_burst: u32) -> PyResult<Self>
Source§impl Quota
Constructors for Quotas
impl Quota
Constructors for Quotas
Sourcepub const fn per_second(max_burst: NonZeroU32) -> Self
pub const fn per_second(max_burst: NonZeroU32) -> Self
Construct a quota for a number of cells per second. The given number of cells is also assumed to be the maximum burst size.
Sourcepub const fn per_minute(max_burst: NonZeroU32) -> Self
pub const fn per_minute(max_burst: NonZeroU32) -> Self
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.
Sourcepub const fn per_hour(max_burst: NonZeroU32) -> Self
pub const fn per_hour(max_burst: NonZeroU32) -> Self
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.
Sourcepub const fn with_period(replenish_1_per: Duration) -> Option<Self>
pub const fn with_period(replenish_1_per: Duration) -> Option<Self>
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
.
Sourcepub const fn allow_burst(self, max_burst: NonZeroU32) -> Self
pub const fn allow_burst(self, max_burst: NonZeroU32) -> Self
Adjusts the maximum burst size for a quota to construct a rate limiter with a capacity for at most the given number of cells.
Sourcepub fn new(max_burst: NonZeroU32, replenish_all_per: Duration) -> Option<Self>
👎Deprecated since 0.2.0: This constructor is often confusing and non-intuitive. Use the per_(interval)
/ with_period
and max_burst
constructors instead.
pub fn new(max_burst: NonZeroU32, replenish_all_per: Duration) -> Option<Self>
per_(interval)
/ with_period
and max_burst
constructors instead.Construct a quota for a given burst size, replenishing the entire burst size in that given unit of time.
Returns None
if the duration is zero.
This constructor allows greater control over the resulting
quota, but doesn’t make as much intuitive sense as other
methods of constructing the same quotas. Unless your quotas
are given as “max burst size, and time it takes to replenish
that burst size”, you are better served by the
Quota::per_second
(and similar)
constructors with the allow_burst
modifier.
Source§impl Quota
Retrieving information about a quota
impl Quota
Retrieving information about a quota
Sourcepub const fn replenish_interval(&self) -> Duration
pub const fn replenish_interval(&self) -> Duration
The time it takes for a rate limiter with an exhausted burst budget to replenish a single element.
Sourcepub const fn burst_size(&self) -> NonZeroU32
pub const fn burst_size(&self) -> NonZeroU32
The maximum number of cells that can be allowed in one burst.
Sourcepub const fn burst_size_replenished_in(&self) -> Duration
pub const fn burst_size_replenished_in(&self) -> Duration
The time it takes to replenish the entire maximum burst size.
Trait Implementations§
Source§impl<'py> IntoPyObject<'py> for Quota
impl<'py> IntoPyObject<'py> for Quota
Source§impl PyClassImpl for Quota
impl PyClassImpl for Quota
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§type ThreadChecker = SendablePyClass<Quota>
type ThreadChecker = SendablePyClass<Quota>
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny
by default, and when you declare
#[pyclass(extends=PyDict)]
, it’s PyDict
.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyMethods<Quota> for PyClassImplCollector<Quota>
impl PyMethods<Quota> for PyClassImplCollector<Quota>
fn py_methods(self) -> &'static PyClassItems
Source§impl PyTypeInfo for Quota
impl PyTypeInfo for Quota
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType>
fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType>
PyTypeInfo::type_object
PyTypeInfo::type_object
].§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object
is an instance of this type or a subclass of this type.§fn is_type_of_bound(object: &Bound<'_, PyAny>) -> bool
fn is_type_of_bound(object: &Bound<'_, PyAny>) -> bool
PyTypeInfo::is_type_of
PyTypeInfo::is_type_of
].§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object
is an instance of this type.§fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool
PyTypeInfo::is_exact_type_of
PyTypeInfo::is_exact_type_of
].impl Copy for Quota
impl DerefToPyAny for Quota
impl Eq for Quota
impl StructuralPartialEq for Quota
Auto Trait Implementations§
impl Freeze for Quota
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> FromPyObject<'_> for Twhere
T: PyClass + Clone,
impl<T> FromPyObject<'_> for Twhere
T: PyClass + Clone,
§fn extract_bound(obj: &Bound<'_, PyAny>) -> Result<T, PyErr>
fn extract_bound(obj: &Bound<'_, PyAny>) -> Result<T, PyErr>
§impl<'py, T> FromPyObjectBound<'_, 'py> for Twhere
T: FromPyObject<'py>,
impl<'py, T> FromPyObjectBound<'_, 'py> for Twhere
T: FromPyObject<'py>,
§fn from_py_object_bound(ob: Borrowed<'_, 'py, PyAny>) -> Result<T, PyErr>
fn from_py_object_bound(ob: Borrowed<'_, 'py, PyAny>) -> Result<T, PyErr>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self
into an owned Python object, dropping type information.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self
into an owned Python object, dropping type information and unbinding it
from the 'py
lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self
into a Python object. Read more