Overview
RPC between services over TCP, UDP, NATS, Redis, MQTT, RabbitMQ or Kafka, with one set of handlers and one wire grammar.
ulo-rpc serves controller methods to callers identified by a pattern string such as calc.divide. A caller sends a message and waits for the reply, or emits an event and waits for nobody. The same handlers run over seven links (TCP, UDP, NATS, Redis, MQTT, RabbitMQ, Kafka). A link is the crate that carries the call between caller and server, a socket or a broker. It is chosen where the server binds and where the client is built, and nothing in the controller changes.
For contract-first services with .proto files, see gRPC.
A first server
A controller with one message and one event, served over TCP. This is examples/25-rpc-basics.
[dependencies]
ulo = "=0.1.0-alpha.3"
ulo-rpc = "=0.1.0-alpha.3"
ulo-rpc-tcp = "=0.1.0-alpha.3"
ulo-runtime-tokio = "=0.1.0-alpha.3"
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }use ulo::{Dep, controller, handlers, module};
use ulo_rpc::{Classify, Payload};
#[controller]
pub struct Calculator {
journal: Dep<Journal>,
}
#[handlers]
impl Calculator {
#[ulo_rpc::message("calc.divide")]
async fn divide(&self, req: Payload<Divide>) -> Result<i64, CalcError> {
req.0.dividend.checked_div(req.0.divisor).ok_or(CalcError::DivideByZero)
}
#[ulo_rpc::event("calc.note")]
async fn note(&self, text: Payload<String>) -> Result<(), CalcError> {
self.journal.0.lock().expect("the journal is not poisoned").push(text.0);
Ok(())
}
}use ulo_rpc::Server;
use ulo_rpc_tcp::Tcp;
use ulo_runtime_tokio::{Tokio, shutdown_signal};
let app = ex_25_rpc_basics::builder()
.runtime(Tokio::current())
.wire()?
.connect()
.await?
.bind(Server::new(Tcp::new(format!("127.0.0.1:{port}"))))
.listen()
.await?;
app.serve(shutdown_signal()).await?;runtime names the runtime, wire builds the container, connect runs the init hooks, bind attaches a transport and listen acquires the sockets. See Getting started for the sequence and Core concepts for modules and injection.
A first call
The client is a separate handle built over the same kind of link. From examples/25-rpc-basics/tests/rpc.rs:
use ulo_rpc::RpcClient;
let client = RpcClient::new(Tcp::new(addr), Arc::new(Tokio::current()))?;
let answer = client.request::<_, i64>("calc.divide", &Divide { dividend: 84, divisor: 2 }).await?;
client.emit("calc.note", "carry the one").await?;An application usually receives its client through RpcClientModule; see Client.
Choosing a link
| Link | Crate | Delivery | Streamed shapes | Needs |
|---|---|---|---|---|
| TCP | ulo-rpc-tcp, ulo-rpc-tcp-smol | the caller names the server | yes | nothing |
| UDP | ulo-rpc-udp | the caller names the server | no | nothing |
| NATS | ulo-rpc-nats | competing (queue group) | yes | a NATS server |
| Redis | ulo-rpc-redis | every subscribed instance | yes | a Redis server; one server instance for requests and for events left at the default |
| MQTT | ulo-rpc-mqtt | competing (shared subscription) | yes | an MQTT v5 broker with shared subscriptions, for patterns that reach one instance |
| RabbitMQ | ulo-rpc-rabbitmq | competing (queue) | yes | a RabbitMQ broker |
| Kafka | ulo-rpc-kafka | competing (consumer group) | yes | a Kafka broker; a C toolchain, since rdkafka builds librdkafka |
All links except ulo-rpc-tcp-smol run on tokio. Links compares what differs when the same handlers move between them.
In this section
- RPC controllers: messages, events, payloads, errors and guards.
- Streaming: streamed requests, streamed replies and duplex calls.
- Client:
RpcClient,RpcClientModule, headers and call options. - Deadlines: how a caller's remaining time travels with a call.
- Server: in-flight limits, queue limits and shutdown.
- Links, then one page per link.