expression

gRPC: cap the wait for a slow stream consumer

A bounded queue alone can leave a producer waiting indefinitely. A per-send budget limits that wait; use the remaining overall RPC budget as an upper bound. The fixture leaves a one-item queue full, with no network or wall-clock wait. Run in the accompanying Cargo project. Expected stdout (not executed): timed out: true queued: 7 Common mistake: Dropping the timed-out item silently when delivery is required.

Formal Syntax & Structure

rust syntaxrust
sender.send_timeout(item, budget).await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() {    use tokio::{sync::mpsc, time::Duration};    let (tx, mut rx) = mpsc::channel(1);    tx.send(7_u64).await.unwrap();    let err = tx.send_timeout(8, Duration::from_millis(10)).await.unwrap_err();    println!("timed out: {}", matches!(err, mpsc::error::SendTimeoutError::Timeout(8)));    println!("queued: {}", rx.recv().await.unwrap());}
Rust Reference