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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! The types of backoff strategies that are supported

use crate::RetryPolicy;
use std::time::Duration;

/// Trait for computing the amount of delay between attempts.
pub trait BackoffStrategy<E> {
    /// The delay type. Will normally be either [`Duration`] or [`RetryPolicy`].
    ///
    /// [`Duration`]: https://doc.rust-lang.org/stable/std/time/struct.Duration.html
    /// [`RetryPolicy`]: ../enum.RetryPolicy.html
    type Output;

    /// Compute the amount of delay given the number of attempts so far and the most previous
    /// error.
    fn delay(&mut self, attempt: u32, error: &E) -> Self::Output;
}

/// No backoff. This will make the future be retried immediately without any delay in between
/// attempts.
#[derive(Debug)]
pub struct NoBackoff;

impl<E> BackoffStrategy<E> for NoBackoff {
    type Output = Duration;

    #[inline]
    fn delay(&mut self, _attempt: u32, _error: &E) -> Duration {
        Duration::new(0, 0)
    }
}

/// Exponential backoff. The delay will double each time.
#[derive(Debug)]
pub struct ExponentialBackoff {
    pub(crate) delay: Duration,
}

impl<E> BackoffStrategy<E> for ExponentialBackoff {
    type Output = Duration;

    #[inline]
    fn delay(&mut self, _attempt: u32, _error: &E) -> Duration {
        let prev_delay = self.delay;
        self.delay *= 2;
        prev_delay
    }
}

/// Fixed backoff. The delay wont change between attempts.
#[derive(Debug)]
pub struct FixedBackoff {
    pub(crate) delay: Duration,
}

impl<E> BackoffStrategy<E> for FixedBackoff {
    type Output = Duration;

    #[inline]
    fn delay(&mut self, _attempt: u32, _error: &E) -> Duration {
        self.delay
    }
}

/// Linear backoff. The delay will scale linearly with the number of attempts.
#[derive(Debug)]
pub struct LinearBackoff {
    pub(crate) delay: Duration,
}

impl<E> BackoffStrategy<E> for LinearBackoff {
    type Output = Duration;

    #[inline]
    fn delay(&mut self, attempt: u32, _error: &E) -> Duration {
        self.delay * attempt
    }
}

/// A custom backoff strategy defined by a function.
#[derive(Debug)]
pub struct CustomBackoffStrategy<F> {
    pub(crate) f: F,
}

impl<F, E, T> BackoffStrategy<E> for CustomBackoffStrategy<F>
where
    F: FnMut(u32, &E) -> T,
    RetryPolicy: From<T>,
{
    type Output = RetryPolicy;

    #[inline]
    fn delay(&mut self, attempt: u32, error: &E) -> RetryPolicy {
        RetryPolicy::from((self.f)(attempt, error))
    }
}