relay_server/utils/
retry.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
use std::time::{Duration, Instant};

use backoff::backoff::Backoff;
use backoff::ExponentialBackoff;

/// Backoff multiplier (1.5 which is 50% increase per backoff).
const DEFAULT_MULTIPLIER: f64 = 1.5;
/// Randomization factor (0 which is no randomization).
const DEFAULT_RANDOMIZATION: f64 = 0.0;
/// Initial interval in milliseconds (1 second).
const INITIAL_INTERVAL: u64 = 1000;

/// A retry interval generator that increases timeouts with exponential backoff.
#[derive(Debug)]
pub struct RetryBackoff {
    backoff: ExponentialBackoff,
    attempt: usize,
}

impl RetryBackoff {
    /// Creates a new retry backoff based on configured thresholds.
    pub fn new(max_interval: Duration) -> Self {
        let backoff = ExponentialBackoff {
            current_interval: Duration::from_millis(INITIAL_INTERVAL),
            initial_interval: Duration::from_millis(INITIAL_INTERVAL),
            randomization_factor: DEFAULT_RANDOMIZATION,
            multiplier: DEFAULT_MULTIPLIER,
            max_interval,
            max_elapsed_time: None,
            clock: Default::default(),
            start_time: Instant::now(),
        };

        RetryBackoff {
            backoff,
            attempt: 0,
        }
    }

    /// Resets this backoff to its initial state.
    pub fn reset(&mut self) {
        self.backoff.reset();
        self.attempt = 0;
    }

    /// Indicates whether a backoff attempt has started.
    pub fn started(&self) -> bool {
        self.attempt > 0
    }

    /// Returns the number of the retry attempt.
    pub fn attempt(&self) -> usize {
        self.attempt
    }

    /// Returns the next backoff duration.
    pub fn next_backoff(&mut self) -> Duration {
        let duration = match self.attempt {
            0 => Duration::new(0, 0),
            _ => self.backoff.next_backoff().unwrap(),
        };

        self.attempt += 1;
        duration
    }
}