Links
The same handlers over TCP, UDP, NATS, Redis, MQTT, RabbitMQ and Kafka, and what differs between the links.
A link is the crate that carries RPC frames: Tcp, Udp, Nats, Redis, Mqtt, RabbitMq or Kafka. A link goes into Server::new(link) on the server and into RpcClient::new(link, runtime) or RpcClientModule::for_root(link) on the client. Controllers do not name a link.
examples/28-rpc-links serves the handlers of examples/25-rpc-basics over any of the six wire links, chosen by a URL scheme:
pub fn bind(app: App<Connected>, url: &str) -> Result<App<Connected>, Box<dyn Error + Send + Sync>> {
let (scheme, address) = split(url)?;
Ok(match scheme {
"tcp" => app.bind(Server::new(Tcp::new(address))),
"nats" => app.bind(Server::new(Nats::url(url))),
"redis" => app.bind(Server::new(Redis::url(url))),
"mqtt" => app.bind(Server::new(Mqtt::url(url))),
"amqp" => app.bind(Server::new(RabbitMq::url(url))),
"kafka" => app.bind(Server::new(Kafka::brokers(address))),
other => return Err(unknown(other)),
})
}cargo test -p ex-28-rpc-links
RPC_LINK=nats://127.0.0.1:4222 cargo run -p ex-28-rpc-linksWithout RPC_LINK the handlers listen on TCP at PORT. The broker tests start each broker in a container and need Docker:
cargo test -p ex-28-rpc-links --features integration --test brokersEach link crate runs the same conformance suite from ulo-rpc-conformance, which is published. A behaviour listed on this page as common to all links is held by a test per link, and a link written elsewhere can run the same suite.
What every link does alike
- Replies, handler errors with their kind and message, and events behave as in RPC controllers.
- Failures a link can signal reach the caller as the same
RpcErrorkinds (see Client). - Calls carry
deadline-msand streams carrycancel(see Deadlines). - A call refused because the server is draining is
unavailablewith the phrase "the server is shutting down". - The default codec is JSON.
.codec(Codec::Cbor)selects CBOR and binary payloads on every link.
The frames are one grammar on every link. A request is req, an event evt, a reply res or err, a reply stream item then end, and a streamed request opens with open and continues with in and in_end. TCP, UDP and Redis carry whole frames. On NATS, RabbitMQ, MQTT and Kafka the payload is the message body, headers are the broker's headers, and the correlation is the broker's own. A request published by hand with a reply address therefore reaches a handler: nats req invoices.create '{..}'.
What differs
| TCP | UDP | NATS | Redis | MQTT | RabbitMQ | Kafka | |
|---|---|---|---|---|---|---|---|
| URL / constructor | Tcp::new(addr) | Udp::new(addr) | Nats::url("nats://..") | Redis::url("redis://..") | Mqtt::url("mqtt://..") | RabbitMq::url("amqp://..") | Kafka::brokers("host:9092") |
| Pattern is | in the frame | in the frame | a subject | a channel | a topic | a queue | a topic |
| Streamed shapes | yes | no | yes | yes | yes | yes | yes |
| Request delivery to instances | addressed | addressed | competing (queue group) | every instance | competing ($share) | competing | competing (consumer group) |
| Event delivery to instances | addressed | addressed | one (queue group) | every instance | one ($share) | one (queue) | one (consumer group) |
Event declared every_instance | refused at listen | refused at listen | every instance | every instance | every instance | every instance | every instance |
| Order | per connection | none | per publisher and subject | per channel | per topic and QoS | per queue | per partition |
| Request guarantee | connection | none | at most once | at most once | by QoS (1 by default) | at least once, ack after the handler | offset stored after the handler |
| Largest frame | 1 MiB, max_frame | 65,507 bytes | the server's max payload | none declared | CONNACK max packet | none declared | 1,000,000 bytes |
| TLS | .tls(Tls) on the server | none | tls:// | rediss://, feature tls | mqtts:// | amqps:// | ssl://, feature tls |
| Unhandled pattern | unavailable, pattern_unhandled | unavailable, pattern_unhandled | unavailable, no_destination | unavailable, no_destination | unavailable, no_destination at QoS 1 and 2 | unavailable, no_destination unless the queue exists | caller Timeout |
| Drain is confirmed | yes | no | no | yes | yes | yes | nothing to confirm |
A pattern nothing handles
The caller's error depends on whether the link can tell. TCP and UDP answer from the server. A broker routes by subscription. A request for an unhandled pattern never reaches this server, and the client maps the broker's "no destination" signal to the same kind:
- NATS: a no-responders status.
- Redis: a
PUBLISHreceiver count of zero. - MQTT: a PUBACK or PUBREC with reason code 0x10 at QoS 1 and 2. At QoS 0 nothing is acknowledged, so the caller sees its own
Timeout. - RabbitMQ: a returned
mandatorypublish, for a pattern whose queue no server ever declared. A queue outlives its server, so a request for a pattern that was once served waits in the queue for the next instance and the caller sees its ownTimeout. - Kafka: nothing signals the miss. With the broker's
auto.create.topics.enableon, the default, a publish to an unhandled pattern creates the topic and the caller waits out itsTimeout.
Where the miss is the caller's own Timeout, only the link's declaration shows the difference: RabbitMQ and Kafka hold requests that arrive while no instance is consuming (the unserved_hold capability), and the next instance runs them.
Delivery to two instances
With two instances of one application on a broker, a request should run once, and so should an event.
- TCP and UDP are addressed: the caller names the server.
- NATS subscribes through a queue group and MQTT through a shared subscription
$share/<group>/<topic>. RabbitMQ queues and Kafka consumer groups compete by construction. The group defaults to the application's root module's full type path and.group(..)overrides it on NATS, MQTT and Kafka. Two instances of one application share a group, and two different applications on one broker never compete for each other's patterns. - Redis Pub/Sub has no competing mode. Every server instance subscribed to a pattern runs a request's handler, and the client drops all but the first reply. Redis therefore supports one server instance for requests and for events left at the default, and the server logs a warning when it mounts either on it.
An event reaches one instance unless its handler is declared #[ulo_rpc::event("pattern", every_instance)], for a cache invalidation or a configuration reload. Such an event reaches every instance on NATS (a plain subscription), MQTT (a plain topic filter), RabbitMQ (a queue of the instance's own on the pattern's fanout exchange), Kafka (a consumer of the instance's own, outside the group) and Redis. TCP and UDP refuse the declaration when the server prepares, because the caller names one server.
Shapes and events
UDP is unary-only: a server that mounts a handler with a streamed shape is refused at listen, and a client refuses a streamed call before any I/O with Unimplemented and the reason shape_unsupported. A stream call is refused too, although a unary handler would answer it with one reply: request is the call for that. Every other link carries all four shapes. A streamed request on a broker opens with an empty body and a header ulo-t: open; the server answers ulo-t: opened before the caller sends anything more. The items, the end and any cancel travel on a control channel every instance reads (ulo.rpc.control on NATS, RabbitMQ and Kafka, ulo/rpc/control on MQTT, ulo:rpc:control on Redis).