xref: /tonic/examples/src/cancellation/client.rs (revision 8cba85e2)
1 use hello_world::greeter_client::GreeterClient;
2 use hello_world::HelloRequest;
3 
4 use tokio::time::{timeout, Duration};
5 
6 pub mod hello_world {
7     tonic::include_proto!("helloworld");
8 }
9 
10 #[tokio::main]
main() -> Result<(), Box<dyn std::error::Error>>11 async fn main() -> Result<(), Box<dyn std::error::Error>> {
12     let mut client = GreeterClient::connect("http://[::1]:50051").await?;
13 
14     let request = tonic::Request::new(HelloRequest {
15         name: "Tonic".into(),
16     });
17 
18     // Cancelling the request by dropping the request future after 1 second
19     let response = match timeout(Duration::from_secs(1), client.say_hello(request)).await {
20         Ok(response) => response?,
21         Err(_) => {
22             println!("Cancelled request after 1s");
23             return Ok(());
24         }
25     };
26 
27     println!("RESPONSE={:?}", response);
28 
29     Ok(())
30 }
31