backoff::future

Function retry_notify

Source
pub fn retry_notify<I, E, Fn, Fut, B, N>(
    backoff: B,
    operation: Fn,
    notify: N,
) -> Retry<impl Sleeper, B, N, Fn, Fut> 
where B: Backoff, Fn: FnMut() -> Fut, Fut: Future<Output = Result<I, Error<E>>>, N: Notify<E>,
Expand description

Retries given operation according to the Backoff policy. Calls notify on failed attempts (in case of Error::Transient). Backoff is reset before it is used. The returned future can be spawned onto a compatible runtime.

Only available through the tokio and async-std feature flags.

§Async notify

notify can be neither async fn or Future. If you need to perform some async operations inside notify, consider using your runtimes task-spawning functionality.

The reason behind this is that Retry future cannot be responsible for polling notify future, because can easily be dropped before notify is completed. So, considering the fact that most of the time no async operations are required in notify, it’s up to the caller to decide how async notify should be performed.

§Example

use backoff::backoff::Stop;

async fn f() -> Result<(), backoff::Error<&'static str>> {
    // Business logic...
    Err(backoff::Error::transient("error"))
}

let err = backoff::future::retry_notify(Stop {}, f, |e, dur| {
    println!("Error happened at {:?}: {}", dur, e)
})
.await
.err()
.unwrap();
assert_eq!(err, "error");