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: Eq + Hash + Clone> MultiRateLimiter<K>
impl<K: Eq + Hash + Clone> MultiRateLimiter<K>
Sourcepub fn new(period: Duration) -> Self
pub fn new(period: Duration) -> Self
Creates a new MultiRateLimiter
.
Sourcepub async fn throttle<Fut, F, T>(&self, key: K, f: F) -> T
pub async fn throttle<Fut, F, T>(&self, key: K, f: F) -> T
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> Freeze for MultiRateLimiter<K>
impl<K> !RefUnwindSafe for MultiRateLimiter<K>
impl<K> Send for MultiRateLimiter<K>where
K: Send,
impl<K> Sync for MultiRateLimiter<K>
impl<K> Unpin for MultiRateLimiter<K>
impl<K> !UnwindSafe for MultiRateLimiter<K>
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
Mutably borrows from an owned value. Read more