33//! The `RetryPolicy` struct defines the configuration for the retry behavior, including the maximum
44//! number of retries, initial delay, maximum delay, and jitter.
55//!
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
77//! 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 .
1010
1111#[ cfg( feature = "experimental_async_runtime" ) ]
1212use opentelemetry:: otel_warn;
@@ -69,45 +69,7 @@ fn generate_jitter(max_jitter: u64) -> u64 {
6969 nanos as u64 % ( max_jitter + 1 )
7070}
7171
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.
11173///
11274/// This function provides sophisticated retry behavior by classifying errors
11375/// and honoring server-provided throttling hints (e.g., HTTP Retry-After, gRPC RetryInfo).
12587/// A `Result` containing the operation's result or an error if max retries are reached
12688/// or a non-retryable error occurs.
12789#[ 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 > (
12991 runtime : R ,
13092 policy : RetryPolicy ,
13193 error_classifier : C ,
@@ -186,9 +148,10 @@ where
186148/// No-op retry function for when experimental_async_runtime is not enabled.
187149/// This function will execute the operation exactly once without any retries.
188150#[ 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 > (
190152 _runtime : R ,
191153 _policy : RetryPolicy ,
154+ _error_classifier : C ,
192155 _operation_name : & str ,
193156 mut operation : F ,
194157) -> Result < T , E >
@@ -227,7 +190,7 @@ mod tests {
227190 jitter_ms : 100 ,
228191 } ;
229192
230- let result = retry_with_exponential_backoff ( runtime, policy, "test_operation" , || {
193+ let result = retry_with_backoff ( runtime, policy, |_ : & ( ) | RetryErrorType :: Retryable , "test_operation" , || {
231194 Box :: pin ( async { Ok :: < _ , ( ) > ( "success" ) } )
232195 } )
233196 . await ;
@@ -248,7 +211,7 @@ mod tests {
248211
249212 let attempts = AtomicUsize :: new ( 0 ) ;
250213
251- let result = retry_with_exponential_backoff ( runtime, policy, "test_operation" , || {
214+ let result = retry_with_backoff ( runtime, policy, |_ : & & str | RetryErrorType :: Retryable , "test_operation" , || {
252215 let attempt = attempts. fetch_add ( 1 , Ordering :: SeqCst ) ;
253216 Box :: pin ( async move {
254217 if attempt < 2 {
@@ -277,7 +240,7 @@ mod tests {
277240
278241 let attempts = AtomicUsize :: new ( 0 ) ;
279242
280- let result = retry_with_exponential_backoff ( runtime, policy, "test_operation" , || {
243+ let result = retry_with_backoff ( runtime, policy, |_ : & & str | RetryErrorType :: Retryable , "test_operation" , || {
281244 attempts. fetch_add ( 1 , Ordering :: SeqCst ) ;
282245 Box :: pin ( async { Err :: < ( ) , _ > ( "error" ) } ) // Always fail
283246 } )
@@ -300,7 +263,7 @@ mod tests {
300263
301264 let result = timeout (
302265 Duration :: from_secs ( 1 ) ,
303- retry_with_exponential_backoff ( runtime, policy, "test_operation" , || {
266+ retry_with_backoff ( runtime, policy, |_ : & & str | RetryErrorType :: Retryable , "test_operation" , || {
304267 Box :: pin ( async { Err :: < ( ) , _ > ( "error" ) } ) // Always fail
305268 } ) ,
306269 )
@@ -337,7 +300,7 @@ mod tests {
337300 // Classifier that returns non-retryable
338301 let classifier = |_: & ( ) | RetryErrorType :: NonRetryable ;
339302
340- let result = retry_with_exponential_backoff_classified (
303+ let result = retry_with_backoff (
341304 runtime,
342305 policy,
343306 classifier,
@@ -368,7 +331,7 @@ mod tests {
368331 // Classifier that returns retryable
369332 let classifier = |_: & ( ) | RetryErrorType :: Retryable ;
370333
371- let result = retry_with_exponential_backoff_classified (
334+ let result = retry_with_backoff (
372335 runtime,
373336 policy,
374337 classifier,
@@ -407,7 +370,7 @@ mod tests {
407370
408371 let start_time = std:: time:: Instant :: now ( ) ;
409372
410- let result = retry_with_exponential_backoff_classified (
373+ let result = retry_with_backoff (
411374 runtime,
412375 policy,
413376 classifier,
@@ -447,7 +410,7 @@ mod tests {
447410 // Classifier that returns retryable
448411 let classifier = |_: & ( ) | RetryErrorType :: Retryable ;
449412
450- let result = retry_with_exponential_backoff_classified (
413+ let result = retry_with_backoff (
451414 runtime,
452415 policy,
453416 classifier,
@@ -482,7 +445,7 @@ mod tests {
482445 _ => RetryErrorType :: Retryable ,
483446 } ;
484447
485- let result = retry_with_exponential_backoff_classified (
448+ let result = retry_with_backoff (
486449 runtime,
487450 policy,
488451 classifier,
0 commit comments