ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Protocols

OverviewRPC controllersStreamingClientDeadlinesServerLinksTCPUDPNATSRedisRabbitMQMQTTKafka

Integrations

Operating

Reference

Microservices

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.

Cargo.toml
[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"] }
src/lib.rs
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(())
    }
}
src/main.rs
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

LinkCrateDeliveryStreamed shapesNeeds
TCPulo-rpc-tcp, ulo-rpc-tcp-smolthe caller names the serveryesnothing
UDPulo-rpc-udpthe caller names the servernonothing
NATSulo-rpc-natscompeting (queue group)yesa NATS server
Redisulo-rpc-redisevery subscribed instanceyesa Redis server; one server instance for requests and for events left at the default
MQTTulo-rpc-mqttcompeting (shared subscription)yesan MQTT v5 broker with shared subscriptions, for patterns that reach one instance
RabbitMQulo-rpc-rabbitmqcompeting (queue)yesa RabbitMQ broker
Kafkaulo-rpc-kafkacompeting (consumer group)yesa 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.

Limits and shutdown

Message and connection limits, close codes, keep-alive and how a WebSocket server drains.

RPC controllers

Messages that answer their caller, events that answer nobody, payload extraction, error kinds and guards.

On this page

A first serverA first callChoosing a linkIn this section