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
//! Retry utilities with exponential backoff for atomic_websocket.
//!
//! This module provides reusable retry logic with configurable exponential backoff,
//! reducing code duplication across the library.
use std::time::Duration;
use tokio::time::sleep;
/// Configuration for exponential backoff retry behavior.
#[derive(Clone, Debug)]
pub struct ExponentialBackoff {
/// Current backoff duration
current: Duration,
/// Initial backoff duration (kept for reset functionality)
#[allow(dead_code)]
initial: Duration,
/// Maximum backoff duration
max: Duration,
/// Current retry count
count: u32,
/// Maximum number of retries
max_count: u32,
}
impl ExponentialBackoff {
/// Creates a new ExponentialBackoff configuration.
///
/// # Arguments
///
/// * `initial_ms` - Initial backoff in milliseconds
/// * `max_secs` - Maximum backoff in seconds
/// * `max_count` - Maximum number of retry attempts
///
/// # Returns
///
/// A new ExponentialBackoff instance
pub fn new(initial_ms: u64, max_secs: u64, max_count: u32) -> Self {
let initial = Duration::from_millis(initial_ms);
Self {
current: initial,
initial,
max: Duration::from_secs(max_secs),
count: 0,
max_count,
}
}
/// Creates a default ExponentialBackoff (50ms initial, 1s max, 5 retries).
pub fn default_retry() -> Self {
Self::new(50, 1, 5)
}
/// Waits for the current backoff duration and increments the counter.
///
/// # Returns
///
/// `true` if more retries are available, `false` if max retries reached
pub async fn wait(&mut self) -> bool {
if self.count >= self.max_count {
return false;
}
sleep(self.current).await;
self.current = std::cmp::min(self.current * 2, self.max);
self.count += 1;
true
}
/// Resets the backoff to initial state.
#[allow(dead_code)]
pub fn reset(&mut self) {
self.current = self.initial;
self.count = 0;
}
/// Returns the current retry count.
#[allow(dead_code)]
pub fn count(&self) -> u32 {
self.count
}
/// Returns whether max retries have been reached.
#[allow(dead_code)]
pub fn is_exhausted(&self) -> bool {
self.count >= self.max_count
}
}
impl Default for ExponentialBackoff {
fn default() -> Self {
Self::default_retry()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_exponential_backoff_count() {
let mut backoff = ExponentialBackoff::new(10, 1, 3);
assert_eq!(backoff.count(), 0);
assert!(!backoff.is_exhausted());
assert!(backoff.wait().await);
assert_eq!(backoff.count(), 1);
assert!(backoff.wait().await);
assert_eq!(backoff.count(), 2);
assert!(backoff.wait().await);
assert_eq!(backoff.count(), 3);
assert!(!backoff.wait().await);
assert!(backoff.is_exhausted());
}
#[test]
fn test_reset() {
let mut backoff = ExponentialBackoff::new(10, 1, 3);
backoff.count = 2;
backoff.current = Duration::from_secs(1);
backoff.reset();
assert_eq!(backoff.count(), 0);
assert_eq!(backoff.current, Duration::from_millis(10));
}
}