#async #async-retry #retry #backoff

no-std backon

Make retry like a built-in feature provided by Rust

22 releases (9 stable)

new 1.5.0 Apr 9, 2025
1.4.1 Mar 19, 2025
1.4.0 Feb 18, 2025
1.3.0 Nov 22, 2024
0.0.2 Apr 14, 2022

#54 in Rust patterns

Download history 52095/week @ 2024-12-22 61119/week @ 2024-12-29 93223/week @ 2025-01-05 93553/week @ 2025-01-12 84008/week @ 2025-01-19 91225/week @ 2025-01-26 115366/week @ 2025-02-02 112892/week @ 2025-02-09 122837/week @ 2025-02-16 147065/week @ 2025-02-23 164505/week @ 2025-03-02 192016/week @ 2025-03-09 217437/week @ 2025-03-16 206475/week @ 2025-03-23 203697/week @ 2025-03-30 265936/week @ 2025-04-06

906,228 downloads per month
Used in 361 crates (53 directly)

Apache-2.0

110KB
2K SLoC

BackON   Build Status Latest Version

BackON

Make retry like a built-in feature provided by Rust.

  • Simple API: Native feel: your_fn.retry(ExponentialBuilder::default()).await.
  • Sync & Async: Supports both blocking and async operations seamlessly.
  • Precise Control: Define when to retry and get notified via when and notify.
  • Custom Strategies: Use built-in backoff strategies (exponential, constant) or define custom ones. Also supports dynamic backoff, such as using the HTTP Retry-After header.
  • Cross-Platform: Works everywhere Rust does, including wasm & no-std.

Quick Start

For more examples, check out the examples.

Retry an async function.

use anyhow::Result;
use backon::ExponentialBuilder;
use backon::Retryable;

async fn fetch() -> Result<String> {
    Ok("hello, world!".to_string())
}

#[tokio::main]
async fn main() -> Result<()> {
    let content = fetch
        // Retry with exponential backoff
        .retry(ExponentialBuilder::default())
        // Sleep implementation, required if no feature has been enabled
        .sleep(tokio::time::sleep)
        // When to retry
        .when(|e| e.to_string() == "EOF")
        // Notify when retrying
        .notify(|err: &anyhow::Error, dur: Duration| {
            println!("retrying {:?} after {:?}", err, dur);
        })
        .await?;
    println!("fetch succeeded: {}", content);

    Ok(())
}

Retry a blocking function.

use anyhow::Result;
use backon::BlockingRetryable;
use backon::ExponentialBuilder;

fn fetch() -> Result<String> {
    Ok("hello, world!".to_string())
}

fn main() -> Result<()> {
    let content = fetch
        // Retry with exponential backoff
        .retry(ExponentialBuilder::default())
        // Sleep implementation, required if no feature has been enabled
        .sleep(std::thread::sleep)
        // When to retry
        .when(|e| e.to_string() == "EOF")
        // Notify when retrying
        .notify(|err: &anyhow::Error, dur: Duration| {
            println!("retrying {:?} after {:?}", err, dur);
        })
        .call()?;
    println!("fetch succeeded: {}", content);

    Ok(())
}

Contributing

Check out the CONTRIBUTING.md guide for more details on getting started with contributing to this project.

Getting help

Submit issues for bug report or asking questions in discussion.

License

Licensed under Apache License, Version 2.0.

Dependencies

~0–6MB
~27K SLoC