#backoff #adaptive #rate-limiting #throttle

adaptive_backoff

library for intelligently backing off rate limited or fallible resources

3 unstable releases

0.2.1 Feb 23, 2021
0.2.0 Dec 21, 2020
0.1.0 Dec 19, 2020

#2 in #ratelimit

Download history 2430/week @ 2024-07-19 1986/week @ 2024-07-26 1918/week @ 2024-08-02 1694/week @ 2024-08-09 1887/week @ 2024-08-16 1439/week @ 2024-08-23 3110/week @ 2024-08-30 1458/week @ 2024-09-06 1218/week @ 2024-09-13 1238/week @ 2024-09-20 1749/week @ 2024-09-27 2173/week @ 2024-10-04 1454/week @ 2024-10-11 1782/week @ 2024-10-18 1680/week @ 2024-10-25 2748/week @ 2024-11-01

8,512 downloads per month

MIT license

17KB
276 lines

Adaptive Backoff

Adaptive backoff provides a mechanism to intelligently back off use of rate limited or failure operations through sources simples structs, Backoffs. Backoffs take input on success and failure, then return a duration to wait.

For an adaptive backoff, as the failure and success calls increase the returned duration eventually converges on a value to avoid rate limiting of requests.

Usage

Include within your Cargo.toml:

adaptive_backoff = "0.2"

And follow an example below:

Adaptive ExponentialBackoff

Below is an example of an adapative ExponentialBackoff which works from a queue and converges on a minimal delay duration between calls. It grows by a factor of 2.0 to a maximum of 300 seconds.

use std::time::Duration;
use adaptive_backoff::prelude::*;

let mut backoff = ExponentialBackoffBuilder::default()
    .factor(2.0)
    .max(Duration::from_secs(300))
    .adaptive()
    .build()
    .unwrap();

while let Some(item) = queue.pop() {
    loop {
        match worker_iter(&conn, &item).await {
            Ok(_) => {
                delay_for(backoff.success()).await;
                break;
            }
            Err(_) => delay_for(backoff.fail()).await,
        }
    }
}

Simple ExponentialBackoff

If adaptive is omitted from the example above, a simple backoff is returned. Its API lacks success() and fail(), instead it can only return increasing delays with wait() until reset() is called to return it.

use std::time::Duration;
use adaptive_backoff::prelude::*;

let mut backoff = ExponentialBackoffBuilder::default()
    .factor(2.0)
    .max(Duration::from_secs(30))
    .build()
    .unwrap();

while let Some(item) = queue.pop() {
    loop {
        match worker_iter(&conn, &item).await {
            Ok(_) => {
                delay_for(backoff.wait()).await;
                break;
            }
            Err(_) => delay_for(backoff.wait()).await,
        }
    }

    backoff.reset();
}

Additional Examples

There are tests for backoff implementations which contain example use of the external API with expected output. See both the simple exponential example and the adaptive exponential backoff example.

Dependencies

~0.4–0.9MB
~19K SLoC