tryhard/
backoff_strategies.rs

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! 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<'a, 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: &'a E) -> Self::Output;
}

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

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

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

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

impl ExponentialBackoff {
    /// Create a new `ExponentialBackoff` with an initial delay.
    pub fn new(initial_delay: Duration) -> Self {
        Self {
            delay: initial_delay,
        }
    }
}

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

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

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

impl FixedBackoff {
    /// Create a new `FixedBackoff` with an initial delay.
    pub fn new(initial_delay: Duration) -> Self {
        Self {
            delay: initial_delay,
        }
    }
}

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

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

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

impl LinearBackoff {
    /// Create a new `LinearBackoff` with an initial delay.
    pub fn new(initial_delay: Duration) -> Self {
        Self {
            delay: initial_delay,
        }
    }
}

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

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

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

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