fedimint_core::util

Function retry

Source
pub async fn retry<F, Fut, T>(
    op_name: impl Into<String>,
    strategy: impl Backoff,
    op_fn: F,
) -> Result<T, Error>
where F: Fn() -> Fut, Fut: Future<Output = Result<T, Error>>,
Expand description

Run the supplied closure op_fn until it succeeds. Frequency and number of retries is determined by the specified strategy.

use std::time::Duration;

use fedimint_core::util::{backoff_util, retry};
retry(
    "Gateway balance after swap".to_string(),
    backoff_util::background_backoff(),
    || async {
        // Fallible network calls …
        Ok(())
    },
)
.await
.expect("never fails");

§Returns

  • If the closure runs successfully, the result is immediately returned
  • If the closure did not run successfully for max_attempts times, the error of the closure is returned