Skip to content

How to implement hyper request timeout #2449

Answered by davidbarsky
kushwahashiv asked this question in Q&A
Discussion options

You must be logged in to vote

Does something like this help?

use hyper::{Client, Uri};
use tokio::time::timeout;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    let client = Client::new();
    let future = client.get(Uri::from_static("http://httpbin.org/ip"));
    
    match timeout(Duration::from_millis(30), future).await {
        Ok(_) => println!("Got response"),
        Err(_) => println!("timed out"),
    };
    
    Ok(())
}

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@rafrafek
Comment options

@seanmonstar
Comment options

Answer selected by kushwahashiv
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #2448 on March 01, 2021 19:04.