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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! RPC library built on top of [fibers] crate.
//!
//!
//! [fibers]: https://github.com/dwango/fibers-rs
//!
//! # Features
//!
//! - Asynchronous RPC server/client using [fibers] crate
//! - Support two type of RPC:
//!   - Request/response model
//!   - Notification model
//! - Strongly typed RPC using [bytecodec] crate
//!   - You can treat arbitrarily Rust structures that support [serde] as RPC messages
//!   - It is possible to handle huge structures as RPC messages without
//!     compromising efficiency and real-time property by implementing your own encoder/decoder
//! - Multiplexing multiple RPC messages in a single TCP stream
//! - Prioritization between messages
//! - Expose [Prometheus] metrics
//!
//! [fibers]: https://github.com/dwango/fibers-rs
//! [bytecodec]: https://github.com/sile/bytecodec
//! [serde]: https://crates.io/crates/serde
//! [Prometheus]: https://prometheus.io/
//!
//! # Examples
//!
//! Simple echo RPC server:
//!
//! ```
//! # extern crate bytecodec;
//! # extern crate fibers;
//! # extern crate fibers_rpc;
//! # extern crate futures;
//! # fn main() {
//! use bytecodec::bytes::{BytesEncoder, RemainingBytesDecoder};
//! use fibers::{Executor, InPlaceExecutor, Spawn};
//! use fibers_rpc::{Call, ProcedureId};
//! use fibers_rpc::client::ClientServiceBuilder;
//! use fibers_rpc::server::{HandleCall, Reply, ServerBuilder};
//! use futures::Future;
//!
//! // RPC definition
//! struct EchoRpc;
//! impl Call for EchoRpc {
//!     const ID: ProcedureId = ProcedureId(0);
//!     const NAME: &'static str = "echo";
//!
//!     type Req = Vec<u8>;
//!     type ReqEncoder = BytesEncoder<Vec<u8>>;
//!     type ReqDecoder = RemainingBytesDecoder;
//!
//!     type Res = Vec<u8>;
//!     type ResEncoder = BytesEncoder<Vec<u8>>;
//!     type ResDecoder = RemainingBytesDecoder;
//! }
//!
//! // Executor
//! let mut executor = InPlaceExecutor::new().unwrap();
//!
//! // RPC server
//! struct EchoHandler;
//! impl HandleCall<EchoRpc> for EchoHandler {
//!     fn handle_call(&self, request: <EchoRpc as Call>::Req) -> Reply<EchoRpc> {
//!         Reply::done(request)
//!     }
//! }
//! let server_addr = "127.0.0.1:1919".parse().unwrap();
//! let server = ServerBuilder::new(server_addr)
//!     .add_call_handler(EchoHandler)
//!     .finish(executor.handle());
//! executor.spawn(server.map_err(|e| panic!("{}", e)));
//!
//! // RPC client
//! let service = ClientServiceBuilder::new().finish(executor.handle());
//!
//! let request = Vec::from(&b"hello"[..]);
//! let response = EchoRpc::client(&service.handle()).call(server_addr, request.clone());
//!
//! executor.spawn(service.map_err(|e| panic!("{}", e)));
//! let result = executor.run_future(response).unwrap();
//! assert_eq!(result.ok(), Some(request));
//! # }
//! ```
#![warn(missing_docs)]
extern crate atomic_immut;
extern crate bytecodec;
extern crate byteorder;
extern crate factory;
extern crate fibers;
extern crate fibers_tasque;
extern crate futures;
extern crate prometrics;
#[macro_use]
extern crate slog;
#[macro_use]
extern crate trackable;

pub use error::{Error, ErrorKind};

pub mod client {
    //! RPC client.

    pub use client_service::{ClientService, ClientServiceBuilder, ClientServiceHandle};
    pub use client_side_handlers::Response;
    pub use rpc_client::{CallClient, CastClient, Options};
}
pub mod channel;
pub mod metrics;
pub mod server {
    //! RPC server.

    pub use rpc_server::{Server, ServerBuilder};
    pub use server_side_handlers::{HandleCall, HandleCast, NoReply, Reply};
}

use client::{CallClient, CastClient, ClientServiceHandle};

mod client_service;
mod client_side_channel;
mod client_side_handlers;
mod error;
mod message;
mod message_stream;
mod packet;
mod rpc_client;
mod rpc_server;
mod server_side_channel;
mod server_side_handlers;

/// This crate specific `Result` type.
pub type Result<T> = std::result::Result<T, Error>;

/// The identifier of a procedure.
///
/// This must be unique among procedures registered in an RPC server.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ProcedureId(pub u32);

/// Request/response RPC.
pub trait Call: Sized + Send + Sync + 'static {
    /// The identifier of the procedure.
    const ID: ProcedureId;

    /// The name of the procedure.
    ///
    /// This is only used for debugging purpose.
    const NAME: &'static str;

    /// Request message.
    type Req: Send + 'static;

    /// Request message encoder.
    type ReqEncoder: bytecodec::Encode<Item = Self::Req> + Send + 'static;

    /// Request message decoder.
    type ReqDecoder: bytecodec::Decode<Item = Self::Req> + Send + 'static;

    /// Response message.
    type Res: Send + 'static;

    /// Response message encoder.
    type ResEncoder: bytecodec::Encode<Item = Self::Res> + Send + 'static;

    /// Response message decoder.
    type ResDecoder: bytecodec::Decode<Item = Self::Res> + Send + 'static;

    /// If it returns `true`, encoding/decoding request messages will be executed asynchronously.
    ///
    /// For large RPC messages, asynchronous encoding/decoding may improve real-time property
    /// (especially if messages will be encoded/decoded by using `serde`).
    ///
    /// The default implementation always return `false`.
    #[allow(unused_variables)]
    fn enable_async_request(request: &Self::Req) -> bool {
        false
    }

    /// If it returns `true`, encoding/decoding response messages will be executed asynchronously.
    ///
    /// For large RPC messages, asynchronous encoding/decoding may improve real-time property
    /// (especially if messages will be encoded/decoded by using `serde`).
    ///
    /// The default implementation always return `false`.
    #[allow(unused_variables)]
    fn enable_async_response(response: &Self::Res) -> bool {
        false
    }

    /// Makes a new RPC client.
    fn client(service: &ClientServiceHandle) -> CallClient<Self>
    where
        Self::ReqEncoder: Default,
        Self::ResDecoder: Default,
    {
        Self::client_with_codec(service, Default::default(), Default::default())
    }

    /// Makes a new RPC client with the given decoder maker.
    fn client_with_decoder(
        service: &ClientServiceHandle,
        decoder: Self::ResDecoder,
    ) -> CallClient<Self>
    where
        Self::ReqEncoder: Default,
    {
        Self::client_with_codec(service, decoder, Default::default())
    }

    /// Makes a new RPC client with the given encoder maker.
    fn client_with_encoder(
        service: &ClientServiceHandle,
        encoder: Self::ReqEncoder,
    ) -> CallClient<Self>
    where
        Self::ResDecoder: Default,
    {
        Self::client_with_codec(service, Default::default(), encoder)
    }

    /// Makes a new RPC client with the given decoder and encoder makers.
    fn client_with_codec(
        service: &ClientServiceHandle,
        decoder: Self::ResDecoder,
        encoder: Self::ReqEncoder,
    ) -> CallClient<Self> {
        CallClient::new(service, decoder, encoder)
    }
}

/// Notification RPC.
pub trait Cast: Sized + Sync + Send + 'static {
    /// The identifier of the procedure.
    const ID: ProcedureId;

    /// The name of the procedure.
    ///
    /// This is only used for debugging purpose.
    const NAME: &'static str;

    /// Notification message.
    type Notification: Send + 'static;

    /// Notification message encoder.
    type Encoder: bytecodec::Encode<Item = Self::Notification> + Send + 'static;

    /// Notification message decoder.
    type Decoder: bytecodec::Decode<Item = Self::Notification> + Send + 'static;

    /// If it returns `true`, encoding/decoding notification messages will be executed asynchronously.
    ///
    /// For large RPC messages, asynchronous encoding/decoding may improve real-time property
    /// (especially if messages will be encoded/decoded by using `serde`).
    ///
    /// The default implementation always return `false`.
    #[allow(unused_variables)]
    fn enable_async(notification: &Self::Notification) -> bool {
        false
    }

    /// Makes a new RPC client.
    fn client(service: &ClientServiceHandle) -> CastClient<Self>
    where
        Self::Encoder: Default,
    {
        Self::client_with_encoder(service, Default::default())
    }

    /// Makes a new RPC client with the given encoder maker.
    fn client_with_encoder(
        service: &ClientServiceHandle,
        encoder: Self::Encoder,
    ) -> CastClient<Self> {
        CastClient::new(service, encoder)
    }
}

#[cfg(test)]
mod test {
    use bytecodec::bytes::{BytesEncoder, RemainingBytesDecoder};
    use fibers::{Executor, InPlaceExecutor, Spawn};
    use futures::Future;

    use client::ClientServiceBuilder;
    use server::{HandleCall, Reply, ServerBuilder};
    use {Call, ProcedureId};

    // RPC
    struct EchoRpc;
    impl Call for EchoRpc {
        const ID: ProcedureId = ProcedureId(0);
        const NAME: &'static str = "echo";

        type Req = Vec<u8>;
        type ReqEncoder = BytesEncoder<Vec<u8>>;
        type ReqDecoder = RemainingBytesDecoder;

        type Res = Vec<u8>;
        type ResEncoder = BytesEncoder<Vec<u8>>;
        type ResDecoder = RemainingBytesDecoder;

        fn enable_async_request(x: &Self::Req) -> bool {
            x == b"async"
        }

        fn enable_async_response(x: &Self::Res) -> bool {
            x == b"async"
        }
    }

    // Handler
    struct EchoHandler;
    impl HandleCall<EchoRpc> for EchoHandler {
        fn handle_call(&self, request: <EchoRpc as Call>::Req) -> Reply<EchoRpc> {
            Reply::done(request)
        }
    }

    #[test]
    fn it_works() {
        // Executor
        let mut executor = track_try_unwrap!(track_any_err!(InPlaceExecutor::new()));

        // Server
        let server_addr = "127.0.0.1:1920".parse().unwrap();
        let server = ServerBuilder::new(server_addr)
            .add_call_handler(EchoHandler)
            .finish(executor.handle());
        executor.spawn(server.map_err(|e| panic!("{}", e)));

        // Client
        let service = ClientServiceBuilder::new().finish(executor.handle());
        let service_handle = service.handle();

        let request = Vec::from(&b"hello"[..]);
        let response = EchoRpc::client(&service_handle).call(server_addr, request.clone());

        executor.spawn(service.map_err(|e| panic!("{}", e)));
        let result = track_try_unwrap!(track_any_err!(executor.run_future(response)));
        assert_eq!(result.ok(), Some(request));

        let metrics = service_handle
            .metrics()
            .channels()
            .as_map()
            .load()
            .get(&server_addr)
            .cloned()
            .unwrap();
        assert_eq!(metrics.async_outgoing_messages(), 0);
        assert_eq!(metrics.async_incoming_messages(), 0);
    }

    #[test]
    fn large_message_works() {
        // Executor
        let mut executor = track_try_unwrap!(track_any_err!(InPlaceExecutor::new()));

        // Server
        let server_addr = "127.0.0.1:1921".parse().unwrap();
        let server = ServerBuilder::new(server_addr)
            .add_call_handler(EchoHandler)
            .finish(executor.handle());
        executor.spawn(server.map_err(|e| panic!("{}", e)));

        // Client
        let service = ClientServiceBuilder::new().finish(executor.handle());

        let request = vec![0; 10 * 1024 * 1024];
        let response = EchoRpc::client(&service.handle()).call(server_addr, request.clone());

        executor.spawn(service.map_err(|e| panic!("{}", e)));
        let result = track_try_unwrap!(track_any_err!(executor.run_future(response)));
        assert_eq!(result.ok(), Some(request));
    }

    #[test]
    fn async_works() {
        // Executor
        let mut executor = track_try_unwrap!(track_any_err!(InPlaceExecutor::new()));

        // Server
        let server_addr = "127.0.0.1:1922".parse().unwrap();
        let server = ServerBuilder::new(server_addr)
            .add_call_handler(EchoHandler)
            .finish(executor.handle());
        executor.spawn(server.map_err(|e| panic!("{}", e)));

        // Client
        let service = ClientServiceBuilder::new().finish(executor.handle());
        let service_handle = service.handle();

        let request = Vec::from(&b"async"[..]);
        let response = EchoRpc::client(&service_handle).call(server_addr, request.clone());

        executor.spawn(service.map_err(|e| panic!("{}", e)));
        let result = track_try_unwrap!(track_any_err!(executor.run_future(response)));
        assert_eq!(result.ok(), Some(request));

        let metrics = service_handle
            .metrics()
            .channels()
            .as_map()
            .load()
            .get(&server_addr)
            .cloned()
            .unwrap();
        assert_eq!(metrics.async_outgoing_messages(), 1);
        assert_eq!(metrics.async_incoming_messages(), 1);
    }
}