Retry synchronous or asynchronous operations until they no longer can or need to be retried.
Provides fn retry
for retrying synchronous operations.
Provides async fn retry_async
for retrying asynchronous operations.
The retried closure may return any type that implements NeedsRetry
. This trait is already implemented for any Result
and Option
, allowing you to retry common fallible outcomes.
A retry strategy is required. The provided Retry
type provides an implementation supporting
A delay strategy is required and performs the actual delaying between executions of the users closure:
ThreadSleep {}
can be used, blocking the current thread until the next try should take place.TokioSleep {}
can be used when using the Tokio runtime.Other delay strategies may be implemented to support async_std or other asynchronous runtimes.
```Rust use try_again::{retry, Delay, Retry, ThreadSleep};
fn somefallibleoperation() -> Result<(), ()> { Ok(()) }
let finaloutcome = retry( Retry { maxtries: 5, delay: Some(Delay::Static { delay: Duration::frommillis(125), }), }, ThreadSleep {}, move || somefallible_operation(), ); ```
```Rust use tryagain::{retryasync, Delay, Retry, TokioSleep};
async fn somefallibleoperation() -> Result<(), ()> { Ok(()) }
let finaloutcome = retryasync( Retry { maxtries: 10, delay: Some(Delay::ExponentialBackoff { initialdelay: Duration::frommillis(125), maxdelay: Some(Duration::fromsecs(2)), }), }, TokioSleep {}, move || async move { somefallible_operation().await }, ).await; ```