backon: Maybe the most elegant retry library ever

159 points xuanwo

Hello, everyone! I have built an elegant retry library: backon today. I share here to get some feedback about this crate.

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(ExponentialBuilder::default()).call()?;
    println!("fetch succeeded: {}", content);

    Ok(())
}

Retry an async function:

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(())
}