ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Protocols

OverviewRPC controllersStreamingClientDeadlinesServerLinksTCPUDPNATSRedisRabbitMQMQTTKafka

Integrations

Operating

Reference

Microservices

Client

RpcClient and RpcClientModule, the five call builders, headers, timeouts and the errors a caller sees.

RpcClient calls patterns on a server over any link, the transport that carries the call (see Links). Building one does no network I/O: it connects on its first call. One client multiplexes all its calls over one connection or one broker session.

In an application

RpcClientModule::for_root(link) binds an RpcClient and exports it. Keying it with a marker type lets two clients coexist. This is examples/27-rpc-deadlines:

src/lib.rs
use ulo::{Dep, Timer, controller, module};
use ulo_rpc::{RpcClient, RpcClientModule};
use ulo_rpc_tcp::Tcp;

/// Names the pricing service's client where more than one is injected.
pub struct Pricing;

#[controller]
pub struct Gateway {
    pricing: Dep<RpcClient, Pricing>,
    timer: Dep<dyn Timer>,
}

#[derive(Clone, PartialEq, Eq, Hash)]
#[module(
    imports = [RpcClientModule::for_root(Tcp::new(self.callee)).keyed::<Pricing>()],
    controllers = [Gateway],
)]
pub struct GatewayModule {
    pub callee: SocketAddr,
}

The app must have a runtime: the client spawns its tasks on the app's runtime and times its calls with the app's timer. The module's destroy hook closes the link when the app closes. A link that can never connect, such as a tokio link built outside a tokio runtime, fails connect with UnusableLink naming .with_handle(..).

Module settings:

MethodDefaultEffect
timeout(Bound)5 secondsthe whole-call bound of every request and send_stream that sets none, sent as deadline-ms, and the bound on an emit's publish
idle_timeout(Bound)nonethe longest silence between reply frames of every stream and duplex that sets none
max_queued_items(Count)1,024unread reply items held per stream
max_queued_bytes(u64)4 MiBthe encoded bytes of the unread reply items held per stream

Bound and Count are re-exported from ulo_rpc::config. Bound::After(Duration::ZERO), Count::Max(0) and max_queued_bytes(0) are refused when the app wires.

Outside an application

RpcClient::new(link, runtime) takes the link by value and the runtime as Arc<dyn Runtime>. From examples/25-rpc-basics/tests/rpc.rs:

fn client(addr: SocketAddr) -> RpcClient {
    RpcClient::new(Tcp::new(addr), Arc::new(Tokio::current())).expect("the link is usable")
}

RpcClient::timeout(Bound), idle_timeout(Bound), max_queued_items(Count) and max_queued_bytes(u64) on the handle return a Result and mirror the module's settings. max_queued_bytes(0) is Err(ZeroBytes).

Calls

MethodSendsReturnsPer-call options
request(pattern, &req)one requestone replyheader, timeout, within
emit(pattern, &data)one eventwhen the link has published itheader, within
stream(pattern, &req)one requesta stream of repliesheader, idle_timeout, deadline, within
send_stream(pattern, items)a stream of itemsone replyheader, timeout, within
duplex(pattern, items)a stream of itemsa stream of repliesheader, idle_timeout, deadline, within

Each is a builder that sends when awaited. request::<Req, Res> takes the reply type as its second parameter. Calls are described in Streaming and Deadlines.

Headers travel with the call, in the frame on TCP, UDP and Redis and as the broker's own headers on NATS, RabbitMQ, MQTT and Kafka. The handler reads them through CallHeaders or RpcCx::headers(). From examples/25-rpc-basics/tests/rpc.rs:

client.request::<_, usize>("calc.clear", &()).header("x-role", "admin").await

Some headers are the client's:

  • deadline-ms carries the remaining time. A caller that sets it with .header fails the call when it is awaited, before connecting, with BadRequest and the reason reserved_header.
  • On a call made .within an execution that carries a trace context, traceparent is written from that context and a caller-set traceparent is refused the same way. Outside such a call it is an ordinary header.

Errors

Every failure is an RpcError with kind(), message(), details() and reason(). The server's err frame arrives as the same type. A handler's error keeps its kind. The client adds the failures a link can signal, mapped the same on every link:

ConditionKindReason
no one is listening (see Links)Unavailablepattern_unhandled or no_destination
connection lost, or a broker refusedUnavailable
the client's timeout passedTimeout
payload over the link's frame limitBadRequestpayload_too_large
a reply item over max_queued_bytes on its own, or a server's refusal of an item over its own bound or of a TCP frame over its max_frameBadRequestpayload_too_large
an exchange name, routing key or header name the link cannot carry (RabbitMQ's 255-byte limit)BadRequestname_too_long
stream, send_stream or duplex on a link whose shapes omit it (UDP)Unimplementedshape_unsupported
binary payload on a JSON linkBadRequestbinary_unsupported
a reply stream the caller read too slowlyUnavailableoverflow
a NATS or RabbitMQ broker that did not answer within connect_withinUnavailableconnect_timed_out
a link misconfiguration found at connect, such as mqtts:// with no usable root certificatesInternalmisconfigured_link
the app's runtime has shut downUnavailableruntime_shut_down

Any other failed connect is Unavailable. The next call tries again.

Codec

Every link is JSON unless built with .codec(Codec::Cbor). CBOR carries Bytes and Payload<Bytes> as raw byte strings and is chosen per link, on both sides: a server and a client must agree. A handler with a binary payload on a JSON link is refused when the server prepares, and a client call with one fails before any I/O.

Streaming

Streamed requests, streamed replies and duplex calls, picked by the handler's signature, with the client calls and timeouts that go with them.

Deadlines

How a caller's remaining time travels with an RPC call, and what the callee does when it runs out.

On this page

In an applicationOutside an applicationCallsErrorsCodec