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

MultiRateLimiter enables key-based rate limiting, where each key has its own RateLimiter.

This behavior is useful when you want to throttle a set of keys independently, for example you may have a web crawler that wants to throttle its requests to each domain independently.

Examples

use async_throttle::MultiRateLimiter;
use std::sync::Arc;

#[tokio::main]
async fn main() {
   let period = std::time::Duration::from_secs(5);
   let rate_limiter = MultiRateLimiter::new(period);
    
   // This completes instantly
   rate_limiter.throttle("foo", || computation()).await;

   // This completes instantly
   rate_limiter.throttle("bar", || computation()).await;

   // This takes 5 seconds to complete because the key "foo" is rate limited
   rate_limiter.throttle("foo", || computation()).await;
}

async fn computation() { }

Implementations§

source§

impl<K> MultiRateLimiter<K>where K: Eq + Hash + Clone,

source

pub fn new(period: Duration) -> MultiRateLimiter<K>

Creates a new MultiRateLimiter.

source

pub async fn throttle<Fut, F, T>(&self, key: K, f: F) -> impl Future<Output = T>where Fut: Future<Output = T>, F: FnOnce() -> Fut,

Throttles the execution of a function based on a key. Throttling is key-specific, so multiple keys can be throttled independently.

Examples
use async_throttle::MultiRateLimiter;
use anyhow::Result;
use std::sync::Arc;

async fn do_work() { /* some computation */ }

async fn throttle_by_key(the_key: u32, limiter: Arc<MultiRateLimiter<u32>>) {
   limiter.throttle(the_key, || do_work()).await
}

Auto Trait Implementations§

§

impl<K> !RefUnwindSafe for MultiRateLimiter<K>

§

impl<K> Send for MultiRateLimiter<K>where K: Send,

§

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

§

impl<K> Unpin for MultiRateLimiter<K>

§

impl<K> !UnwindSafe for MultiRateLimiter<K>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V