Crates.io | fibers_rpc |
lib.rs | fibers_rpc |
version | 0.3.4 |
source | src |
created_at | 2018-03-12 17:38:50.372269 |
updated_at | 2020-09-07 14:36:11.691998 |
description | RPC library built on top of fibers crate |
homepage | https://github.com/sile/fibers_rpc |
repository | https://github.com/sile/fibers_rpc |
max_upload_size | |
id | 55192 |
size | 172,815 |
Rust RPC library built on top of fibers crate.
See doc/.
Simple echo RPC server:
use bytecodec::bytes::{BytesEncoder, RemainingBytesDecoder};
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;
}
// 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 mut builder = ServerBuilder::new(server_addr);
builder.add_call_handler(EchoHandler);
let server = builder.finish(fibers_global::handle());
fibers_global::spawn(server.map_err(|e| panic!("{}", e)));
// RPC client
let service = ClientServiceBuilder::new().finish(fibers_global::handle());
let service_handle = service.handle();
fibers_global::spawn(service.map_err(|e| panic!("{}", e)));
let request = Vec::from(&b"hello"[..]);
let response = EchoRpc::client(&service_handle).call(server_addr, request.clone());
let response = fibers_global::execute(response)?;
assert_eq!(response, request);
Informal benchmark result (v0.2.1):
$ uname -a
Linux DESKTOP 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 x86_64 x86_64 x86_64 GNU/Linux
$ lscpu | grep 'Model name:'
Model name: Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz
// Runs the example echo server in a shell.
$ cargo run --example echo --release -- server
// Executes a benchmark command in another shell.
$ echo "hello" | cargo run --example echo --release -- bench -c 1024 -n 1000000
# ELAPSED: 8.111424
# RPS: 123282.91555218912