quick_retry 0.1.0

a lightweight, zero-dependenc retry library for Rust operations.
Documentation
  • Coverage
  • 10%
    1 out of 10 items documented0 out of 7 items with examples
  • Size
  • Source code size: 9.7 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 108.7 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • dex0o0/quick_retry
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • dex0o0

Crates.io Documentation License: MIT

A lightweight, zero-dependency Rust library for retrying fallible operations with flexible delay and backoff strategies.


Features

  • Zero External Dependencies: Fast compile times and zero bloat.
  • Builder Pattern: Clean, chainable API for configuring retry behaviors.
  • Exponential Backoff: Easily configure delay scaling factors for resilient network/IO operations.
  • Zero-Cost Abstractions: Lightweight implementation designed for maximum performance.
  • 100% Safe & Tested: Fully covered with unit tests and doc tests.

Installation

Add quick_retry to your Cargo.toml:

cargo add quick_retry
[dependencies]
quick_retry = "0.1.0"

Quick Start

Basic Usage

Retry an operation up to 3 times with a default 100ms fixed delay:

use quick_retry::Retry;
use std::time::Duration;

fn main() {
    let result = Retry::new()
        .max_attempts(3)
        .delay(Duration::from_millis(100))
        .run(|| {
            // Your fallible operation (I/O, network request, database query)
            fetch_data()
        });

    match result {
        Ok(data) => println!("Success: {}", data),
        Err(err) => eprintln!("Failed after retries: {}", err),
    }
}

fn fetch_data() -> Result<&'static str, &'static str> {
    // Simulating operation
    Ok("Data fetched successfully!")
}

Advanced Usage: Exponential Backoff

increase delay exponential after each failed attempt (e.g 100ms --> 200ms --> 400ms):

use quick_retry::Retry;
use std::time::Duration;

fn main() {
    let result = Retry::new()
        .max_attempts(5)
        .delay(Duration::from_millis(100))
        .exponential_backoff(2.0) // Doubles the delay each time
        .run(|| {
            connect_to_server()
        });

    if let Ok(connection) = result {
        println!("Connected: {:?}", connection);
    }
}

fn connect_to_server() -> Result<&'static str, &'static str> {
    Err("Connection timed out")
}

API Reference

Method Dercription Default
Retry::new() Creates Retry build instance. max_attempts: 3, delay: 100ms , Fixed
.max_attempts(u32) Sets the maximum number of attempts before failing. 3
.delay(Duration) Sets the initial delay duration between 100ms
exponential_backoff(f64) Enables exponential backoff with th given multiplier factor. Disabled(Fixed)
.run(F) Executes the operation closure FnMut() -> Result<T,E>. N/A

License

This project Licensed under the MIT License-see the LICENSE file for details.

Support & Feadback

if you find this crate useful, please consider given it a star on GitHub