ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Protocols

OverviewRPC controllersStreamingClientDeadlinesServerLinksTCPUDPNATSRedisRabbitMQMQTTKafka

Integrations

Operating

Reference

Microservices

Streaming

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

The handler's signature picks the call shape. An Inbound<T> parameter is a streamed request. A stream return is a streamed reply. Both together are a duplex call. The shape is read from the types, so an alias or an impl Stream return counts. This page follows examples/26-rpc-streams.

HandlerShapeClient call
Payload<T> in, value outunaryrequest
Payload<T> in, stream outstreamed replystream
Inbound<T> in, value outstreamed requestsend_stream
Inbound<T> in, stream outduplexduplex

Handlers

src/lib.rs
#[handlers]
impl Streams {
    #[ulo_rpc::message("stream.up")]
    async fn up(&self, n: Payload<u32>) -> impl Stream<Item = Result<u32, BadItem>> {
        stream::iter((1..=n.0).map(Ok))
    }

    #[ulo_rpc::message("stream.sum")]
    async fn sum(&self, mut items: Inbound<i64>) -> Result<i64, BadItem> {
        let mut total = 0;
        while let Some(item) = items.next().await {
            total += item.map_err(|_| BadItem)?;
        }
        Ok(total)
    }

    #[ulo_rpc::message("stream.double")]
    async fn double(&self, items: Inbound<i64>) -> impl Stream<Item = Result<i64, BadItem>> {
        items.map(|item| item.map(|n| n * 2).map_err(|_| BadItem))
    }
}

Inbound<T> is a stream of Result<T, _>: an item that does not decode, or a request that ends abnormally, arrives as an Err. A reply stream yields Result<U, E> items. An item that is an Err ends the stream with that error's kind. A stream of bare items is not an answer.

A streamed reply ends cleanly with an end frame and ends with an error when an item is an Err, the call is cut off or the link is lost.

Calling

From examples/26-rpc-streams/tests/streams.rs:

// streamed reply
let replies = client.stream::<_, u32>("stream.up", &3u32).await?;
let items: Vec<_> = replies.collect().await;                 // 1, 2, 3

// streamed request: answered once the stream ends
let total = client.send_stream::<_, i64>("stream.sum", stream::iter([1i64, 2, 3, 4])).await?;

// duplex
let replies = client.duplex::<_, i64>("stream.double", stream::iter([1i64, 2, 3])).await?;

Items arrive in order. Each reply item is a Result<T, RpcError>.

Timeouts

A stream has no timeout of its own unless the call sets one:

CallMethodBoundsSent to the server
stream, duplexidle_timeout(Bound)the silence between reply framesno
stream, duplexdeadline(Duration)the whole callyes, as deadline-ms
request, send_streamtimeout(Duration)the whole callyes, as deadline-ms

Each ends the call with the kind Timeout. An idle timeout is the client's alone, so its message is "no reply frame arrived within the stream's idle timeout". A deadline or a send_stream timeout is also sent to the server, which may end the call first.

let call = client.stream::<_, u32>("stream.stall", &()).idle_timeout(Bound::After(Duration::from_millis(150)));
let call = client.stream::<_, u64>("stream.ticks", &()).deadline(Duration::from_millis(300));
let total = client.send_stream::<_, i64>("stream.sum", items).timeout(Duration::from_millis(150)).await;

timeout exists on request and send_stream only, and stream(..).timeout(d) does not compile. RpcClientModule::idle_timeout(..) sets a default idle timeout for every stream the client makes. None is set unless configured. A lost connection fails a waiting stream with Unavailable whatever the idle timeout says.

Cancellation and backpressure

Dropping a reply stream before it ends sends a cancel for the call, which cancels the handler's execution with the reason ClientCancelled. On the server a handler reading an Inbound sees the caller's cancel or a lost link the same way.

Queues are bounded by count and by bytes: the server holds up to max_queued_items unread items of one streamed request (1,024 by default) and max_queued_bytes of their encoded payloads (4 MiB). The client holds the same bounds on unread reply items per stream. The item past either limit ends that call with unavailable and the reason overflow. An item over the byte bound on its own ends the call with bad_request and the reason payload_too_large, since a retry sends the same item again. See Server and Client.

Links

Every link except UDP carries all four shapes. A UDP server refuses a handler with a streamed shape at startup. A UDP client refuses stream, send_stream and duplex before any I/O with Unimplemented and the reason shape_unsupported. See UDP.

RPC controllers

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

Client

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

On this page

HandlersCallingTimeoutsCancellation and backpressureLinks