openzeppelin_relayer/jobs/
retry_backoff.rs

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
//! Retry policy implementation with exponential backoff for job processing.
//!
//! This module provides a configurable retry mechanism with exponential backoff
//! for handling transient failures in job processing.
//!
//! # Example
//! ```rust
//! use crate::jobs::workers::retry_backoff::BackoffRetryPolicy;
//! use std::time::Duration;
//!
//! let policy = BackoffRetryPolicy {
//!     retries: 5,
//!     initial_backoff: Duration::from_secs(1),
//!     multiplier: 2.0,
//!     max_backoff: Duration::from_secs(60),
//! };
//! ```

use apalis::prelude::*;
use std::time::Duration;
use tokio::time::{sleep, Sleep};
use tower::retry::Policy;

type Req<T, Ctx> = Request<T, Ctx>;
type Err = Error;

/// A retry policy that implements exponential backoff.
///
/// This policy will retry failed operations with increasing delays between attempts,
/// using an exponential backoff algorithm with a configurable multiplier.
///
/// # Fields
///
/// * `retries` - Maximum number of retry attempts
/// * `initial_backoff` - Initial delay duration before first retry
/// * `multiplier` - Factor by which the delay increases after each attempt
/// * `max_backoff` - Maximum delay duration between retries
///
/// # Example
///
/// ```rust
/// let policy = BackoffRetryPolicy {
///     retries: 5,
///     initial_backoff: Duration::from_secs(1),
///     multiplier: 2.0,
///     max_backoff: Duration::from_secs(60),
/// };
/// ```
#[derive(Clone, Debug)]
pub struct BackoffRetryPolicy {
    /// Maximum number of retry attempts
    pub retries: usize,
    /// Initial delay duration before first retry
    pub initial_backoff: Duration,
    /// Factor by which the delay increases after each attempt
    pub multiplier: f64,
    /// Maximum delay duration between retries
    pub max_backoff: Duration,
}

/// Default configuration for retry policy
impl Default for BackoffRetryPolicy {
    fn default() -> Self {
        Self {
            retries: 5,
            initial_backoff: Duration::from_millis(1000),
            multiplier: 1.5,
            max_backoff: Duration::from_secs(60),
        }
    }
}

impl BackoffRetryPolicy {
    fn backoff_duration(&self, attempt: usize) -> Duration {
        let backoff =
            self.initial_backoff.as_millis() as f64 * self.multiplier.powi(attempt as i32);
        Duration::from_millis(backoff.min(self.max_backoff.as_millis() as f64) as u64)
    }
}

impl<T, Res, Ctx> Policy<Req<T, Ctx>, Res, Err> for BackoffRetryPolicy
where
    T: Clone,
    Ctx: Clone,
{
    type Future = Sleep;

    fn retry(
        &mut self,
        req: &mut Req<T, Ctx>,
        result: &mut Result<Res, Err>,
    ) -> Option<Self::Future> {
        let attempt = req.parts.attempt.current();

        match result {
            Ok(_) => None,
            Err(_) if (self.retries - attempt > 0) => Some(sleep(self.backoff_duration(attempt))),
            Err(_) => None,
        }
    }

    fn clone_request(&mut self, req: &Req<T, Ctx>) -> Option<Req<T, Ctx>> {
        let req = req.clone();
        req.parts.attempt.increment();
        Some(req)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Clone)]
    struct TestJob;

    #[tokio::test]
    async fn test_backoff_duration_calculation() {
        let policy = BackoffRetryPolicy {
            retries: 5,
            initial_backoff: Duration::from_secs(1),
            multiplier: 2.0,
            max_backoff: Duration::from_secs(60),
        };

        // Check first few backoff durations
        assert_eq!(policy.backoff_duration(0), Duration::from_secs(1));
        assert_eq!(policy.backoff_duration(1), Duration::from_secs(2));
        assert_eq!(policy.backoff_duration(2), Duration::from_secs(4));
        assert_eq!(policy.backoff_duration(3), Duration::from_secs(8));

        // Test max backoff limit
        let policy = BackoffRetryPolicy {
            retries: 10,
            initial_backoff: Duration::from_secs(10),
            multiplier: 3.0,
            max_backoff: Duration::from_secs(60),
        };

        // This would be 10 * 3^3 = 270 seconds, but should be capped at 60
        assert_eq!(policy.backoff_duration(3), Duration::from_secs(60));
    }

    #[tokio::test]
    async fn test_retry_policy_success() {
        let mut policy = BackoffRetryPolicy::default();
        let job = TestJob;
        let ctx = ();
        let mut req = Request::new_with_ctx(job, ctx);
        let mut result: Result<(), Err> = Ok(());

        // Should return None for successful results
        assert!(policy.retry(&mut req, &mut result).is_none());
    }

    #[tokio::test]
    async fn test_retry_policy_failure_with_retries() {
        let mut policy = BackoffRetryPolicy {
            retries: 3,
            initial_backoff: Duration::from_millis(10),
            multiplier: 2.0,
            max_backoff: Duration::from_secs(1),
        };

        let job = TestJob;
        let ctx = ();
        let mut req = Request::new_with_ctx(job, ctx);
        let mut result: Result<(), Err> =
            Err(Error::from(Box::new(std::io::Error::other("Test error"))
                as Box<dyn std::error::Error + Send + Sync>));

        // First attempt (0) should return Some(Sleep) since retries > 0
        assert!(policy.retry(&mut req, &mut result).is_some());

        // Simulate first retry
        req.parts.attempt.increment();
        assert!(policy.retry(&mut req, &mut result).is_some());

        // Simulate second retry
        req.parts.attempt.increment();
        assert!(policy.retry(&mut req, &mut result).is_some());

        // Simulate third retry - should be the last one
        req.parts.attempt.increment();
        assert!(policy.retry(&mut req, &mut result).is_none());
    }

    #[tokio::test]
    async fn test_clone_request() {
        let mut policy: BackoffRetryPolicy = BackoffRetryPolicy::default();
        let job = TestJob;
        let ctx = ();
        let req: Request<TestJob, ()> = Request::new_with_ctx(job, ctx);

        // Original request attempt should be 0
        assert_eq!(req.parts.attempt.current(), 0);

        // Cloned request should have attempt incremented to 1
        let cloned_req =
            <BackoffRetryPolicy as Policy<Request<TestJob, ()>, (), Error>>::clone_request(
                &mut policy,
                &req,
            )
            .unwrap();
        let cloned_req_attempt = cloned_req.parts.attempt.current();
        assert_eq!(cloned_req_attempt, 1);
    }

    #[test]
    fn test_default_policy() {
        let policy = BackoffRetryPolicy::default();

        assert_eq!(policy.retries, 5);
        assert_eq!(policy.initial_backoff, Duration::from_millis(1000));
        assert_eq!(policy.multiplier, 1.5);
        assert_eq!(policy.max_backoff, Duration::from_secs(60));
    }

    #[test]
    fn test_zero_initial_backoff() {
        let policy = BackoffRetryPolicy {
            retries: 3,
            initial_backoff: Duration::from_millis(0),
            multiplier: 2.0,
            max_backoff: Duration::from_secs(60),
        };

        // With zero initial backoff, all durations should be zero
        assert_eq!(policy.backoff_duration(0), Duration::from_millis(0));
        assert_eq!(policy.backoff_duration(1), Duration::from_millis(0));
        assert_eq!(policy.backoff_duration(2), Duration::from_millis(0));
    }

    #[test]
    fn test_multiplier_one() {
        let policy = BackoffRetryPolicy {
            retries: 3,
            initial_backoff: Duration::from_millis(500),
            multiplier: 1.0,
            max_backoff: Duration::from_secs(60),
        };

        // With multiplier of 1.0, all durations should be the same as initial
        assert_eq!(policy.backoff_duration(0), Duration::from_millis(500));
        assert_eq!(policy.backoff_duration(1), Duration::from_millis(500));
        assert_eq!(policy.backoff_duration(2), Duration::from_millis(500));
    }

    #[test]
    fn test_multiplier_less_than_one() {
        let policy = BackoffRetryPolicy {
            retries: 3,
            initial_backoff: Duration::from_millis(1000),
            multiplier: 0.5,
            max_backoff: Duration::from_secs(60),
        };

        // With multiplier < 1.0, each duration should be less than the previous
        assert_eq!(policy.backoff_duration(0), Duration::from_millis(1000));
        assert_eq!(policy.backoff_duration(1), Duration::from_millis(500));
        assert_eq!(policy.backoff_duration(2), Duration::from_millis(250));
    }

    #[tokio::test]
    async fn test_retry_policy_exhausted_retries() {
        let mut policy = BackoffRetryPolicy {
            retries: 0, // No retries allowed
            initial_backoff: Duration::from_millis(10),
            multiplier: 2.0,
            max_backoff: Duration::from_secs(1),
        };

        let job = TestJob;
        let ctx = ();
        let mut req = Request::new_with_ctx(job, ctx);
        let mut result: Result<(), Err> =
            Err(Error::from(Box::new(std::io::Error::other("Test error"))
                as Box<dyn std::error::Error + Send + Sync>));

        // Should return None immediately because retries=0
        assert!(policy.retry(&mut req, &mut result).is_none());
    }

    #[tokio::test]
    async fn test_retry_policy_large_max_backoff() {
        let policy = BackoffRetryPolicy {
            retries: 10,
            initial_backoff: Duration::from_millis(100),
            multiplier: 10.0,                               // Large multiplier
            max_backoff: Duration::from_secs(24 * 60 * 60), // 24 hours
        };

        // Even with a large multiplier, we should never exceed max_backoff
        assert!(policy.backoff_duration(10) <= Duration::from_secs(24 * 60 * 60));
    }
}