summaryrefslogtreecommitdiffstats
path: root/src/backoff.rs
blob: 1f87b8d4c9ff97cdf289ebfbbcbc9b0b5626a7e9 (plain)
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
//! Exponential backoff: a delay that doubles, up to a limit, while something keeps
//! failing, and starts over when it succeeds. Only the delay; each user keeps its own
//! point in time (next attempt, deadline).

use std::time::Duration;

#[derive(Clone, Copy, Debug)]
pub(crate) struct Backoff {
    initial: Duration,
    max: Duration,
    current: Duration,
}

impl Backoff {
    pub(crate) const fn new(initial: Duration, max: Duration) -> Self {
        Self {
            initial,
            max,
            current: initial,
        }
    }

    /// The delay to use now.
    pub(crate) fn current(&self) -> Duration {
        self.current
    }

    /// Doubles the delay, up to the limit.
    pub(crate) fn grow(&mut self) {
        self.current = self.current.saturating_mul(2).min(self.max);
    }

    /// The delay to use now; the next call gives twice as much (up to the limit).
    pub(crate) fn next(&mut self) -> Duration {
        let delay = self.current;
        self.grow();
        delay
    }

    /// Back to the initial delay.
    pub(crate) fn reset(&mut self) {
        self.current = self.initial;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn doubles_up_to_the_limit_and_resets() {
        let mut backoff = Backoff::new(Duration::from_secs(1), Duration::from_secs(5));
        let delays: Vec<_> = (0..5).map(|_| backoff.next().as_secs()).collect();
        assert_eq!(delays, [1, 2, 4, 5, 5]);
        backoff.reset();
        assert_eq!(backoff.current(), Duration::from_secs(1));
    }
}