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

connect timeout for multiple IPs #1940

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -277,10 +277,11 @@ jobs:
with:
toolchain: ${{ steps.metadata.outputs.msrv }}

- name: Make sure log v0.4.18 is used
- name: Fix log and tokio versions
run: |
cargo update
cargo update -p log --precise 0.4.18
cargo update -p tokio --precise 1.29.1

- uses: Swatinem/rust-cache@v2

Expand Down
3 changes: 2 additions & 1 deletion src/async_impl/client.rs
Expand Up @@ -279,7 +279,8 @@ impl ClientBuilder {
config.dns_overrides,
));
}
let http = HttpConnector::new_with_resolver(DynResolver::new(resolver.clone()));
let mut http = HttpConnector::new_with_resolver(DynResolver::new(resolver.clone()));
http.set_connect_timeout(config.connect_timeout);

#[cfg(feature = "__tls")]
match config.tls {
Expand Down
5 changes: 5 additions & 0 deletions src/error.rs
Expand Up @@ -105,6 +105,11 @@ impl Error {
if err.is::<TimedOut>() {
return true;
}
if let Some(io) = err.downcast_ref::<io::Error>() {
if io.kind() == io::ErrorKind::TimedOut {
return true;
}
}
source = err.source();
}

Expand Down
57 changes: 57 additions & 0 deletions tests/timeouts.rs
Expand Up @@ -86,6 +86,63 @@ async fn connect_timeout() {
assert!(err.is_connect() && err.is_timeout());
}

#[cfg(not(target_arch = "wasm32"))]
#[tokio::test]
async fn connect_many_timeout_succeeds() {
let _ = env_logger::try_init();

let server = server::http(move |_req| async { http::Response::default() });
let port = server.addr().port();

let client = reqwest::Client::builder()
.resolve_to_addrs(
"many_addrs",
&["10.255.255.1:81".parse().unwrap(), server.addr()],
)
.connect_timeout(Duration::from_millis(100))
.build()
.unwrap();

let url = format!("http://many_addrs:{port}/eventual");

let _res = client
.get(url)
.timeout(Duration::from_millis(1000))
.send()
.await
.unwrap();
}

#[cfg(not(target_arch = "wasm32"))]
#[tokio::test]
async fn connect_many_timeout() {
let _ = env_logger::try_init();

let client = reqwest::Client::builder()
.resolve_to_addrs(
"many_addrs",
&[
"10.255.255.1:81".parse().unwrap(),
"10.255.255.2:81".parse().unwrap(),
],
)
.connect_timeout(Duration::from_millis(100))
.build()
.unwrap();

let url = format!("http://many_addrs:81/slow");

let res = client
.get(url)
.timeout(Duration::from_millis(1000))
.send()
.await;

let err = res.unwrap_err();

assert!(err.is_connect() && err.is_timeout());
}

#[tokio::test]
async fn response_timeout() {
let _ = env_logger::try_init();
Expand Down