Expand description

backon intends to provide an opposite backoff implementation of the popular backoff.

  • Newer: developed by Rust edition 2021 and latest stable.
  • Cleaner: Iterator based abstraction, easy to use, customization friendly.
  • Easier: Trait based implementations, works like a native function provided by closures.

Backoff

Any types that implements Iterator<Item = Duration> can be used as backoff.

backon also provides backoff implementations with reasonable defaults:

Examples

Retry with default settings.

use anyhow::Result;
use backon::ExponentialBackoff;
use backon::Retryable;

async fn fetch() -> Result<String> {
    Ok(reqwest::get("https://www.rust-lang.org")
        .await?
        .text()
        .await?)
}

#[tokio::main]
async fn main() -> Result<()> {
    let content = fetch.retry(ExponentialBackoff::default()).await?;

    println!("fetch succeeded: {}", content);
    Ok(())
}

Retry with specify retryable error.

use anyhow::Result;
use backon::ExponentialBackoff;
use backon::Retryable;

async fn fetch() -> Result<String> {
    Ok(reqwest::get("https://www.rust-lang.org")
        .await?
        .text()
        .await?)
}

#[tokio::main]
async fn main() -> Result<()> {
    let content = fetch
        .retry(ExponentialBackoff::default())
        .when(|e| e.to_string() == "retryable")
        .await?;

    println!("fetch succeeded: {}", content);
    Ok(())
}

Structs

ConstantBackoff provides backoff with constant delay and limited times.
Exponential backoff implementation.
Retry struct generated by Retryable.

Traits

Backoff is an Iterator that returns Duration.
Retryable will add retry support for functions that produces a futures with results.