#retry #macro #block #operations #backoff #behavior #iterator

retry-block

Utilities to retry operations that may fail with configurable backoff behavior using macros over blocks of code

1 stable release

1.0.0 Jul 6, 2022

#2068 in Rust patterns

Download history 15/week @ 2023-11-20 37/week @ 2023-11-27 13/week @ 2023-12-04 18/week @ 2023-12-11 2/week @ 2024-01-01 10/week @ 2024-01-08 26/week @ 2024-02-19 13/week @ 2024-02-26 16/week @ 2024-03-04

55 downloads per month

MIT license

39KB
682 lines

retry-block

retry-block provides utilities to retry operations that may fail with configurable backoff behavior using macros over blocks of code

Usage

Retry an operation using the corresponding retry macro or retry_fn function. The macro accepts an iterator over Durations and a block that returns a Result (or OperationResult; see below). The iterator is used to determine how long to wait after each unsuccessful try and how many times to try before giving up and returning Result::Err. The block determines either the final successful value, or an error value, which can either be returned immediately or used to indicate that the operation should be retried.

Any type that implements IntoIterator<Item = Duration> can be used to determine retry behavior, though a few useful implementations are provided in the delay module, including a fixed delay and exponential back-off.

The Iterator API can be used to limit or modify the delay strategy. For example, to limit the number of retries to 1:

# use retry_block::retry;
# use retry_block::delay::Fixed;
# use std::time::Duration;
# use retry_block::OperationResult;

let mut collection = vec![1, 2, 3].into_iter();

let result = retry!(Fixed::new(Duration::from_millis(100)).take(1), {
    match collection.next() {
        Some(n) if n == 3 => Ok("n is 3!"),
        Some(_) => Err("n must be 3!"),
        None => Err("n was never 3!"),
    }
});

assert!(result.is_err());

Dependencies

~0–11MB
~83K SLoC