3
3
//! The `RetryPolicy` struct defines the configuration for the retry behavior, including the maximum
4
4
//! number of retries, initial delay, maximum delay, and jitter.
5
5
//!
6
- //! The `retry_with_exponential_backoff ` function retries the given operation according to the
6
+ //! The `retry_with_backoff ` function retries the given operation according to the
7
7
//! specified retry policy, using exponential backoff and jitter to determine the delay between
8
- //! retries. The function logs errors and retries the operation until it succeeds or the maximum
9
- //! number of retries is reached .
8
+ //! retries. The function uses error classification to determine retry behavior and can honor
9
+ //! server-provided throttling hints .
10
10
11
11
#[ cfg( feature = "experimental_async_runtime" ) ]
12
12
use opentelemetry:: otel_warn;
@@ -69,45 +69,7 @@ fn generate_jitter(max_jitter: u64) -> u64 {
69
69
nanos as u64 % ( max_jitter + 1 )
70
70
}
71
71
72
- /// Retries the given operation with exponential backoff and jitter.
73
- ///
74
- /// # Arguments
75
- ///
76
- /// * `runtime` - The async runtime to use for delays.
77
- /// * `policy` - The retry policy configuration.
78
- /// * `operation_name` - The name of the operation being retried.
79
- /// * `operation` - The operation to be retried.
80
- ///
81
- /// # Returns
82
- ///
83
- /// A `Result` containing the operation's result or an error if the maximum retries are reached.
84
- #[ cfg( feature = "experimental_async_runtime" ) ]
85
- pub async fn retry_with_exponential_backoff < R , F , Fut , T , E > (
86
- runtime : R ,
87
- policy : RetryPolicy ,
88
- operation_name : & str ,
89
- operation : F ,
90
- ) -> Result < T , E >
91
- where
92
- R : Runtime ,
93
- F : FnMut ( ) -> Fut ,
94
- E : std:: fmt:: Debug ,
95
- Fut : Future < Output = Result < T , E > > ,
96
- {
97
- // Use a simple classifier that treats all errors as retryable
98
- let simple_classifier = |_: & E | RetryErrorType :: Retryable ;
99
-
100
- retry_with_exponential_backoff_classified (
101
- runtime,
102
- policy,
103
- simple_classifier,
104
- operation_name,
105
- operation,
106
- )
107
- . await
108
- }
109
-
110
- /// Enhanced retry with exponential backoff, jitter, and error classification.
72
+ /// Retries the given operation with exponential backoff, jitter, and error classification.
111
73
///
112
74
/// This function provides sophisticated retry behavior by classifying errors
113
75
/// and honoring server-provided throttling hints (e.g., HTTP Retry-After, gRPC RetryInfo).
125
87
/// A `Result` containing the operation's result or an error if max retries are reached
126
88
/// or a non-retryable error occurs.
127
89
#[ cfg( feature = "experimental_async_runtime" ) ]
128
- pub async fn retry_with_exponential_backoff_classified < R , F , Fut , T , E , C > (
90
+ pub async fn retry_with_backoff < R , F , Fut , T , E , C > (
129
91
runtime : R ,
130
92
policy : RetryPolicy ,
131
93
error_classifier : C ,
@@ -186,9 +148,10 @@ where
186
148
/// No-op retry function for when experimental_async_runtime is not enabled.
187
149
/// This function will execute the operation exactly once without any retries.
188
150
#[ cfg( not( feature = "experimental_async_runtime" ) ) ]
189
- pub async fn retry_with_exponential_backoff < R , F , Fut , T , E > (
151
+ pub async fn retry_with_backoff < R , F , Fut , T , E , C > (
190
152
_runtime : R ,
191
153
_policy : RetryPolicy ,
154
+ _error_classifier : C ,
192
155
_operation_name : & str ,
193
156
mut operation : F ,
194
157
) -> Result < T , E >
@@ -227,7 +190,7 @@ mod tests {
227
190
jitter_ms : 100 ,
228
191
} ;
229
192
230
- let result = retry_with_exponential_backoff ( runtime, policy, "test_operation" , || {
193
+ let result = retry_with_backoff ( runtime, policy, |_ : & ( ) | RetryErrorType :: Retryable , "test_operation" , || {
231
194
Box :: pin ( async { Ok :: < _ , ( ) > ( "success" ) } )
232
195
} )
233
196
. await ;
@@ -248,7 +211,7 @@ mod tests {
248
211
249
212
let attempts = AtomicUsize :: new ( 0 ) ;
250
213
251
- let result = retry_with_exponential_backoff ( runtime, policy, "test_operation" , || {
214
+ let result = retry_with_backoff ( runtime, policy, |_ : & & str | RetryErrorType :: Retryable , "test_operation" , || {
252
215
let attempt = attempts. fetch_add ( 1 , Ordering :: SeqCst ) ;
253
216
Box :: pin ( async move {
254
217
if attempt < 2 {
@@ -277,7 +240,7 @@ mod tests {
277
240
278
241
let attempts = AtomicUsize :: new ( 0 ) ;
279
242
280
- let result = retry_with_exponential_backoff ( runtime, policy, "test_operation" , || {
243
+ let result = retry_with_backoff ( runtime, policy, |_ : & & str | RetryErrorType :: Retryable , "test_operation" , || {
281
244
attempts. fetch_add ( 1 , Ordering :: SeqCst ) ;
282
245
Box :: pin ( async { Err :: < ( ) , _ > ( "error" ) } ) // Always fail
283
246
} )
@@ -300,7 +263,7 @@ mod tests {
300
263
301
264
let result = timeout (
302
265
Duration :: from_secs ( 1 ) ,
303
- retry_with_exponential_backoff ( runtime, policy, "test_operation" , || {
266
+ retry_with_backoff ( runtime, policy, |_ : & & str | RetryErrorType :: Retryable , "test_operation" , || {
304
267
Box :: pin ( async { Err :: < ( ) , _ > ( "error" ) } ) // Always fail
305
268
} ) ,
306
269
)
@@ -337,7 +300,7 @@ mod tests {
337
300
// Classifier that returns non-retryable
338
301
let classifier = |_: & ( ) | RetryErrorType :: NonRetryable ;
339
302
340
- let result = retry_with_exponential_backoff_classified (
303
+ let result = retry_with_backoff (
341
304
runtime,
342
305
policy,
343
306
classifier,
@@ -368,7 +331,7 @@ mod tests {
368
331
// Classifier that returns retryable
369
332
let classifier = |_: & ( ) | RetryErrorType :: Retryable ;
370
333
371
- let result = retry_with_exponential_backoff_classified (
334
+ let result = retry_with_backoff (
372
335
runtime,
373
336
policy,
374
337
classifier,
@@ -407,7 +370,7 @@ mod tests {
407
370
408
371
let start_time = std:: time:: Instant :: now ( ) ;
409
372
410
- let result = retry_with_exponential_backoff_classified (
373
+ let result = retry_with_backoff (
411
374
runtime,
412
375
policy,
413
376
classifier,
@@ -447,7 +410,7 @@ mod tests {
447
410
// Classifier that returns retryable
448
411
let classifier = |_: & ( ) | RetryErrorType :: Retryable ;
449
412
450
- let result = retry_with_exponential_backoff_classified (
413
+ let result = retry_with_backoff (
451
414
runtime,
452
415
policy,
453
416
classifier,
@@ -482,7 +445,7 @@ mod tests {
482
445
_ => RetryErrorType :: Retryable ,
483
446
} ;
484
447
485
- let result = retry_with_exponential_backoff_classified (
448
+ let result = retry_with_backoff (
486
449
runtime,
487
450
policy,
488
451
classifier,
0 commit comments