I’d like to introduce the may_rpc
project which is a coroutine based RPC framework for rust that powered by may. It’s inspired by tarpc which has more detailed documentation.
may_rpc
allows users write coroutine style code for server/client logic which is very efficient.
How to use the framework
Declare the rpc specification
1 | rpc! { |
The rpc!
macro expands to a collection of items that form an rpc service. In the above example, the rcp!
macro is called with a user supplied rcp spec. This will generate RpcClient
type, RpcServer
type and RpcSpec
trait. These generated types make it easy and ergonomic to write server and client without dealing with sockets or serialization directly. Just simply implement the generated traits for sever, that’s it.
when net type is Multiplex
, you can freely clone the client instance without multiple connections to the server.
Implement RpcSpec for server
1 | struct HelloImpl; |
Create the server
1 | let server = RpcServer(HelloImpl).start("127.0.0.1:3333").unwrap(); |
The returned server is just a coroutine
handle. You can shut down it later if necessary.
Create a client and call the service
1 | let client = RpcClient::connect("127.0.0.1:3333").unwrap(); |
Shut down the service gracefully
1 | unsafe { server.coroutine().cancel() }; |
This will kill all the coroutines that spawned by the server even they are not finished.
You can see more examples in the project.
Performance
Just run the throughput example under may_rcp, the service is return immediately after receiving a request, so the result is just the framework’s maximum limit.
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:
To fully utilize the CPU, I use the following config on my laptop. Normally it will reach the maximum speed with workers:io_workers = 3:2, you can tune yours accordingly.
1 | may::config().set_workers(6).set_io_workers(4); |
result:
1 | $ cargo run --example=throughput --release |
Easy coding with high performance, enjoy your life!