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
use std::time::Duration;
use super::super::RetryStrategy;

/// A retry strategy driven by exponential back-off.
///
/// The power corresponds to the number of past attempts.
#[derive(Clone)]
pub struct ExponentialBackoff {
    current: u64,
    base: u64
}

impl ExponentialBackoff {
    /// Constructs a new exponential back-off strategy,
    /// given a base duration in milliseconds.
    ///
    /// The resulting duration is calculated by taking the base to the `n`-th power,
    /// where `n` denotes the number of past attempts.
    pub fn from_millis(base: u64) -> ExponentialBackoff {
        ExponentialBackoff{current: base, base: base}
    }
}

impl RetryStrategy for ExponentialBackoff {
    fn delay(&mut self) -> Option<Duration> {
        let duration = Duration::from_millis(self.current);
        self.current = self.current * self.base;
        return Some(duration);
    }
}

#[test]
fn returns_some_exponential_base_10() {
    let mut s = ExponentialBackoff::from_millis(10);

    assert_eq!(s.delay(), Some(Duration::from_millis(10)));
    assert_eq!(s.delay(), Some(Duration::from_millis(100)));
    assert_eq!(s.delay(), Some(Duration::from_millis(1000)));
}

#[test]
fn returns_some_exponential_base_2() {
    let mut s = ExponentialBackoff::from_millis(2);

    assert_eq!(s.delay(), Some(Duration::from_millis(2)));
    assert_eq!(s.delay(), Some(Duration::from_millis(4)));
    assert_eq!(s.delay(), Some(Duration::from_millis(8)));
}