expression

Tonic: attach a deadline budget to an RPC request

Set the maximum duration on a request before dispatch. Tonic encodes this relative budget in grpc-timeout metadata. The receiver must honor it; this example constructs metadata only and does not send an RPC. Run in the accompanying Cargo project. Expected stdout (not executed): deadline metadata: true Common mistake: Assuming adding metadata itself starts a local timer.

Formal Syntax & Structure

rust syntaxrust
request.set_timeout(Duration::from_millis(250))

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread", start_paused = true)]async fn main() {    use std::time::Duration;    let mut request = tonic::Request::new(7_u64);    request.set_timeout(Duration::from_millis(250));    println!("deadline metadata: {}", request.metadata().get("grpc-timeout").is_some());}
Rust Reference