20 releases (7 stable)

new 1.4.0 Feb 18, 2025
1.3.0 Nov 22, 2024
1.2.0 Sep 2, 2024
0.5.0 Aug 22, 2024
0.0.2 Apr 14, 2022

#54 in Rust patterns

Download history 67691/week @ 2024-10-30 62763/week @ 2024-11-06 67661/week @ 2024-11-13 71892/week @ 2024-11-20 62209/week @ 2024-11-27 72899/week @ 2024-12-04 69488/week @ 2024-12-11 63791/week @ 2024-12-18 53475/week @ 2024-12-25 74556/week @ 2025-01-01 93565/week @ 2025-01-08 90790/week @ 2025-01-15 86470/week @ 2025-01-22 94068/week @ 2025-01-29 120606/week @ 2025-02-05 94044/week @ 2025-02-12

410,341 downloads per month
Used in 283 crates (40 directly)

Apache-2.0

98KB
2K SLoC

BackON   Build Status Latest Version

BackON

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

  • Simple: Just like a built-in feature: your_fn.retry(ExponentialBuilder::default()).await.
  • Flexible: Supports both blocking and async functions.
  • Powerful: Allows control over retry behavior such as when and notify.
  • Customizable: Supports custom retry strategies like exponential, constant, etc.
  • Adaptable: Works on all platforms supported by Rust, including both wasm and no-std.

Quick Start

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–6.5MB
~27K SLoC