7 releases

0.1.6 Jul 10, 2024
0.1.5 Jul 8, 2024
0.1.4 Apr 30, 2024
0.1.3 Mar 1, 2024
0.1.0 Feb 28, 2024

#624 in Network programming

Download history 32/week @ 2024-04-01 219/week @ 2024-04-29 13/week @ 2024-05-20 13/week @ 2024-07-01 275/week @ 2024-07-08

288 downloads per month

MIT license

41KB
697 lines

may_rpc

Rust coroutine based RPC framework

Build Status Current Crates.io Version Document

may_rpc is an RPC framework for rust based on coroutines that powered by may with a focus on ease of use. Inspired by tarpc.

Usage

Add to your Cargo.toml dependencies:

may_rpc = "0.1"

Example

#[may_rpc::service]
trait Hello {
    fn hello(&self, name: String) -> String;
}

#[derive(may_rpc::Server)]
#[service(Hello)]
struct HelloServer;

impl Hello for HelloServer {
    fn hello(&self, name: String) -> String {
        format!("Hello, {}!", name)
    }
}

fn main() {
    use may_rpc::TcpServer;
    let addr = "127.0.0.1:10000";
    let server = HelloServer.start(addr).unwrap();

    let stream = may::net::TcpStream::connect(addr).unwrap();
    let client = HelloClient::new(stream).unwrap();
    println!("{}", client.hello("Mom".to_string()).unwrap());

    server.shutdown();
}

The service attribute macro expands to a collection of items that form an rpc service. In the above example, the service macro is derived for a Hello rcp spec trait. This will generate HelloClient for ease of use. Then the HelloServer type derive Server and impl Hello trait for a rpc server. The generated types make it easy and ergonomic to write servers without dealing with sockets or serialization directly. Simply implement the generated traits, and you're off to the races!

See the examples directory for more examples.

Errors

All generated may_rpc RPC methods return Result<T, may_rpc::Error>. the Error reason could be an io error or timeout.

Default timeout is 10s while you can configure through the RpcClient instance.

Performance

Just run the throughput example under this project

Machine Specs:

  • Logical Cores: 4 (4 cores x 2 threads)
  • Memory: 4gb ECC DDR3 @ 1600mhz
  • Processor: CPU Intel(R) Core(TM) i7-3820QM CPU @ 2.70GHz
  • Operating System: Windows 10

Test config:

may::config().set_workers(6).set_io_workers(4);

result:

$ cargo run --example=throughput --release
......
     Running `target\release\examples\throughput.exe`
206127.39 rpc/second

Additional Features

  • Concurrent requests from a single client. client can be cloned to reuse the connection
  • Run any number of clients and services
  • Any type that impls serde's Serialize and Deserialize can be used in rpc signatures.
  • Attributes can be specified on rpc methods. These will be included on both the services' trait methods as well as on the clients' stub methods.

License

This project is licensed under either of the following, at your option:

Dependencies

~5–32MB
~486K SLoC