with_deadline

Function with_deadline 

Source
pub fn with_deadline<S, T>(
    stream: S,
    deadline: Instant,
    operation: &'static str,
) -> BoxStream<'static, Result<T, Status>>
where S: Stream<Item = Result<T, Status>> + Send + 'static, T: Send + 'static,
Expand description

Wrap a server-streaming response with a wall-clock deadline.

Guarantee: when the deadline fires, the inner stream is dropped and its resources (Bigtable permits, in-flight RPCs, render buffers, blocking scan workers) are released in real time — even if the gRPC consumer has stopped pulling frames. The DeadlineExceeded Status itself is delivered on the next poll from tonic, which may be later if the h2 send window is closed.

The naive design — race deadline and inner.next() in a single select! inside the wrapper — fails when tonic’s task is parked at its h2-write await: timer wakes hit the task but resume at the wrong await point, and the wrapper’s select never runs. Spawning gives the drain loop its own task whose only outer await is timeout_at(...), so deadline wakes always land where they can cancel.

The mpsc(1) channel is just the bridge between two polling roots (tonic ↔ our spawn). Capacity 1 = tightest backpressure; per-item wake overhead is negligible against IO/render cost.

Shared by both the fullnode (sui-rpc-api) and bigtable (sui-kv-rpc) ledger-history streaming services so they enforce deadlines identically.