Skip to main content

Crate ritlers

Crate ritlers 

Source
Expand description

A task-length aware rate limiter.

Most rate limiters count when a task starts. Because network requests have variable latency, this can still cause bursts to arrive at a downstream API simultaneously. Ritlers instead waits for each task to finish before its slot becomes available again, so the rate limit is respected regardless of how long individual requests take.

When tasks complete instantly the behaviour is identical to a standard token-bucket limiter.

§Feature flags

FeatureDefaultDescription
blockingyesSynchronous blocking::RateLimiter that parks the calling thread
asyncnoAsync async_rt::RateLimiter backed by Tokio

§Quick start

§Blocking

use std::time::Duration;
use ritlers::blocking::RateLimiter;
use ritlers::TaskResult;

let mut limiter = RateLimiter::new(2, Duration::from_secs(1)).unwrap();

// Schedule a plain task
limiter.schedule_task(|| { /* perform API call */ });

// Schedule a task that retries on rate-limit errors
limiter.schedule_task_with_retry(|| {
    match do_api_call() {
        Ok(_)  => TaskResult::Done,
        Err(_) => TaskResult::TryAgain,
    }
});

§Async

use std::time::Duration;
use ritlers::async_rt::RateLimiter;
use ritlers::TaskResult;

let limiter = RateLimiter::new(2, Duration::from_secs(1)).unwrap();

// Schedule a plain task
limiter.schedule_task(async { /* perform API call */ }).await;

// Schedule a task that retries on rate-limit errors
limiter.schedule_task_with_retry(|| async {
    match do_api_call().await {
        Ok(_)  => TaskResult::Done,
        Err(_) => TaskResult::TryAgain,
    }
}).await;

Modules§

async_rt
blocking

Structs§

ZeroAmountError
Error returned by blocking::RateLimiter::new and async_rt::RateLimiter::new when amount is zero.

Enums§

TaskResult
The outcome of a retryable task.