#retry #serde #backoff #backon

backoff-config

Flexible backoff configuration in Rust

5 releases

Uses new Rust 2024

new 0.0.5 May 24, 2025
0.0.4 May 22, 2025
0.0.3 May 22, 2025
0.0.2 May 20, 2025
0.0.1 May 20, 2025

#278 in Configuration

Download history 247/week @ 2025-05-17

247 downloads per month

MIT license

17KB
251 lines

🔄 backoff-config ⚙️

codecov

backoff-config makes configuring backoff more flexible by providing a unified Backoff strategy enum and a BackoffConfig that supports deserialization.

The actual backoff logic is powered by the awesome backon crate. Make sure to check out backon to explore its amazing features and ergonomics!

backoff-config integrates with backon by implementing:

Usage

  1. Add backoff-config to your dependencies:
cargo add backoff-config
  1. Load BackoffConfig and use it straight away in retries. Example with figment:
use serde::Deserialize;
use backon::Retryable;

#[derive(Deserialize)]
pub struct Config {
    pub backoff: BackoffConfig,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Env variables:
    //
    // CONFIG__BACKOFF__STRATEGY=Constant
    // CONFIG__BACKOFF__DELAY=1s
    // CONFIG__BACKOFF__MAX_RETRIES=4
    let config = figment::Figment::new()
        .merge(Env::prefixed("CONFIG__").split("__"))
        .extract::<Config>()?;

    // Use in retries: 
    let response_body = fetch.retry(config.backoff).await?;
    println!("Response body: {response_body}");

    Ok(())
}

// Function that may fail
async fn fetch() -> anyhow::Result<String> {
    let body = reqwest::get("https://www.rust-lang.org")
        .await?
        .text()
        .await?;

    Ok(body)
}

Examples

cargo run --example toml

And some examples on data formats:

  • Env – loading config from environment variables.
  • TOML – loading config from a TOML file.

Dependencies

~2–2.6MB
~60K SLoC