1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use anyhow::Result;
use backoff::{future::retry, Error as BackoffError, ExponentialBackoff};
use std::time::Duration;
use tonic::Code;
/// Default timeout for retry operations.
pub const DEFAULT_RETRY_TIMEOUT: Duration = Duration::from_secs(120);
/// Trait for implementing retryable RPC operations.
#[async_trait::async_trait]
pub trait RetryableRpc {
/// Execute an operation with retries using default timeout.
async fn with_retry<'a, T, F, Fut>(&'a self, operation: F, operation_name: &str) -> Result<T>
where
F: Fn() -> Fut + Send + Sync + 'a,
Fut: std::future::Future<Output = Result<T>> + Send,
T: Send;
/// Execute an operation with retries using custom timeout.
async fn with_retry_timeout<'a, T, F, Fut>(
&'a self,
operation: F,
timeout: Duration,
operation_name: &str,
) -> Result<T>
where
F: Fn() -> Fut + Send + Sync + 'a,
Fut: std::future::Future<Output = Result<T>> + Send,
T: Send;
}
/// Execute an async operation with exponential backoff retries.
pub async fn retry_operation<T, F, Fut>(
operation: F,
timeout: Option<Duration>,
operation_name: &str,
) -> Result<T>
where
F: Fn() -> Fut + Send + Sync,
Fut: std::future::Future<Output = Result<T>> + Send,
{
let backoff = ExponentialBackoff {
initial_interval: Duration::from_secs(1),
max_interval: Duration::from_secs(120),
max_elapsed_time: timeout,
..Default::default()
};
retry(backoff, || async {
match operation().await {
Ok(result) => Ok(result),
Err(e) => {
// Check for tonic status errors.
if let Some(status) = e.downcast_ref::<tonic::Status>() {
match status.code() {
Code::Unavailable
| Code::DeadlineExceeded
| Code::Internal
| Code::Aborted
| Code::Cancelled => {
tracing::warn!(
"Network temporarily unavailable when {} due to {}, retrying...",
operation_name,
status.message(),
);
Err(BackoffError::transient(e))
}
Code::NotFound => {
tracing::error!(
"{} not found due to {}",
operation_name,
status.message(),
);
Err(BackoffError::permanent(e))
}
// Dropped connection on the reused channel; failed before send, safe to
// retry — see `is_connection_not_ready`.
_ if is_connection_not_ready(status) => {
tracing::warn!(
"Connection not ready when {} ({}), retrying...",
operation_name,
status.message(),
);
Err(BackoffError::transient(e))
}
_ => {
tracing::error!(
"Permanent error encountered when {}: {} ({})",
operation_name,
status.message(),
status.code()
);
Err(BackoffError::permanent(e))
}
}
} else {
// Check for common transport errors.
let error_msg = e.to_string().to_lowercase();
let error_debug_msg = format!("{e:?}");
if error_debug_msg.contains("no native certs found") {
tracing::error!(
"Permanent error when {}: no native certs found",
operation_name
);
Err(BackoffError::permanent(e))
} else {
let is_transient = error_msg.contains("tls handshake")
|| error_msg.contains("dns error")
|| error_msg.contains("connection reset")
|| error_msg.contains("broken pipe")
|| error_msg.contains("transport error")
|| error_msg.contains("failed to lookup")
|| error_msg.contains("timeout")
|| error_msg.contains("deadline exceeded")
|| error_msg.contains("error sending request for url");
if is_transient {
tracing::warn!(
"Transient transport error when {}: {}, retrying...",
operation_name,
error_msg
);
Err(BackoffError::transient(e))
} else {
tracing::error!(
"Permanent error when {}: {}",
operation_name,
error_msg
);
Err(BackoffError::permanent(e))
}
}
}
}
}
})
.await
}
/// True for tonic's `poll_ready` failure on a reused channel — reported by the generated client as
/// `Status::unknown("Service was not ready: ...")` when the channel's connection has dropped (e.g.
/// an idle connection reaped by a load balancer).
///
/// The gate fails before the request is sent, so it's safe to retry; the channel re-establishes on
/// the next attempt. Matched narrowly on `Code::Unknown` plus this prefix so other errors stay
/// permanent.
fn is_connection_not_ready(status: &tonic::Status) -> bool {
status.code() == Code::Unknown
&& status.message().to_lowercase().contains("service was not ready")
}
#[cfg(test)]
mod tests {
use super::{is_connection_not_ready, retry_operation, Result};
use std::{
sync::atomic::{AtomicUsize, Ordering},
time::Duration,
};
use tonic::Status;
#[test]
fn not_ready_is_the_only_retryable_unknown() {
// The poll_ready signature, matched case-insensitively.
assert!(is_connection_not_ready(&Status::unknown(
"Service was not ready: transport error"
)));
assert!(is_connection_not_ready(&Status::unknown("SERVICE WAS NOT READY: x")));
// Regression guard: any other error must stay permanent — other `Unknown` messages, and
// the same message under any other code. We never retry on free-form server text.
assert!(!is_connection_not_ready(&Status::unknown("invalid request timeout value")));
assert!(!is_connection_not_ready(&Status::invalid_argument("Service was not ready: x")));
assert!(!is_connection_not_ready(&Status::resource_exhausted("Service was not ready: x")));
}
#[tokio::test]
async fn retry_loop_recovers_from_not_ready() {
let attempts = AtomicUsize::new(0);
let res = retry_operation(
|| async {
if attempts.fetch_add(1, Ordering::SeqCst) == 0 {
Err(Status::unknown("Service was not ready: transport error").into())
} else {
Ok(())
}
},
Some(Duration::from_secs(10)),
"test op",
)
.await;
assert!(res.is_ok());
assert_eq!(attempts.load(Ordering::SeqCst), 2, "should retry once, then succeed");
}
#[tokio::test]
async fn retry_loop_fails_fast_on_permanent() {
let attempts = AtomicUsize::new(0);
let res: Result<()> = retry_operation(
|| async {
attempts.fetch_add(1, Ordering::SeqCst);
Err(Status::unknown("invalid argument").into())
},
Some(Duration::from_secs(10)),
"test op",
)
.await;
assert!(res.is_err());
assert_eq!(attempts.load(Ordering::SeqCst), 1, "permanent errors must not retry");
}
}