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
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 => {
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))
}
_ => {
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
}