[][src]Function retry_fn::retry

pub fn retry<I, F, T, E>(iter: I, mut f: F) -> Result<T, RetryErr<E>> where
    I: IntoIterator<Item = Duration>,
    F: FnMut(RetryOp) -> RetryResult<T, E>, 

Retry a function on some time interval

use retry_fn::{retry, RetryResult, strategy::ExponentialBackoff};
let mut count = 0;
let res = retry(ExponentialBackoff::new(Duration::from_secs(2)), |op| {
   if op.retries >= 3 {
       RetryResult::<&str, _>::Err(io::Error::new(
           io::ErrorKind::TimedOut,
           "timed out",
       ))
   } else {
       count += 1;
       RetryResult::Retry()
   }
});
assert_eq!(count, 3);
assert!(res.is_err());
Ok(())

Returns

If successful, return Ok, otherwise return Retry to try again or Err to exit with an error