fluxus_core/error_handling/
retry_strategy.rs1use std::time::Duration;
2
3#[derive(Debug, Clone)]
5pub enum RetryStrategy {
6 NoRetry,
8 Fixed {
10 delay: Duration,
11 max_attempts: usize,
12 },
13 ExponentialBackoff {
15 initial_delay: Duration,
16 max_delay: Duration,
17 max_attempts: usize,
18 multiplier: f64,
19 },
20}
21
22impl RetryStrategy {
23 pub fn fixed(delay: Duration, max_attempts: usize) -> Self {
25 Self::Fixed {
26 delay,
27 max_attempts,
28 }
29 }
30
31 pub fn exponential(
33 initial_delay: Duration,
34 max_delay: Duration,
35 max_attempts: usize,
36 multiplier: f64,
37 ) -> Self {
38 Self::ExponentialBackoff {
39 initial_delay,
40 max_delay,
41 max_attempts,
42 multiplier,
43 }
44 }
45
46 pub fn get_delay(&self, attempt: usize) -> Option<Duration> {
48 match self {
49 Self::NoRetry => None,
50 Self::Fixed {
51 delay,
52 max_attempts,
53 } => {
54 if attempt < *max_attempts {
55 Some(*delay)
56 } else {
57 None
58 }
59 }
60 Self::ExponentialBackoff {
61 initial_delay,
62 max_delay,
63 max_attempts,
64 multiplier,
65 } => {
66 if attempt < *max_attempts {
67 let delay = Duration::from_secs_f64(
68 initial_delay.as_secs_f64() * multiplier.powi(attempt as i32),
69 );
70 Some(delay.min(*max_delay))
71 } else {
72 None
73 }
74 }
75 }
76 }
77}