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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Retry policies and tracking for batch job execution.
//!
//! Supports fixed-delay, exponential back-off, and no-retry strategies,
//! with a `RetryTracker` that records attempts and signals exhaustion.
#![allow(dead_code)]
/// Strategy used to compute delays between retry attempts.
#[derive(Debug, Clone, PartialEq)]
pub enum RetryStrategy {
/// Never retry; fail immediately on first error.
NoRetry,
/// Wait a fixed number of milliseconds between every attempt.
Fixed {
/// Delay in milliseconds between each retry.
delay_ms: u64,
},
/// Double the delay on each attempt, starting from `initial_ms`.
Exponential {
/// Starting delay in milliseconds for the first retry.
initial_ms: u64,
/// Maximum delay in milliseconds; caps exponential growth.
max_ms: u64,
},
/// Exponential backoff with decorrelated jitter to prevent thundering herd.
///
/// Uses the "full jitter" algorithm: `delay = random(0, min(max_ms, initial_ms * 2^attempt))`.
/// This spreads retries uniformly across the window, eliminating synchronised
/// retry storms when many jobs fail at the same time.
ExponentialWithJitter {
/// Starting delay in milliseconds for the first retry.
initial_ms: u64,
/// Maximum delay in milliseconds; caps exponential growth.
max_ms: u64,
/// Jitter mode.
jitter: JitterMode,
},
/// Immediate retry with no delay.
Immediate,
}
/// Jitter algorithms for exponential backoff.
///
/// All modes compute a base delay of `initial_ms * 2^attempt` (capped at `max_ms`),
/// then apply a randomisation strategy to spread retries over the window.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JitterMode {
/// Full jitter: `uniform(0, base_delay)`. Maximum spread; best for
/// preventing thundering herd when many clients retry simultaneously.
Full,
/// Equal jitter: `base_delay / 2 + uniform(0, base_delay / 2)`.
/// Guarantees at least half the base delay, limiting how aggressive the
/// retry can be while still providing good spread.
Equal,
/// Decorrelated jitter: `min(max_ms, uniform(initial_ms, prev_delay * 3))`.
/// Each retry's delay depends on the previous one rather than the attempt
/// number, producing a smoothly varying sequence.
Decorrelated,
}
/// Configuration for a retry policy.
#[derive(Debug, Clone)]
pub struct RetryConfig {
/// Retry strategy controlling delay calculation
pub strategy: RetryStrategy,
/// Maximum number of retry attempts (0 means no retries)
pub max_attempts: u32,
}
impl RetryConfig {
/// Creates a no-retry configuration.
#[must_use]
pub fn no_retry() -> Self {
Self {
strategy: RetryStrategy::NoRetry,
max_attempts: 0,
}
}
/// Creates a fixed-delay retry configuration.
#[must_use]
pub fn fixed(max_attempts: u32, delay_ms: u64) -> Self {
Self {
strategy: RetryStrategy::Fixed { delay_ms },
max_attempts,
}
}
/// Creates an exponential back-off retry configuration.
#[must_use]
pub fn exponential(max_attempts: u32, initial_ms: u64, max_ms: u64) -> Self {
Self {
strategy: RetryStrategy::Exponential { initial_ms, max_ms },
max_attempts,
}
}
/// Creates an exponential back-off with full jitter configuration.
///
/// Full jitter spreads retries uniformly across `[0, base_delay]` to prevent
/// thundering herd when many jobs fail simultaneously.
#[must_use]
pub fn exponential_full_jitter(max_attempts: u32, initial_ms: u64, max_ms: u64) -> Self {
Self {
strategy: RetryStrategy::ExponentialWithJitter {
initial_ms,
max_ms,
jitter: JitterMode::Full,
},
max_attempts,
}
}
/// Creates an exponential back-off with equal jitter configuration.
///
/// Equal jitter guarantees at least half the base delay while still providing
/// spread: `delay = base/2 + uniform(0, base/2)`.
#[must_use]
pub fn exponential_equal_jitter(max_attempts: u32, initial_ms: u64, max_ms: u64) -> Self {
Self {
strategy: RetryStrategy::ExponentialWithJitter {
initial_ms,
max_ms,
jitter: JitterMode::Equal,
},
max_attempts,
}
}
/// Creates an exponential back-off with decorrelated jitter configuration.
///
/// Decorrelated jitter computes each delay relative to the previous one,
/// producing a smooth, unpredictable retry cadence.
#[must_use]
pub fn exponential_decorrelated_jitter(
max_attempts: u32,
initial_ms: u64,
max_ms: u64,
) -> Self {
Self {
strategy: RetryStrategy::ExponentialWithJitter {
initial_ms,
max_ms,
jitter: JitterMode::Decorrelated,
},
max_attempts,
}
}
/// Returns `true` if another attempt should be made.
///
/// `attempt` is the number of attempts *already made* (0-based).
#[must_use]
pub fn should_retry(&self, attempt: u32) -> bool {
if self.strategy == RetryStrategy::NoRetry {
return false;
}
attempt < self.max_attempts
}
/// Returns the delay in milliseconds before the next attempt.
///
/// `attempt` is the number of attempts *already made* (0-based).
/// Returns 0 for `Immediate` or `NoRetry`, the fixed delay for `Fixed`,
/// and `initial_ms * 2^attempt` (capped at `max_ms`) for `Exponential`.
#[allow(clippy::cast_precision_loss)]
#[must_use]
pub fn next_delay_ms(&self, attempt: u32) -> u64 {
match &self.strategy {
RetryStrategy::NoRetry | RetryStrategy::Immediate => 0,
RetryStrategy::Fixed { delay_ms } => *delay_ms,
RetryStrategy::Exponential { initial_ms, max_ms } => {
let factor = 1u64.checked_shl(attempt).unwrap_or(u64::MAX);
let delay = initial_ms.saturating_mul(factor);
delay.min(*max_ms)
}
RetryStrategy::ExponentialWithJitter {
initial_ms,
max_ms,
jitter,
} => {
let factor = 1u64.checked_shl(attempt).unwrap_or(u64::MAX);
let base_delay = initial_ms.saturating_mul(factor).min(*max_ms);
// Apply jitter deterministically for `next_delay_ms` (upper bound).
// Actual randomisation happens at the call site; here we return the
// maximum possible delay for the given jitter mode.
match jitter {
JitterMode::Full => base_delay,
JitterMode::Equal => base_delay,
JitterMode::Decorrelated => base_delay,
}
}
}
}
}
/// Tracks retry attempts for a single job and reports exhaustion.
#[derive(Debug, Clone)]
pub struct RetryTracker {
config: RetryConfig,
attempts: u32,
}
impl RetryTracker {
/// Creates a new tracker with the given configuration.
#[must_use]
pub fn new(config: RetryConfig) -> Self {
Self {
config,
attempts: 0,
}
}
/// Records a completed attempt. Returns the delay in ms before the next
/// attempt should be made, or `None` if retries are exhausted.
pub fn record_attempt(&mut self) -> Option<u64> {
let delay = self.config.next_delay_ms(self.attempts);
self.attempts += 1;
if self.config.should_retry(self.attempts) || self.attempts <= self.config.max_attempts {
// still within limit — but we already incremented, so check:
if self.attempts <= self.config.max_attempts
&& self.config.strategy != RetryStrategy::NoRetry
{
return Some(delay);
}
}
None
}
/// Returns `true` if no more retries are available.
#[must_use]
pub fn is_exhausted(&self) -> bool {
!self.config.should_retry(self.attempts)
}
/// Returns the number of attempts made so far.
#[must_use]
pub fn attempt_count(&self) -> u32 {
self.attempts
}
/// Resets the tracker back to zero attempts.
pub fn reset(&mut self) {
self.attempts = 0;
}
}
// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_no_retry_should_retry_false() {
let cfg = RetryConfig::no_retry();
assert!(!cfg.should_retry(0));
}
#[test]
fn test_fixed_should_retry_within_limit() {
let cfg = RetryConfig::fixed(3, 100);
assert!(cfg.should_retry(0));
assert!(cfg.should_retry(2));
assert!(!cfg.should_retry(3));
}
#[test]
fn test_fixed_next_delay() {
let cfg = RetryConfig::fixed(3, 250);
assert_eq!(cfg.next_delay_ms(0), 250);
assert_eq!(cfg.next_delay_ms(2), 250);
}
#[test]
fn test_immediate_next_delay_zero() {
let cfg = RetryConfig {
strategy: RetryStrategy::Immediate,
max_attempts: 2,
};
assert_eq!(cfg.next_delay_ms(0), 0);
}
#[test]
fn test_exponential_delay_doubles() {
let cfg = RetryConfig::exponential(5, 100, 10_000);
assert_eq!(cfg.next_delay_ms(0), 100); // 100 * 2^0
assert_eq!(cfg.next_delay_ms(1), 200); // 100 * 2^1
assert_eq!(cfg.next_delay_ms(2), 400); // 100 * 2^2
assert_eq!(cfg.next_delay_ms(3), 800); // 100 * 2^3
}
#[test]
fn test_exponential_delay_capped() {
let cfg = RetryConfig::exponential(10, 1_000, 5_000);
assert_eq!(cfg.next_delay_ms(10), 5_000); // capped
}
#[test]
fn test_no_retry_delay_zero() {
let cfg = RetryConfig::no_retry();
assert_eq!(cfg.next_delay_ms(0), 0);
}
#[test]
fn test_tracker_initial_not_exhausted() {
let tracker = RetryTracker::new(RetryConfig::fixed(3, 100));
assert!(!tracker.is_exhausted());
}
#[test]
fn test_tracker_exhausted_after_max_attempts() {
let mut tracker = RetryTracker::new(RetryConfig::fixed(2, 50));
tracker.record_attempt();
tracker.record_attempt();
assert!(tracker.is_exhausted());
}
#[test]
fn test_tracker_attempt_count() {
let mut tracker = RetryTracker::new(RetryConfig::fixed(5, 10));
tracker.record_attempt();
tracker.record_attempt();
assert_eq!(tracker.attempt_count(), 2);
}
#[test]
fn test_tracker_no_retry_immediately_exhausted() {
let tracker = RetryTracker::new(RetryConfig::no_retry());
assert!(tracker.is_exhausted());
}
#[test]
fn test_tracker_reset() {
let mut tracker = RetryTracker::new(RetryConfig::fixed(3, 100));
tracker.record_attempt();
tracker.record_attempt();
tracker.record_attempt();
assert!(tracker.is_exhausted());
tracker.reset();
assert!(!tracker.is_exhausted());
assert_eq!(tracker.attempt_count(), 0);
}
}