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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use std::time::Duration;
use pilota::thrift::TMessageIdentifier;
use volo::{
context::{Role, RpcCx, RpcInfo},
newtype_impl_context,
};
use crate::{client::CallOpt, protocol::TMessageType};
#[derive(Default, Clone, Debug)]
pub struct ServerTransportInfo {
conn_reset: bool,
}
impl ServerTransportInfo {
pub fn is_conn_reset(&self) -> bool {
self.conn_reset
}
pub fn set_conn_reset(&mut self, reset: bool) {
self.conn_reset = reset
}
pub fn reset(&mut self) {
*self = Self { ..Self::default() }
}
}
#[derive(Default)]
pub struct PooledTransport {
pub should_reuse: bool,
}
impl PooledTransport {
pub fn set_reuse(&mut self, should_reuse: bool) {
if !self.should_reuse && should_reuse {
panic!("cannot reuse a transport which should_reuse is false");
}
self.should_reuse = should_reuse;
}
}
pub struct ClientCxInner {
pub seq_id: i32,
pub message_type: TMessageType,
pub transport: PooledTransport,
}
pub struct ServerCxInner {
pub seq_id: Option<i32>,
pub req_msg_type: Option<TMessageType>,
pub msg_type: Option<TMessageType>,
pub transport: ServerTransportInfo,
}
pub struct ClientContext(pub(crate) RpcCx<ClientCxInner, Config>);
newtype_impl_context!(ClientContext, Config, 0);
impl ClientContext {
pub fn new(seq_id: i32, ri: RpcInfo<Config>, msg_type: TMessageType) -> Self {
Self(RpcCx::new(
ri,
ClientCxInner {
seq_id,
message_type: msg_type,
transport: PooledTransport { should_reuse: true },
},
))
}
}
impl std::ops::Deref for ClientContext {
type Target = RpcCx<ClientCxInner, Config>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for ClientContext {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub struct ServerContext(pub(crate) RpcCx<ServerCxInner, Config>);
impl Default for ServerContext {
fn default() -> Self {
Self(RpcCx::new(
RpcInfo::with_role(Role::Server),
ServerCxInner {
seq_id: None,
req_msg_type: None,
msg_type: None,
transport: ServerTransportInfo::default(),
},
))
}
}
newtype_impl_context!(ServerContext, Config, 0);
impl std::ops::Deref for ServerContext {
type Target = RpcCx<ServerCxInner, Config>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for ServerContext {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub trait ThriftContext: volo::context::Context<Config = Config> + Send + 'static {
fn encode_conn_reset(&self) -> Option<bool>;
fn set_conn_reset_by_ttheader(&mut self, reset: bool);
fn handle_decoded_msg_ident(&mut self, ident: &TMessageIdentifier);
fn seq_id(&self) -> i32;
fn msg_type(&self) -> TMessageType;
}
impl ThriftContext for ClientContext {
#[inline]
fn encode_conn_reset(&self) -> Option<bool> {
None
}
#[inline]
fn set_conn_reset_by_ttheader(&mut self, reset: bool) {
self.transport.set_reuse(!reset);
}
#[inline]
fn handle_decoded_msg_ident(&mut self, _ident: &TMessageIdentifier) {}
#[inline]
fn seq_id(&self) -> i32 {
self.seq_id
}
#[inline]
fn msg_type(&self) -> TMessageType {
self.message_type
}
}
impl ThriftContext for ServerContext {
#[inline]
fn encode_conn_reset(&self) -> Option<bool> {
Some(self.transport.is_conn_reset())
}
#[inline]
fn set_conn_reset_by_ttheader(&mut self, _reset: bool) {}
#[inline]
fn handle_decoded_msg_ident(&mut self, ident: &TMessageIdentifier) {
self.seq_id = Some(ident.sequence_number);
self.req_msg_type = Some(ident.message_type);
self.rpc_info.method = Some(ident.name.clone());
}
#[inline]
fn seq_id(&self) -> i32 {
self.seq_id.unwrap_or(0)
}
#[inline]
fn msg_type(&self) -> TMessageType {
self.msg_type.unwrap()
}
}
const DEFAULT_MAX_FRAME_SIZE: u32 = 1024 * 1024 * 16;
const DEFAULT_RPC_TIMEOUT: Duration = Duration::from_secs(1);
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_millis(50);
const DEFAULT_READ_WRITE_TIMEOUT: Duration = Duration::from_secs(1);
#[derive(Debug, Clone, Copy)]
pub struct Config {
rpc_timeout: Option<Duration>,
connect_timeout: Option<Duration>,
read_write_timeout: Option<Duration>,
max_frame_size: u32,
}
impl Config {
pub fn new() -> Self {
Self {
rpc_timeout: Some(DEFAULT_RPC_TIMEOUT),
connect_timeout: None,
read_write_timeout: None,
max_frame_size: DEFAULT_MAX_FRAME_SIZE,
}
}
pub fn rpc_timeout(&self) -> Option<Duration> {
self.rpc_timeout
}
pub fn set_rpc_timeout(&mut self, rpc_timeout: Option<Duration>) {
self.rpc_timeout = rpc_timeout;
}
pub fn rpc_timeout_or_default(&self) -> Duration {
self.rpc_timeout.unwrap_or(DEFAULT_RPC_TIMEOUT)
}
pub fn connect_timeout(&self) -> Option<Duration> {
self.connect_timeout
}
pub fn connect_timeout_or_default(&self) -> Duration {
self.connect_timeout.unwrap_or(DEFAULT_CONNECT_TIMEOUT)
}
pub fn set_connect_timeout(&mut self, timeout: Option<Duration>) {
self.connect_timeout = timeout;
}
pub fn read_write_timeout(&self) -> Option<Duration> {
self.read_write_timeout
}
pub fn read_write_timeout_or_default(&self) -> Duration {
self.read_write_timeout
.unwrap_or(DEFAULT_READ_WRITE_TIMEOUT)
}
pub(crate) fn set_read_write_timeout(&mut self, timeout: Option<Duration>) {
self.read_write_timeout = timeout;
}
pub fn max_frame_size(&self) -> u32 {
self.max_frame_size
}
pub(crate) fn set_max_frame_size(&mut self, size: u32) {
self.max_frame_size = size
}
pub fn merge(&mut self, other: Self) {
self.max_frame_size = other.max_frame_size;
if let Some(t) = other.rpc_timeout {
self.rpc_timeout = Some(t);
}
if let Some(t) = other.connect_timeout {
self.connect_timeout = Some(t);
}
if let Some(t) = other.read_write_timeout {
self.read_write_timeout = Some(t);
}
}
}
impl Default for Config {
fn default() -> Self {
Self {
rpc_timeout: None,
connect_timeout: None,
read_write_timeout: None,
max_frame_size: DEFAULT_MAX_FRAME_SIZE,
}
}
}
impl ::volo::client::Apply<ClientContext> for CallOpt {
type Error = crate::Error;
#[inline]
fn apply(self, cx: &mut ClientContext) -> Result<(), Self::Error> {
let callee = cx.rpc_info.callee.as_mut().unwrap();
callee.tags.extend(self.callee_tags);
if let Some(a) = &self.address {
callee.set_address(a.clone());
}
let caller = cx.rpc_info.caller.as_mut().unwrap();
caller.tags.extend(self.caller_tags);
cx.rpc_info.config_mut().unwrap().merge(self.config);
Ok(())
}
}