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

Allow a connection attempt to timeout and keep trying resolved addresses #618

Merged
merged 1 commit into from
Dec 9, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use log::debug;
use std::io::{self, BufRead, BufReader, Read, Write};
use std::net::SocketAddr;
use std::net::TcpStream;
use std::ops::Div;
use std::time::Duration;
use std::time::Instant;
use std::{fmt, io::Cursor};
Expand Down Expand Up @@ -366,10 +367,18 @@ pub(crate) fn connect_host(
let mut any_err = None;
let mut any_stream_and_addr = None;
// Find the first sock_addr that accepts a connection
let multiple_addrs = sock_addrs.len() > 1;

for sock_addr in sock_addrs {
// ensure connect timeout or overall timeout aren't yet hit.
let timeout = match connect_deadline {
Some(deadline) => Some(time_until_deadline(deadline)?),
Some(deadline) => {
let mut deadline = time_until_deadline(deadline)?;
if multiple_addrs {
deadline = deadline.div(2);
}
Some(deadline)
}
None => None,
};

Expand Down
29 changes: 28 additions & 1 deletion src/test/agent_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::error::Error;
use crate::testserver::{read_request, TestServer};
use std::io::{self, Read, Write};
use std::net::TcpStream;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
use std::thread;
use std::time::Duration;

Expand Down Expand Up @@ -111,6 +111,33 @@ fn custom_resolver() {
assert_eq!(&server.join().unwrap(), b"GET / HTTP/1.1\r\n");
}

#[test]
fn socket_addr_fail_over() {
use std::net::TcpListener;

let listener = TcpListener::bind("127.0.0.1:0").unwrap();

let local_addr = listener.local_addr().unwrap();
let non_routable_ipv4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(10, 255, 255, 1)), 9872);
let server = std::thread::spawn(move || {
let (mut client, _) = listener.accept().unwrap();
let mut buf = vec![0u8; 16];
let read = client.read(&mut buf).unwrap();
buf.truncate(read);
buf
});

AgentBuilder::new()
.resolver(move |_: &str| Ok(vec![non_routable_ipv4, local_addr]))
.timeout_connect(Duration::from_secs(2))
.build()
.get("http://cool.server/")
.call()
.ok();

assert_eq!(&server.join().unwrap(), b"GET / HTTP/1.1\r\n");
}

#[cfg(feature = "cookies")]
#[cfg(test)]
fn cookie_and_redirect(mut stream: TcpStream) -> io::Result<()> {
Expand Down