pub struct RateLimiter { /* private fields */ }
Expand description
RateLimiter
is a tool which can control the rate at which processing happens.
§Examples
use async_throttle::RateLimiter;
#[tokio::main]
async fn main() {
let period = std::time::Duration::from_millis(10);
let rate_limiter = RateLimiter::new(period);
// Takes 90ms to complete, the first iteration is instant, the next 9 iterations take 100ms
for _ in 0..10 {
rate_limiter.throttle(|| async { /* work */ }).await;
}
}
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(period: Duration) -> RateLimiter
pub fn new(period: Duration) -> RateLimiter
Creates a new rate limiter.
§Examples
use tokio::sync::Mutex;
use anyhow::Result;
use std::time::Duration;
use async_throttle::RateLimiter;
#[tokio::main]
async fn main() -> Result<()> {
RateLimiter::new(Duration::from_millis(10));
Ok(())
}
Sourcepub async fn throttle<Fut, F, T>(&self, f: F) -> T
pub async fn throttle<Fut, F, T>(&self, f: F) -> T
Throttles the execution of a function.
§Examples
use async_throttle::RateLimiter;
use anyhow::Result;
use std::sync::Arc;
async fn do_work() { /* some computation */ }
async fn do_throttle(limiter: Arc<RateLimiter>) {
limiter.throttle(|| do_work()).await
}
Auto Trait Implementations§
impl !Freeze for RateLimiter
impl !RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl !UnwindSafe for RateLimiter
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