1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
pub mod exponential;
pub mod infinite;
pub mod linear;

use std::time::Duration;

use crate::error::TooManyAttempts;
pub use exponential::ExponentialRetryStrategy;
pub use infinite::InfiniteRetryStrategy;
pub use linear::LinearRetryStrategy;

/// Configuration trait for [RetryFuture](crate::RetryFuture).
///
/// Goal of the trait is to return either a [duration](std::time::Duration)
/// which means how long a future needs to sleep before trying to resolve again
/// or an [error](TooManyAttempts) if there were already too many attempts.
pub trait RetryStrategy {
    /// `attempts_before` means how many attempts a [future](crate::future::FutureFactory::Future)
    /// was trying to resolve to `Ok(_)` after returning `Err(_)`.
    fn check_attempt(&mut self, attempts_before: usize) -> Result<Duration, TooManyAttempts>;

    /// If `true`, errors propagated using `?` inside a [future](crate::future::FutureFactory::Future)
    /// will be retried.
    fn retry_early_returned_errors(&self) -> bool;
}

impl<T> RetryStrategy for &mut T
where
    T: RetryStrategy,
{
    fn check_attempt(&mut self, attempts_before: usize) -> Result<Duration, TooManyAttempts> {
        (*self).check_attempt(attempts_before)
    }

    fn retry_early_returned_errors(&self) -> bool {
        (**self).retry_early_returned_errors()
    }
}