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:
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:
| Method | Default | Effect |
|---|---|---|
timeout(Bound) | 5 seconds | the 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) | none | the longest silence between reply frames of every stream and duplex that sets none |
max_queued_items(Count) | 1,024 | unread reply items held per stream |
max_queued_bytes(u64) | 4 MiB | the 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
| Method | Sends | Returns | Per-call options |
|---|---|---|---|
request(pattern, &req) | one request | one reply | header, timeout, within |
emit(pattern, &data) | one event | when the link has published it | header, within |
stream(pattern, &req) | one request | a stream of replies | header, idle_timeout, deadline, within |
send_stream(pattern, items) | a stream of items | one reply | header, timeout, within |
duplex(pattern, items) | a stream of items | a stream of replies | header, 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").awaitSome headers are the client's:
deadline-mscarries the remaining time. A caller that sets it with.headerfails the call when it is awaited, before connecting, withBadRequestand the reasonreserved_header.- On a call made
.withinan execution that carries a trace context,traceparentis written from that context and a caller-settraceparentis 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:
| Condition | Kind | Reason |
|---|---|---|
| no one is listening (see Links) | Unavailable | pattern_unhandled or no_destination |
| connection lost, or a broker refused | Unavailable | |
| the client's timeout passed | Timeout | |
| payload over the link's frame limit | BadRequest | payload_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_frame | BadRequest | payload_too_large |
| an exchange name, routing key or header name the link cannot carry (RabbitMQ's 255-byte limit) | BadRequest | name_too_long |
stream, send_stream or duplex on a link whose shapes omit it (UDP) | Unimplemented | shape_unsupported |
| binary payload on a JSON link | BadRequest | binary_unsupported |
| a reply stream the caller read too slowly | Unavailable | overflow |
a NATS or RabbitMQ broker that did not answer within connect_within | Unavailable | connect_timed_out |
a link misconfiguration found at connect, such as mqtts:// with no usable root certificates | Internal | misconfigured_link |
| the app's runtime has shut down | Unavailable | runtime_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.