Skip to main content

AsyncLimiter

Struct AsyncLimiter 

Source
pub struct AsyncLimiter<C: Clock + Clone = SystemClock> { /* private fields */ }
Expand description

An async-friendly wrapper around a RateLimiter.

Holds a limiter and adds until_ready / until_ready_n, which await until the key is admitted rather than returning a denial. The synchronous check / check_n pass straight through.

§Examples

use rate_net::{AsyncLimiter, RateLimiter};

let limiter = AsyncLimiter::new(RateLimiter::per_second(100));

// Non-blocking: returns immediately, allow or deny.
let _ = limiter.check("user:42");

// Awaiting: returns once the key is within its limit.
limiter.until_ready("user:42").await;

Implementations§

Source§

impl<C: Clock + Clone> AsyncLimiter<C>

Source

pub fn new(inner: RateLimiter<C>) -> Self

Wraps a limiter for async use.

Source

pub fn inner(&self) -> &RateLimiter<C>

Borrows the wrapped limiter.

Source

pub fn into_inner(self) -> RateLimiter<C>

Unwraps back into the limiter.

Source

pub fn check(&self, key: impl Into<Key>) -> Decision

Checks a single unit against key without awaiting — a straight pass-through to RateLimiter::check.

Source

pub fn check_n(&self, key: impl Into<Key>, n: u32) -> Decision

Checks n units against key without awaiting — a straight pass-through to RateLimiter::check_n.

Source

pub async fn until_ready(&self, key: impl Into<Key>)

Awaits until a single unit is admitted for key.

Equivalent to until_ready_n(key, 1).

Source

pub async fn until_ready_n(&self, key: impl Into<Key>, n: u32)

Awaits until n units are admitted for key, sleeping for the reported retry_after after each denial.

Returns immediately if n exceeds what the limit can ever grant (a retry_after of Duration::MAX): no amount of waiting would help, so it gives up rather than sleep forever.

Trait Implementations§

Source§

impl<C: Clock + Clone> From<RateLimiter<C>> for AsyncLimiter<C>

Source§

fn from(inner: RateLimiter<C>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<C> Freeze for AsyncLimiter<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for AsyncLimiter<C>
where C: RefUnwindSafe,

§

impl<C> Send for AsyncLimiter<C>

§

impl<C> Sync for AsyncLimiter<C>

§

impl<C> Unpin for AsyncLimiter<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for AsyncLimiter<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for AsyncLimiter<C>
where C: 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> 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, 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<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,