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
use bytes::{Bytes, BytesMut};
use smallvec::SmallVec;
use std::fmt;
use std::io;
use tokio_io::codec::{Decoder, Encoder};
use tokio_proto::multiplex::RequestId;
use controller::Controller;
use message::{RpcMeta, RpcRequestMeta, RpcResponseMeta};
use message::{RequestPackage, ResponsePackage};
pub use self::brpc::BrpcProtocol;
pub use self::http::HttpProtocol;
pub mod brpc;
pub mod http;
#[derive(Clone, Debug)]
pub enum Protocol {
Brpc,
Http,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ProtocolError {
TryOthers,
NeedMoreBytes,
AbsolutelyWrong,
}
pub trait RpcProtocol: Sync + Send {
fn try_parse(
&mut self,
buf: &mut BytesMut,
) -> Result<(RequestId, (RpcMeta, Controller, Bytes)), ProtocolError>;
fn new_boxed(&self) -> Box<RpcProtocol>;
fn write_package(
&self,
meta: (RpcMeta, Controller, Bytes),
buf: &mut BytesMut,
) -> io::Result<()>;
fn name(&self) -> &'static str;
}
impl fmt::Debug for RpcProtocol {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name())
}
}
#[derive(Debug)]
pub struct ProtoCodec {
schemes: SmallVec<[Box<RpcProtocol>; 4]>,
cached_scheme: usize,
tried_num: i32,
}
impl ProtoCodec {
pub fn new(protos: &[Box<RpcProtocol>]) -> Self {
let schemes: SmallVec<[Box<RpcProtocol>; 4]> =
protos.iter().map(|proto| proto.new_boxed()).collect();
ProtoCodec {
schemes,
cached_scheme: 0,
tried_num: 0,
}
}
}
impl Decoder for ProtoCodec {
type Item = (RequestId, RequestPackage);
type Error = io::Error;
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
loop {
match self.schemes[self.cached_scheme].try_parse(buf) {
Ok((id, (mut meta, controller, body))) => {
self.tried_num = 0;
return Ok(Some((id, (meta.take_request(), controller, body))));
}
Err(ProtocolError::NeedMoreBytes) => return Ok(None),
Err(ProtocolError::TryOthers) => {
self.cached_scheme = (self.cached_scheme + 1) % self.schemes.len();
self.tried_num += 1;
if self.tried_num >= self.schemes.len() as i32 {
self.tried_num = 0;
warn!("No protocol recognize this package");
return Err(io::Error::new(
io::ErrorKind::Other,
"No protocol recognize this package",
));
}
}
Err(ProtocolError::AbsolutelyWrong) => {
warn!("Invalid request package");
return Err(io::Error::new(io::ErrorKind::Other, "Invalid package"));
}
}
}
}
}
impl Encoder for ProtoCodec {
type Item = (RequestId, ResponsePackage);
type Error = io::Error;
fn encode(&mut self, msg: Self::Item, buf: &mut BytesMut) -> Result<(), Self::Error> {
let scheme = &self.schemes[self.cached_scheme];
let (id, (resp_meta, controller, body)) = msg;
let mut meta = RpcMeta::new();
meta.set_response(resp_meta);
meta.set_correlation_id(id);
scheme.write_package((meta, controller, body), buf)
}
}
pub struct ProtoCodecClient {
scheme: Box<RpcProtocol>,
}
impl fmt::Debug for ProtoCodecClient {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ProtoCodecClient")
.field("scheme", &self.scheme.name())
.finish()
}
}
impl ProtoCodecClient {
pub fn new(proto: Box<RpcProtocol>) -> Self {
ProtoCodecClient { scheme: proto }
}
}
impl Decoder for ProtoCodecClient {
type Item = (RequestId, (RpcResponseMeta, Bytes));
type Error = io::Error;
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
match self.scheme.try_parse(buf) {
Ok((id, (mut meta, _, body))) => {
if !meta.has_response() {
return Err(io::Error::new(
io::ErrorKind::Other,
"Response package do not have response field",
));
}
return Ok(Some((id, (meta.take_response(), body))));
}
Err(ProtocolError::NeedMoreBytes) => return Ok(None),
Err(ProtocolError::TryOthers) | Err(ProtocolError::AbsolutelyWrong) => {
error!("Decode response package failed, invalid package or wrong protocol");
return Err(io::Error::new(
io::ErrorKind::Other,
"Invalid package or wrong protocol",
));
}
}
}
}
impl Encoder for ProtoCodecClient {
type Item = (RequestId, (RpcRequestMeta, Bytes));
type Error = io::Error;
fn encode(&mut self, msg: Self::Item, buf: &mut BytesMut) -> Result<(), Self::Error> {
let (id, (request_meta, body)) = msg;
let mut meta = RpcMeta::new();
meta.set_request(request_meta);
meta.set_correlation_id(id);
self.scheme
.write_package((meta, Controller::default(), body), buf)
}
}