Skip to content

Commit

Permalink
fix: split connect timeout for multiple IPs (#1940)
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Oct 2, 2023
1 parent 17c893f commit 2a881fb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
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 @@ -283,7 +283,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(all(feature = "http3", feature = "__rustls"))]
let build_h3_connector =
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

0 comments on commit 2a881fb

Please sign in to comment.