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
use std::{sync::{Arc, Mutex}, time::Duration};
use backoff::backoff::Backoff as InnerBackoff;
#[derive(Debug)]
pub enum SupervisionStrategy {
Stop,
Retry(Box<dyn RetryStrategy>)
}
pub trait RetryStrategy: std::fmt::Debug + Send + Sync {
fn max_retries(&self) -> usize;
fn next_backoff(&mut self) -> Option<Duration>;
}
#[derive(Debug, Default)]
pub struct NoIntervalStrategy {
max_retries: usize
}
impl NoIntervalStrategy {
pub fn new(max_retries: usize) -> Self {
NoIntervalStrategy {
max_retries
}
}
}
impl RetryStrategy for NoIntervalStrategy {
fn max_retries(&self) -> usize {
self.max_retries
}
fn next_backoff(&mut self) -> Option<Duration> {
None
}
}
#[derive(Debug, Default)]
pub struct FixedIntervalStrategy {
max_retries: usize,
duration: Duration
}
impl FixedIntervalStrategy {
pub fn new(max_retries: usize, duration: Duration) -> Self {
FixedIntervalStrategy {
max_retries,
duration
}
}
}
impl RetryStrategy for FixedIntervalStrategy {
fn max_retries(&self) -> usize {
self.max_retries
}
fn next_backoff(&mut self) -> Option<Duration> {
Some(self.duration)
}
}
#[derive(Debug, Default)]
pub struct ExponentialBackoffStrategy {
max_retries: usize,
inner: Arc<Mutex<backoff::ExponentialBackoff>>
}
impl ExponentialBackoffStrategy {
pub fn new(max_retries: usize) -> Self {
ExponentialBackoffStrategy {
max_retries,
inner: Arc::new(Mutex::new(backoff::ExponentialBackoff::default()))
}
}
}
impl RetryStrategy for ExponentialBackoffStrategy {
fn max_retries(&self) -> usize {
self.max_retries
}
fn next_backoff(&mut self) -> Option<Duration> {
self.inner.lock().ok().and_then(|mut eb| {
eb.next_backoff()
})
}
}