Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request timeouts #1097

Closed
crackcomm opened this issue Mar 19, 2017 · 6 comments
Closed

Request timeouts #1097

crackcomm opened this issue Mar 19, 2017 · 6 comments

Comments

@crackcomm
Copy link

crackcomm commented Mar 19, 2017

Is it possible to configure request or HTTP client timeouts?
I am interested in master/tokio branch.

@seanmonstar
Copy link
Member

As of now, there isn't specific timeouts to set that are inside hyper. There are a few reasons why, but the main reason was because using futures makes it quite simple for anyone to deal with timeouts external to hyper. For example:

let timeout = tokio_core::reactor::Timeout::new(Duration:from_secs(30), &handle))?;
// a tokio timeout doesn't necessarily mean an error,
//  but in our case, always make it an error
// (it could also be used for things like "send this message after this amount of time")
let timeout = timeout.then(|_| Err(io::Error::new(io::ErrorKind::TimedOut, "our timeout")));
// set up our client request
let future_body = client.get(some_url).and_then(|res| {
    res.body().fold(Vec::new(), |mut vec, chunk| {
        vec.extend_from_slice(&chunk);
        Ok(vec)
    })
});

// select on the request and the timeout
// the first one to 'complete' will trigger our callback
// since we converted the timeout to always be an error,
// it will only ever be in the `Err` variant
let work = future_body.select(timeout).then(|result| {
    match result { 
        Ok(body) => {
            // do something with the body
        },
        Err(e) => {
            // could be an http  or io error (including our time out error)
            // inspect to find out which it was
        }
    }
});
handle.spawn(work);

@crackcomm
Copy link
Author

Thanks, that explains a lot.
I can't see any correlation between timeouts and request, how can I ensure that request is cancelled after timeout?

@carllerche
Copy link

carllerche commented Mar 19, 2017 via email

@crackcomm
Copy link
Author

@carllerche this is a great mechanism, thank you for this explanation and @seanmonstar thank you for this example.

@Trenson
Copy link

Trenson commented Jul 16, 2019

As of now, there isn't specific timeouts to set that are inside hyper. There are a few reasons why, but the main reason was because using futures makes it quite simple for anyone to deal with timeouts external to hyper. For example:

let timeout = tokio_core::reactor::Timeout::new(Duration:from_secs(30), &handle))?;
// a tokio timeout doesn't necessarily mean an error,
//  but in our case, always make it an error
// (it could also be used for things like "send this message after this amount of time")
let timeout = timeout.then(|_| Err(io::Error::new(io::ErrorKind::TimedOut, "our timeout")));
// set up our client request
let future_body = client.get(some_url).and_then(|res| {
    res.body().fold(Vec::new(), |mut vec, chunk| {
        vec.extend_from_slice(&chunk);
        Ok(vec)
    })
});

// select on the request and the timeout
// the first one to 'complete' will trigger our callback
// since we converted the timeout to always be an error,
// it will only ever be in the `Err` variant
let work = future_body.select(timeout).then(|result| {
    match result { 
        Ok(body) => {
            // do something with the body
        },
        Err(e) => {
            // could be an http  or io error (including our time out error)
            // inspect to find out which it was
        }
    }
});
handle.spawn(work);

Hello, I found your code have compile error.
let work = future_body.select(timeout).then(|result| {
| ^^^^^^ expected struct std::io::Error, found struct hyper::Error

@rafrafek
Copy link

Will slow download trigger timeout?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants