Expand description
A streaming rpc system for transports that support multiple bidirectional streams, such as QUIC and HTTP2.
A lightweight memory transport is provided for cases where you want have multiple cleanly separated substreams in the same process.
For supported transports, see the transport module.
§Motivation
See the README
§Example
use derive_more::{From, TryInto};
use quic_rpc::{message::RpcMsg, RpcClient, RpcServer, Service};
use serde::{Deserialize, Serialize};
// Define your messages
#[derive(Debug, Serialize, Deserialize)]
struct Ping;
#[derive(Debug, Serialize, Deserialize)]
struct Pong;
// Define your RPC service and its request/response types
#[derive(Debug, Clone)]
struct PingService;
#[derive(Debug, Serialize, Deserialize, From, TryInto)]
enum PingRequest {
Ping(Ping),
}
#[derive(Debug, Serialize, Deserialize, From, TryInto)]
enum PingResponse {
Pong(Pong),
}
impl Service for PingService {
type Req = PingRequest;
type Res = PingResponse;
}
// Define interaction patterns for each request type
impl RpcMsg<PingService> for Ping {
type Response = Pong;
}
// create a transport channel, here a memory channel for testing
let (server, client) = quic_rpc::transport::flume::channel(1);
// client side
// create the rpc client given the channel and the service type
let mut client = RpcClient::<PingService, _>::new(client);
// call the service
let res = client.rpc(Ping).await?;
// server side
// create the rpc server given the channel and the service type
let mut server = RpcServer::<PingService, _>::new(server);
let handler = Handler;
loop {
// accept connections
let (msg, chan) = server.accept().await?.read_first().await?;
// dispatch the message to the appropriate handler
match msg {
PingRequest::Ping(ping) => chan.rpc(ping, handler, Handler::ping).await?,
}
}
// the handler. For a more complex example, this would contain any state
// needed to handle the request.
#[derive(Debug, Clone, Copy)]
struct Handler;
impl Handler {
// the handle fn for a Ping request.
// The return type is the response type for the service.
// Note that this must take self by value, not by reference.
async fn ping(self, _req: Ping) -> Pong {
Pong
}
}
§Features
hyper-transport
— HTTP transport using thehyper
cratequinn-transport
— QUIC transport using theiroh-quinn
crateflume-transport
(enabled by default) — In memory transport using theflume
crateiroh-transport
— p2p QUIC transport using theiroh
cratemacros
— Macros for creating request handlerstest-utils
— Utilities for testingdefault
— Default, includes the memory transport
Re-exports§
Modules§
- Client side api
- Service definition
- Predefined interaction patterns.
- Server side api
- Built in transports for quic-rpc
Macros§
- declare_
bidi_ streaming macros
Declare a message to be a server streaming message for a service. - declare_
client_ streaming macros
Declare a message to be a server streaming message for a service. - declare_
rpc macros
Declare a message to be a rpc message for a service. - declare_
server_ streaming macros
Declare a message to be a server streaming message for a service. - rpc_
service macros
Derive a set of RPC types and message implementation from a declaration.
Traits§
- A connector to a specific service
- A listener for a specific service
- Requirements for an internal error
- Requirements for a RPC message
- A service
Functions§
- flume_
channel flume-transport
- quinn_
channel test-utils