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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use crate::EntryRetryInfo;
use std::cmp;
use std::time::Duration;
/// What to do when a `RetryPolicy` runs out of attempts or duration.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub enum OnMaxAttempts {
/// Convert the retryable failure into a terminal failure on the run handle.
#[default]
FailAsTerminal,
/// Pause the invocation instead of failing it. The invocation MUST be manually resumed by the user.
/// Requires service protocol V7 or newer.
Pause,
}
/// This struct represents the policy to execute retries.
#[derive(Debug, Clone, Default)]
pub enum RetryPolicy {
/// # Infinite
///
/// Infinite retry strategy.
#[default]
Infinite,
/// # None
///
/// No retry strategy, fail on first failure.
None,
/// # Fixed delay
///
/// Retry with a fixed delay strategy.
FixedDelay {
/// # Interval
///
/// Interval between retries. If none, the runtime will provide one based on the invoker retry policy.
interval: Option<Duration>,
/// # Max attempts
///
/// Gives up retrying when either this number of attempts is reached,
/// or `max_duration` (if set) is reached first.
/// Infinite retries if this field and `max_duration` are unset.
max_attempts: Option<u32>,
/// # Max duration
///
/// Gives up retrying when either the retry loop lasted for this given max duration,
/// or `max_attempts` (if set) is reached first.
/// Infinite retries if this field and `max_attempts` are unset.
max_duration: Option<Duration>,
/// # On max attempts
///
/// What to do once `max_attempts` or `max_duration` is reached.
on_max_attempts: OnMaxAttempts,
},
/// # Exponential
///
/// Retry with an exponential strategy. The next retry is computed as `min(last_retry_interval * factor, max_interval)`.
Exponential {
/// # Initial Interval
///
/// Initial interval for the first retry attempt.
initial_interval: Duration,
/// # Factor
///
/// The factor to use to compute the next retry attempt. This value should be higher than 1.0
factor: f32,
/// # Max interval
///
/// Maximum interval between retries.
max_interval: Option<Duration>,
/// # Max attempts
///
/// Gives up retrying when either this number of attempts is reached,
/// or `max_duration` (if set) is reached first.
/// Infinite retries if this field and `max_duration` are unset.
max_attempts: Option<u32>,
/// # Max duration
///
/// Gives up retrying when either the retry loop lasted for this given max duration,
/// or `max_attempts` (if set) is reached first.
/// Infinite retries if this field and `max_attempts` are unset.
max_duration: Option<Duration>,
/// # On max attempts
///
/// What to do once `max_attempts` or `max_duration` is reached.
on_max_attempts: OnMaxAttempts,
},
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) enum NextRetry {
Retry(Option<Duration>),
FailAsTerminal,
Pause,
}
impl RetryPolicy {
pub fn fixed_delay(
interval: Option<Duration>,
max_attempts: Option<u32>,
max_duration: Option<Duration>,
on_max_attempts: OnMaxAttempts,
) -> Self {
Self::FixedDelay {
interval,
max_attempts,
max_duration,
on_max_attempts,
}
}
pub fn exponential(
initial_interval: Duration,
factor: f32,
max_attempts: Option<u32>,
max_interval: Option<Duration>,
max_duration: Option<Duration>,
on_max_attempts: OnMaxAttempts,
) -> Self {
Self::Exponential {
initial_interval,
factor,
max_attempts,
max_interval,
max_duration,
on_max_attempts,
}
}
pub(crate) fn should_pause_on_max_attempts(&self) -> bool {
matches!(
self,
RetryPolicy::FixedDelay {
on_max_attempts: OnMaxAttempts::Pause,
..
} | RetryPolicy::Exponential {
on_max_attempts: OnMaxAttempts::Pause,
..
}
)
}
pub(crate) fn next_retry(&self, retry_info: EntryRetryInfo) -> NextRetry {
match self {
RetryPolicy::Infinite => NextRetry::Retry(None),
RetryPolicy::None => NextRetry::FailAsTerminal,
RetryPolicy::FixedDelay {
interval,
max_attempts,
max_duration,
on_max_attempts,
} => {
if max_attempts.is_some_and(|max_attempts| max_attempts <= retry_info.retry_count)
|| max_duration
.is_some_and(|max_duration| max_duration <= retry_info.retry_loop_duration)
{
// Reached either max_attempts or max_duration bound
return match on_max_attempts {
OnMaxAttempts::FailAsTerminal => NextRetry::FailAsTerminal,
OnMaxAttempts::Pause => NextRetry::Pause,
};
}
// No bound reached, we need to retry
NextRetry::Retry(*interval)
}
RetryPolicy::Exponential {
initial_interval,
factor,
max_interval,
max_attempts,
max_duration,
on_max_attempts,
} => {
if max_attempts.is_some_and(|max_attempts| max_attempts <= retry_info.retry_count)
|| max_duration
.is_some_and(|max_duration| max_duration <= retry_info.retry_loop_duration)
{
// Reached either max_attempts or max_duration bound
return match on_max_attempts {
OnMaxAttempts::FailAsTerminal => NextRetry::FailAsTerminal,
OnMaxAttempts::Pause => NextRetry::Pause,
};
}
NextRetry::Retry(Some(cmp::min(
max_interval.unwrap_or(Duration::MAX),
initial_interval.mul_f32(factor.powi((retry_info.retry_count - 1) as i32)),
)))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_exponential_policy() {
let policy = RetryPolicy::Exponential {
initial_interval: Duration::from_millis(100),
factor: 2.0,
max_interval: Some(Duration::from_millis(500)),
max_attempts: None,
max_duration: Some(Duration::from_secs(10)),
on_max_attempts: OnMaxAttempts::FailAsTerminal,
};
assert_eq!(
policy.next_retry(EntryRetryInfo {
retry_count: 2,
retry_loop_duration: Duration::from_secs(1)
}),
NextRetry::Retry(Some(Duration::from_millis(100).mul_f32(2.0)))
);
assert_eq!(
policy.next_retry(EntryRetryInfo {
retry_count: 3,
retry_loop_duration: Duration::from_secs(1)
}),
NextRetry::Retry(Some(Duration::from_millis(100).mul_f32(4.0)))
);
assert_eq!(
policy.next_retry(EntryRetryInfo {
retry_count: 4,
retry_loop_duration: Duration::from_secs(1)
}),
NextRetry::Retry(Some(Duration::from_millis(500)))
);
assert_eq!(
policy.next_retry(EntryRetryInfo {
retry_count: 4,
retry_loop_duration: Duration::from_secs(10)
}),
NextRetry::FailAsTerminal
);
}
}