Crates.io | try-again |
lib.rs | try-again |
version | 0.1.1 |
source | src |
created_at | 2023-02-26 15:36:08.372776 |
updated_at | 2023-02-26 15:51:51.084 |
description | Retry synchronous and asynchronous operations. |
homepage | |
repository | https://github.com/lpotthast/try-again |
max_upload_size | |
id | 795224 |
size | 26,351 |
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.
use try_again::{retry, Delay, Retry, ThreadSleep};
fn some_fallible_operation() -> Result<(), ()> {
Ok(())
}
let final_outcome = retry(
Retry {
max_tries: 5,
delay: Some(Delay::Static {
delay: Duration::from_millis(125),
}),
},
ThreadSleep {},
move || some_fallible_operation(),
);
use try_again::{retry_async, Delay, Retry, TokioSleep};
async fn some_fallible_operation() -> Result<(), ()> {
Ok(())
}
let final_outcome = retry_async(
Retry {
max_tries: 10,
delay: Some(Delay::ExponentialBackoff {
initial_delay: Duration::from_millis(125),
max_delay: Some(Duration::from_secs(2)),
}),
},
TokioSleep {},
move || async move {
some_fallible_operation().await
},
).await;