prism3_retry/simple_config.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025.
4 * 3-Prism Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! # Simple Retry Configuration Implementation
10//!
11//! Provides the simplest retry configuration implementation, with all fields stored directly in the struct.
12//!
13//! # Author
14//!
15//! Haixing Hu
16
17use super::config::RetryConfig;
18use super::delay_strategy::RetryDelayStrategy;
19use std::time::Duration;
20
21/// Simple retry configuration implementation
22///
23/// This is the simplest `RetryConfig` implementation, with all configuration fields stored directly in the struct,
24/// without depending on an external configuration system. Suitable for scenarios requiring direct control over all configurations.
25///
26/// # Features
27///
28/// - **Simple and Direct**: All fields stored directly, no configuration system needed
29/// - **Type Safe**: Use Rust's type system to ensure correct configuration
30/// - **High Performance**: No configuration queries, direct field access
31/// - **Easy to Understand**: Clear structure, simple code
32///
33/// # Example
34///
35/// ```rust
36/// use prism3_retry::{SimpleRetryConfig, RetryConfig, RetryDelayStrategy};
37/// use std::time::Duration;
38///
39/// let mut config = SimpleRetryConfig::new();
40/// config
41/// .set_max_attempts(3)
42/// .set_max_duration(Some(Duration::from_secs(30)))
43/// .set_fixed_delay_strategy(Duration::from_secs(1));
44///
45/// assert_eq!(config.max_attempts(), 3);
46/// assert_eq!(config.max_duration(), Some(Duration::from_secs(30)));
47/// ```
48///
49/// # Author
50///
51/// Haixing Hu
52#[derive(Debug, Clone)]
53pub struct SimpleRetryConfig {
54 /// Maximum number of attempts
55 max_attempts: u32,
56 /// Delay strategy
57 delay_strategy: RetryDelayStrategy,
58 /// Jitter factor
59 jitter_factor: f64,
60 /// Maximum duration
61 max_duration: Option<Duration>,
62 /// Single operation timeout
63 operation_timeout: Option<Duration>,
64}
65
66impl SimpleRetryConfig {
67 /// Create a new `SimpleRetryConfig` instance with default configuration
68 ///
69 /// # Returns
70 ///
71 /// Returns a new `SimpleRetryConfig` instance with default configuration
72 ///
73 /// # Example
74 ///
75 /// ```rust
76 /// use prism3_retry::{SimpleRetryConfig, RetryConfig};
77 ///
78 /// let config = SimpleRetryConfig::new();
79 /// assert_eq!(config.max_attempts(), 5);
80 /// ```
81 pub fn new() -> Self {
82 Self {
83 max_attempts: Self::DEFAULT_MAX_ATTEMPTS,
84 delay_strategy: Self::DEFAULT_DELAY_STRATEGY,
85 jitter_factor: Self::DEFAULT_JITTER_FACTOR,
86 max_duration: None,
87 operation_timeout: None,
88 }
89 }
90
91 /// Create a new `SimpleRetryConfig` instance with specified parameters
92 ///
93 /// # Parameters
94 ///
95 /// * `max_attempts` - Maximum number of attempts
96 /// * `delay_strategy` - Delay strategy
97 /// * `jitter_factor` - Jitter factor
98 /// * `max_duration` - Maximum duration
99 /// * `operation_timeout` - Single operation timeout
100 ///
101 /// # Returns
102 ///
103 /// Returns a new `SimpleRetryConfig` instance with the specified parameters
104 ///
105 /// # Example
106 ///
107 /// ```rust
108 /// use prism3_retry::{SimpleRetryConfig, RetryDelayStrategy};
109 /// use std::time::Duration;
110 ///
111 /// let config = SimpleRetryConfig::with_params(
112 /// 3,
113 /// RetryDelayStrategy::fixed(Duration::from_secs(1)),
114 /// 0.1,
115 /// Some(Duration::from_secs(30)),
116 /// Some(Duration::from_secs(5)),
117 /// );
118 /// ```
119 pub fn with_params(
120 max_attempts: u32,
121 delay_strategy: RetryDelayStrategy,
122 jitter_factor: f64,
123 max_duration: Option<Duration>,
124 operation_timeout: Option<Duration>,
125 ) -> Self {
126 Self {
127 max_attempts,
128 delay_strategy,
129 jitter_factor,
130 max_duration,
131 operation_timeout,
132 }
133 }
134}
135
136impl Default for SimpleRetryConfig {
137 fn default() -> Self {
138 Self::new()
139 }
140}
141
142impl RetryConfig for SimpleRetryConfig {
143 fn max_attempts(&self) -> u32 {
144 self.max_attempts
145 }
146
147 fn set_max_attempts(&mut self, max_attempts: u32) -> &mut Self {
148 self.max_attempts = max_attempts;
149 self
150 }
151
152 fn max_duration(&self) -> Option<Duration> {
153 self.max_duration
154 }
155
156 fn set_max_duration(&mut self, max_duration: Option<Duration>) -> &mut Self {
157 self.max_duration = max_duration;
158 self
159 }
160
161 fn operation_timeout(&self) -> Option<Duration> {
162 self.operation_timeout
163 }
164
165 fn set_operation_timeout(&mut self, timeout: Option<Duration>) -> &mut Self {
166 self.operation_timeout = timeout;
167 self
168 }
169
170 fn delay_strategy(&self) -> RetryDelayStrategy {
171 self.delay_strategy.clone()
172 }
173
174 fn set_delay_strategy(&mut self, delay_strategy: RetryDelayStrategy) -> &mut Self {
175 self.delay_strategy = delay_strategy;
176 self
177 }
178
179 fn jitter_factor(&self) -> f64 {
180 self.jitter_factor
181 }
182
183 fn set_jitter_factor(&mut self, jitter_factor: f64) -> &mut Self {
184 self.jitter_factor = jitter_factor;
185 self
186 }
187}