Function fluvio_future::retry::retry_if
source · pub async fn retry_if<I, O, F, E, A, P>(
retries: I,
factory: A,
condition: P
) -> Result<O, E>
Expand description
Provides retry functionality in async context. The Future
that you want to retry needs to be
represented in FnMut() -> Future
structure. Each retry creates a new instance of Future
and
awaits it. Iterator Iterator<Item=Duration>
controls the number of retries and delays between
them. If iterator returns None, retries stop. There are three common implementations of retry
strategies: FixedDelay, FibonacciBackoff, and ExponentialBackoff.
Example:
use std::io::{Error, ErrorKind};
use std::ops::AddAssign;
use std::time::Duration;
use fluvio_future::retry::FixedDelay;
use fluvio_future::retry::retry;
fluvio_future::task::run(async {
let mut attempts = 0u8;
let result = retry(FixedDelay::from_millis(100).take(2), || {
attempts.add_assign(1);
operation()
}).await;
assert!(matches!(result, Err(err) if err.kind() == ErrorKind::NotFound));
assert_eq!(attempts, 3); // first attempt + 2 retries
});
async fn operation() -> Result<(), Error> {
Err(Error::from(ErrorKind::NotFound))
}