#exponential-backoff #retry #backoff #jittered

exp_backoff

Jittered exponential backoff implementation in Rust

6 releases

Uses new Rust 2024

0.1.5 Mar 30, 2025
0.1.4 Mar 14, 2025

#4 in #exponential-backoff

Download history 436/week @ 2025-03-12 33/week @ 2025-03-19 148/week @ 2025-03-26 38/week @ 2025-04-02 16/week @ 2025-04-09 4/week @ 2025-04-16

214 downloads per month
Used in 2 crates

MIT license

6KB
78 lines

main crates.io docs-rs

Overview

This crate implements jittered backoff. Useful when retrying operations that can potentially fail (i.e. network calls). The implementation is based on this article from the AWS Architecture Blog.

Usage

You can use it like so:

use exp_backoff::*;
use std::error::Error;
use std::{thread, time::Duration};

fn func_that_can_fail() -> Result<(), Box<dyn Error>> {
    if true {
        return Err("some error")?;
    }

    Ok(())
}

fn main() -> Result<(), Box<dyn Error>> {
    let mut bo = BackoffBuilder::new().build();
    for _ in 0..5 {
        match func_that_can_fail() {
            Err(e) => {
                println!("failed: {:?}, retry...", e);
                thread::sleep(Duration::from_nanos(bo.pause()));
            }
            _ => println!("we're okay"),
        }
    }

    Ok(())
}

Dependencies

~405KB