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
#[macro_export]
#[doc(hidden)]
macro_rules! define_client {
($client_type:ident) => {
#[derive(Clone)]
pub struct $client_type {
pool: $crate::ManagedChannelPool,
}
impl $client_type {
pub fn new(endpoint: $crate::EndpointTemplate) -> Self {
Self {
pool: $crate::ManagedChannelPoolBuilder::new(endpoint).build(),
}
}
}
impl From<$crate::ManagedChannelPool> for $client_type {
fn from(pool: $crate::ManagedChannelPool) -> Self {
Self { pool }
}
}
};
(
$client_type:ident, $original_client:ident, $(($name:ident, $request:ty, $response:ty)),+ $(,)?) => {
define_client!($client_type);
impl $client_type {
$(
define_method!($original_client, $name, $request, $response);
)+
}
};
($client_type:ident, $(($original_client:ident, $name:ident, $request:ty, $response:ty)),+) => {
define_client!($client_type);
impl $client_type {
$(
define_method!($original_client, $name, $request, $response);
)+
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! define_method {
($client:ident, $name:ident, $request:ty, $response:ty) => {
$crate::deps::paste! {
pub async fn $name(
&self,
request: impl tonic::IntoRequest<$request>,
) -> Result<tonic::Response<$response>, tonic::Status> {
self.[<$name _with_retry>]::<$crate::DefaultRetryPolicy>(
request,
).await
}
pub async fn [<$name _with_retry>] <RP: $crate::RetryPolicy>(
&self,
request: impl tonic::IntoRequest<$request>,
) -> Result<tonic::Response<$response>, tonic::Status> {
let (metadata, extensions, message) = request.into_request().into_parts();
let mut tries = 0;
loop {
tries += 1;
// Get channel of random index.
let (ip_address, channel) = self.pool.get_channel().await.ok_or_else(|| {
tonic::Status::unavailable("No ready channels")
})?;
let request = tonic::Request::from_parts(
metadata.clone(),
extensions.clone(),
message.clone(),
);
#[allow(deprecated)]
let result = $client::new(channel).$name(request).await;
match result {
Ok(response) => {
return Ok(response);
}
Err(e) => {
let (server_status, retry_time) = RP::should_retry(&e, tries);
if matches!(server_status, $crate::ServerStatus::Dead) {
// If the server is dead, we should report it.
self.pool.report_broken(ip_address).await;
}
match retry_time {
$crate::RetryTime::DoNotRetry => {
// Do not retry and do not report broken endpoint.
return Err(e);
}
$crate::RetryTime::Immediately => {
// Retry immediately.
continue;
}
$crate::RetryTime::After(duration) => {
// Wait for the specified duration before retrying.
// todo-interface: Don't require client to have tokio dependency.
$crate::deps::sleep(duration).await;
}
}
}
}
}
}
}
};
}