retry_future/
retry_strategy.rs

1pub mod exponential;
2pub mod infinite;
3pub mod linear;
4
5use std::time::Duration;
6
7use crate::error::TooManyAttempts;
8pub use exponential::ExponentialRetryStrategy;
9pub use infinite::InfiniteRetryStrategy;
10pub use linear::LinearRetryStrategy;
11
12/// Configuration trait for [RetryFuture](crate::RetryFuture).
13///
14/// Goal of the trait is to return either a [duration](std::time::Duration)
15/// which means how long a future needs to sleep before trying to resolve again
16/// or an [error](TooManyAttempts) if there were already too many attempts.
17pub trait RetryStrategy {
18    /// `attempts_before` means how many attempts a [future](crate::future::FutureFactory::Future)
19    /// was trying to resolve to `Ok(_)` after returning `Err(_)`.
20    fn check_attempt(&mut self, attempts_before: usize) -> Result<Duration, TooManyAttempts>;
21
22    /// If `true`, errors propagated using `?` inside a [future](crate::future::FutureFactory::Future)
23    /// will be retried.
24    fn retry_early_returned_errors(&self) -> bool;
25}
26
27impl<T> RetryStrategy for &mut T
28where
29    T: RetryStrategy,
30{
31    fn check_attempt(&mut self, attempts_before: usize) -> Result<Duration, TooManyAttempts> {
32        (*self).check_attempt(attempts_before)
33    }
34
35    fn retry_early_returned_errors(&self) -> bool {
36        (**self).retry_early_returned_errors()
37    }
38}