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
use std::time::Duration;
/// Parameterizes the minimum and maximum delays between retries
/// for a retry strategy based on exponential backoff
#[derive(Copy, Clone, Debug)]
#[cfg_attr(
feature = "serialization",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct RetryStrategy {
pub(crate) min_delay: Duration,
pub(crate) max_delay: Duration,
}
impl RetryStrategy {
/// construct a `RetryStrategy`
///
/// `min_delay` - the minimum amount of time for the retry
/// `max_delay` - the maximum amount of time for the retry
pub fn new(min_delay: Duration, max_delay: Duration) -> Self {
Self {
min_delay,
max_delay,
}
}
}
/// Parameterizes connection attempts
#[derive(Copy, Clone, Debug)]
pub struct ConnectStrategy {
/// Minimum delay between two connection attempts, doubles up to the maximum delay
pub(crate) min_connect_delay: Duration,
/// Maximum delay between two connection attempts
pub(crate) max_connect_delay: Duration,
/// Delay before attempting a connection after a disconnect
pub(crate) reconnect_delay: Duration,
}
impl ConnectStrategy {
/// construct a `ConnectStrategy`
///
/// `min_connect_delay` - Minimum delay between two connection attempts, doubles up to the maximum delay
/// `max_connect_delay` - Maximum delay between two connection attempts
/// `reconnect_delay` - Delay before attempting a connection after a disconnect
pub fn new(
min_connect_delay: Duration,
max_connect_delay: Duration,
reconnect_delay: Duration,
) -> Self {
ConnectStrategy {
min_connect_delay,
max_connect_delay,
reconnect_delay,
}
}
}
impl Default for ConnectStrategy {
fn default() -> Self {
Self::new(
Duration::from_secs(1),
Duration::from_secs(10),
Duration::from_secs(1),
)
}
}
impl Default for RetryStrategy {
fn default() -> Self {
Self::new(Duration::from_secs(1), Duration::from_secs(10))
}
}
#[derive(Clone, Debug)]
pub(crate) struct ExponentialBackOff {
strategy: RetryStrategy,
last: Option<Duration>,
}
impl ExponentialBackOff {
pub(crate) fn new(strategy: RetryStrategy) -> Self {
Self {
strategy,
last: None,
}
}
pub(crate) fn on_success(&mut self) {
self.last = None;
}
pub(crate) fn on_failure(&mut self) -> Duration {
match self.last {
Some(x) => {
let next = x
.checked_mul(2)
.unwrap_or(self.strategy.max_delay)
.min(self.strategy.max_delay);
self.last = Some(next);
next
}
None => {
self.last = Some(self.strategy.min_delay);
self.strategy.min_delay
}
}
}
}