8
8
//! retries. The function uses error classification to determine retry behavior and can honor
9
9
//! server-provided throttling hints.
10
10
11
- #[ cfg( any( feature = "experimental-grpc-retry" , feature = "experimental-http-retry" ) ) ]
11
+ #[ cfg( any(
12
+ feature = "experimental-grpc-retry" ,
13
+ feature = "experimental-http-retry"
14
+ ) ) ]
12
15
use opentelemetry:: otel_warn;
13
- #[ cfg( any( feature = "experimental-grpc-retry" , feature = "experimental-http-retry" ) ) ]
16
+ #[ cfg( any(
17
+ feature = "experimental-grpc-retry" ,
18
+ feature = "experimental-http-retry"
19
+ ) ) ]
14
20
use std:: future:: Future ;
15
- use std:: hash:: DefaultHasher ;
21
+ #[ cfg( any(
22
+ feature = "experimental-grpc-retry" ,
23
+ feature = "experimental-http-retry"
24
+ ) ) ]
25
+ use std:: hash:: { DefaultHasher , Hasher } ;
16
26
use std:: time:: Duration ;
17
- #[ cfg( any( feature = "experimental-grpc-retry" , feature = "experimental-http-retry" ) ) ]
27
+ #[ cfg( any(
28
+ feature = "experimental-grpc-retry" ,
29
+ feature = "experimental-http-retry"
30
+ ) ) ]
18
31
use std:: time:: SystemTime ;
19
32
20
- #[ cfg( any( feature = "experimental-grpc-retry" , feature = "experimental-http-retry" ) ) ]
33
+ #[ cfg( any(
34
+ feature = "experimental-grpc-retry" ,
35
+ feature = "experimental-http-retry"
36
+ ) ) ]
21
37
use opentelemetry_sdk:: runtime:: Runtime ;
22
38
23
39
/// Classification of errors for retry purposes.
@@ -47,11 +63,17 @@ pub struct RetryPolicy {
47
63
48
64
/// A runtime stub for when experimental_async_runtime is not enabled.
49
65
/// This allows retry policy to be configured but no actual retries occur.
50
- #[ cfg( not( any( feature = "experimental-grpc-retry" , feature = "experimental-http-retry" ) ) ) ]
66
+ #[ cfg( not( any(
67
+ feature = "experimental-grpc-retry" ,
68
+ feature = "experimental-http-retry"
69
+ ) ) ) ]
51
70
#[ derive( Debug , Clone , Default ) ]
52
71
pub struct NoOpRuntime ;
53
72
54
- #[ cfg( not( any( feature = "experimental-grpc-retry" , feature = "experimental-http-retry" ) ) ) ]
73
+ #[ cfg( not( any(
74
+ feature = "experimental-grpc-retry" ,
75
+ feature = "experimental-http-retry"
76
+ ) ) ) ]
55
77
impl NoOpRuntime {
56
78
/// Creates a new no-op runtime.
57
79
pub fn new ( ) -> Self {
@@ -60,17 +82,19 @@ impl NoOpRuntime {
60
82
}
61
83
62
84
// Generates a random jitter value up to max_jitter
63
- #[ cfg( any( feature = "experimental-grpc-retry" , feature = "experimental-http-retry" ) ) ]
85
+ #[ cfg( any(
86
+ feature = "experimental-grpc-retry" ,
87
+ feature = "experimental-http-retry"
88
+ ) ) ]
64
89
fn generate_jitter ( max_jitter : u64 ) -> u64 {
65
- let now = SystemTime :: now ( ) ;
66
- let nanos = now
90
+ let nanos = SystemTime :: now ( )
67
91
. duration_since ( SystemTime :: UNIX_EPOCH )
68
92
. unwrap ( )
69
93
. subsec_nanos ( ) ;
70
94
71
- let hasher = DefaultHasher :: default ( ) ;
72
-
73
- nanos as u64 % ( max_jitter + 1 )
95
+ let mut hasher = DefaultHasher :: default ( ) ;
96
+ hasher . write_u32 ( nanos ) ;
97
+ hasher . finish ( ) % ( max_jitter + 1 )
74
98
}
75
99
76
100
/// Retries the given operation with exponential backoff, jitter, and error classification.
@@ -90,7 +114,10 @@ fn generate_jitter(max_jitter: u64) -> u64 {
90
114
///
91
115
/// A `Result` containing the operation's result or an error if max retries are reached
92
116
/// or a non-retryable error occurs.
93
- #[ cfg( any( feature = "experimental-grpc-retry" , feature = "experimental-http-retry" ) ) ]
117
+ #[ cfg( any(
118
+ feature = "experimental-grpc-retry" ,
119
+ feature = "experimental-http-retry"
120
+ ) ) ]
94
121
pub async fn retry_with_backoff < R , F , Fut , T , E , C > (
95
122
runtime : R ,
96
123
policy : RetryPolicy ,
@@ -151,7 +178,10 @@ where
151
178
152
179
/// No-op retry function for when experimental_async_runtime is not enabled.
153
180
/// This function will execute the operation exactly once without any retries.
154
- #[ cfg( not( any( feature = "experimental-grpc-retry" , feature = "experimental-http-retry" ) ) ) ]
181
+ #[ cfg( not( any(
182
+ feature = "experimental-grpc-retry" ,
183
+ feature = "experimental-http-retry"
184
+ ) ) ) ]
155
185
pub async fn retry_with_backoff < R , F , Fut , T , E , C > (
156
186
_runtime : R ,
157
187
_policy : RetryPolicy ,
0 commit comments