Struct tokio_utils::MultiRateLimiter
source · 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,
impl<K> MultiRateLimiter<K>where K: Eq + Hash + Clone,
sourcepub fn new(period: Duration) -> MultiRateLimiter<K>
pub fn new(period: Duration) -> MultiRateLimiter<K>
Creates a new MultiRateLimiter
.
sourcepub async fn throttle<Fut, F, T>(&self, key: K, f: F) -> impl Future<Output = T>where
Fut: Future<Output = T>,
F: FnOnce() -> Fut,
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
}