Skip to main content

RateLimiterMap

Struct RateLimiterMap 

Source
pub struct RateLimiterMap<K> { /* private fields */ }
Expand description

A per-key rate limiter map.

Keys must be explicitly inserted via insert_rate before use; unknown keys are always rejected.

Implementations§

Source§

impl<K> RateLimiterMap<K>
where K: Hash + Eq + Clone + Send + Sync + 'static,

Source

pub fn new() -> Self

Create an empty map.

Source

pub fn insert_rate(&self, key: K, rate: u64)

Insert a rate limiter for key with the given rate and a 1-second interval.

Overwrites any existing limiter for the same key.

Source

pub fn insert_rate_interval(&self, key: K, rate: u64, interval: Duration)

Insert a rate limiter for key with the given rate and interval.

Source

pub fn remove(&self, key: &K)

Remove the rate limiter for key.

After removal the key is unknown and will be rejected by check (same as a key that was never inserted).

Source

pub fn len(&self) -> usize

Returns the number of keys currently managed.

Source

pub fn is_empty(&self) -> bool

Returns true if no keys are being rate-limited.

Source

pub fn contains_key(&self, key: &K) -> bool

Returns true if key has an active rate limiter.

Source

pub fn clear(&self)

Remove all rate limiters.

Source

pub fn retain(&self, f: impl FnMut(&K, &mut RateLimiter) -> bool)

Retain only the keys for which f returns true.

Each remaining key’s limiter is unchanged.

Source

pub fn entry(&self, key: K) -> Entry<'_, K, RateLimiter>

Get an Entry for key, allowing in-place insertion or modification.

Use entry(key).or_insert(limiter) to insert a limiter only if the key is absent, or entry(key).and_modify(...) to adjust an existing one.

Source

pub async fn check(&self, key: &K) -> bool

Check whether key is allowed right now.

Returns true if the key’s limiter has available tokens. Returns false if the key has not been inserted (unknown keys are rejected by default).

Source

pub async fn check_opt(&self, key: &K) -> Option<bool>

Check key and distinguish “unknown” from “rate-limited”.

Returns Some(true) if allowed, Some(false) if rate-limited, None if the key has not been inserted.

Source

pub async fn time_until_available(&self, key: &K) -> Duration

Returns the duration until key becomes available again.

Returns Duration::MAX if the key is not inserted or the rate is zero, Duration::ZERO if tokens are immediately available.

Source

pub async fn wait(&self, key: &K)

Block until key is allowed, then consume a token.

Returns immediately if the key is not inserted (no-op).

Trait Implementations§

Source§

impl<K: Clone> Clone for RateLimiterMap<K>

Source§

fn clone(&self) -> RateLimiterMap<K>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<K> !RefUnwindSafe for RateLimiterMap<K>

§

impl<K> !UnwindSafe for RateLimiterMap<K>

§

impl<K> Freeze for RateLimiterMap<K>

§

impl<K> Send for RateLimiterMap<K>
where K: Send + Sync,

§

impl<K> Sync for RateLimiterMap<K>
where K: Send + Sync,

§

impl<K> Unpin for RateLimiterMap<K>

§

impl<K> UnsafeUnpin for RateLimiterMap<K>

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more