Crate fibers_rpc [] [src]

RPC library built on top of fibers crate.

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

Examples

Simple echo RPC server:

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));

Modules

channel

RPC channel related components.

client

RPC client.

metrics

Prometheus metrics.

server

RPC server.

Structs

Error

This crate specific Error type.

ProcedureId

The identifier of a procedure.

Enums

ErrorKind

Possible error kinds.

Traits

Call

Request/response RPC.

Cast

Notification RPC.

Type Definitions

Result

This crate specific Result type.