arl 0.2.0

A rate limiter to be used with tokio
Documentation
  • Coverage
  • 80%
    4 out of 5 items documented2 out of 3 items with examples
  • Size
  • Source code size: 24.34 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.57 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 26s Average build duration of successful builds.
  • all releases: 26s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • lukaszwojtow/arl
    9 0 1
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • lukaszwojtow

ARL - Async Rate Limiter

Arl's main purpose is to simplify dealing with various APIs' limits. For example, GitHub allows 5000 requests per hour. Keeping a timer inside your business logic can obscure the main flow and will add lots of boilerplate.

With Arl it's possible to limit a task's speed with single line of code. As an added benefit, the limiter can be cloned and send to other threads/tasks and all of them will see the same instance and adhere to the same limits.

Example:

    // Create a rate limiter. Limit speed to 10 times in 2 seconds.
    let limiter = RateLimiter::new(10, Duration::from_secs(2));
    // Spawn 4 threads.
    for i in 0..4 {
        // RateLimiter can be cloned - all threads will use the same timer/stats underneath.
        let limiter = limiter.clone();
        tokio::spawn(async move {
            // Create some imaginary client (for a rest service or sth).
            let client = Client::new();
            // Do things in a loop. Notice there is no `sleep` in here.
            loop {
                // Wait if needed. First 10 will be executed immediately.
                limiter.wait().await;
                // Call some api here.
                let response = client.call();
            }
        });
    }