expression

gRPC: bound the entire response consumption period

A fresh timeout on every message is not a total stream deadline. Reuse an absolute deadline for each receive operation and stop on expiry. This local channel model receives one message and then reaches the deadline while waiting for the next. It does not exercise tonic::Streaming transport. Run in the accompanying Cargo project. Expected stdout (not executed): item: 7 expired: true Common mistake: Renewing the total deadline on every successful message and allowing an endless stream.

Formal Syntax & Structure

rust syntaxrust
timeout_at(deadline, receiver.recv()).await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() {    use tokio::{sync::mpsc, time::{timeout_at, Duration, Instant}};    let (tx, mut rx) = mpsc::channel(1);    tx.send(7_u64).await.unwrap();    let deadline = Instant::now() + Duration::from_millis(10);    println!("item: {}", timeout_at(deadline, rx.recv()).await.unwrap().unwrap());    println!("expired: {}", timeout_at(deadline, rx.recv()).await.is_err());    drop(tx);}
Rust Reference