Skip to main content

Crate relentless

Crate relentless 

Source
Expand description

relentless — a Rust library for retrying fallible operations and polling for conditions.

This crate provides composable retry strategies with support for std, alloc, and no_std environments.

§Custom wait strategies

use core::time::Duration;
use relentless::{RetryState, Wait, wait};

struct CustomWait(Duration);

impl Wait for CustomWait {
    fn next_wait(&self, _state: &RetryState) -> Duration {
        self.0
    }
}

let strategy = CustomWait(Duration::from_millis(20))
    .cap(Duration::from_millis(15))
    .chain(wait::fixed(Duration::from_millis(50)), 2);

let state = RetryState::new(3, None);
assert_eq!(strategy.next_wait(&state), Duration::from_millis(50));

§Extension-first usage

In sync std builds, .sleep(...) is optional because relentless falls back to std::thread::sleep. The example below still calls .sleep(...) so it compiles under no_std documentation test runs too.

use core::time::Duration;
use relentless::{RetryExt, stop, wait};

let result = (|| Err::<u32, &str>("transient"))
    .retry()
    .stop(stop::attempts(3))
    .wait(wait::fixed(Duration::from_millis(5)))
    .sleep(|_dur| {})
    .call();

assert!(result.is_err());

Re-exports§

pub use predicate::Predicate;
pub use sleep::Sleeper;
pub use stop::Stop;
pub use wait::Wait;

Modules§

predicate
Predicate trait and built-in retry predicate factories.
sleep
stop
Stop trait and built-in stop strategies.
wait
Wait trait and built-in wait strategies.

Structs§

AsyncRetry
Async retry execution object.
AsyncRetryBuilder
Owned async retry builder created from AsyncRetryExt::retry_async.
AsyncRetryBuilderWithStats
Owned async retry builder wrapper that returns statistics.
AsyncRetryWithStats
Async retry execution wrapper that returns statistics.
AttemptState
Read-only context passed to the after_attempt hook.
ExitState
Final read-only context passed to the on_exit hook.
RetryPolicy
Reusable retry configuration.
RetryState
Non-generic retry context passed to Stop::should_stop, Wait::next_wait, the operation, and the before_attempt hook.
RetryStats
Aggregate statistics for a completed retry execution.
SyncRetry
Sync retry execution object.
SyncRetryBuilder
Owned sync retry builder created from RetryExt::retry.
SyncRetryBuilderWithStats
Owned sync retry builder wrapper that returns statistics.
SyncRetryWithStats
Sync retry execution wrapper that returns statistics.

Enums§

RetryError
Error returned when a retry loop terminates without producing an accepted result.
StopReason
Why a retry loop terminated.

Traits§

AsyncRetryExt
Extension trait to start async retries directly from a closure/function.
RetryExt
Extension trait to start sync retries directly from a closure/function.

Functions§

retry
Returns a SyncRetryBuilder with default policy: attempts(3), exponential(100ms), any_error().
retry_async
Returns an AsyncRetryBuilder with default policy: attempts(3), exponential(100ms), any_error().

Type Aliases§

DefaultAsyncRetryBuilder
Alias for the default owned async retry builder returned by AsyncRetryExt::retry_async.
DefaultAsyncRetryBuilderWithStats
Alias for the default owned async retry builder-with-stats returned by calling .with_stats() on AsyncRetryExt::retry_async.
DefaultSyncRetryBuilder
Alias for the default owned sync retry builder returned by RetryExt::retry.
DefaultSyncRetryBuilderWithStats
Alias for the default owned sync retry builder-with-stats returned by calling .with_stats() on RetryExt::retry.
RetryResult
Convenience alias: Result<T, RetryError<T, E>>.