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

feat(client): Add connect timeout to HttpConnector (0.12 backport) #1986

Merged
merged 1 commit into from Oct 22, 2019
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
4 changes: 4 additions & 0 deletions src/client/connect/dns.rs
Expand Up @@ -261,6 +261,10 @@ impl IpAddrs {
pub(super) fn is_empty(&self) -> bool {
self.iter.as_slice().is_empty()
}

pub(super) fn len(&self) -> usize {
self.iter.as_slice().len()
}
}

impl Iterator for IpAddrs {
Expand Down
73 changes: 58 additions & 15 deletions src/client/connect/http.rs
Expand Up @@ -12,7 +12,7 @@ use http::uri::Scheme;
use net2::TcpBuilder;
use tokio_reactor::Handle;
use tokio_tcp::{TcpStream, ConnectFuture};
use tokio_timer::Delay;
use tokio_timer::{Delay, Timeout};

use super::{Connect, Connected, Destination};
use super::dns::{self, GaiResolver, Resolve, TokioThreadpoolGaiResolver};
Expand All @@ -29,6 +29,7 @@ use super::dns::{self, GaiResolver, Resolve, TokioThreadpoolGaiResolver};
pub struct HttpConnector<R = GaiResolver> {
enforce_http: bool,
handle: Option<Handle>,
connect_timeout: Option<Duration>,
happy_eyeballs_timeout: Option<Duration>,
keep_alive_timeout: Option<Duration>,
local_address: Option<IpAddr>,
Expand Down Expand Up @@ -120,6 +121,7 @@ impl<R> HttpConnector<R> {
HttpConnector {
enforce_http: true,
handle: None,
connect_timeout: None,
happy_eyeballs_timeout: Some(Duration::from_millis(300)),
keep_alive_timeout: None,
local_address: None,
Expand Down Expand Up @@ -187,6 +189,17 @@ impl<R> HttpConnector<R> {
self.local_address = addr;
}

/// Set the connect timeout.
///
/// If a domain resolves to multiple IP addresses, the timeout will be
/// evenly divided across them.
///
/// Default is `None`.
#[inline]
pub fn set_connect_timeout(&mut self, dur: Option<Duration>) {
self.connect_timeout = dur;
}

/// Set timeout for [RFC 6555 (Happy Eyeballs)][RFC 6555] algorithm.
///
/// If hostname resolves to both IPv4 and IPv6 addresses and connection
Expand Down Expand Up @@ -259,6 +272,7 @@ where
HttpConnecting {
state: State::Lazy(self.resolver.clone(), host.into(), self.local_address),
handle: self.handle.clone(),
connect_timeout: self.connect_timeout,
happy_eyeballs_timeout: self.happy_eyeballs_timeout,
keep_alive_timeout: self.keep_alive_timeout,
nodelay: self.nodelay,
Expand All @@ -285,6 +299,7 @@ fn invalid_url<R: Resolve>(err: InvalidUrl, handle: &Option<Handle>) -> HttpConn
keep_alive_timeout: None,
nodelay: false,
port: 0,
connect_timeout: None,
happy_eyeballs_timeout: None,
reuse_address: false,
send_buffer_size: None,
Expand Down Expand Up @@ -319,6 +334,7 @@ impl StdError for InvalidUrl {
pub struct HttpConnecting<R: Resolve = GaiResolver> {
state: State<R>,
handle: Option<Handle>,
connect_timeout: Option<Duration>,
happy_eyeballs_timeout: Option<Duration>,
keep_alive_timeout: Option<Duration>,
nodelay: bool,
Expand Down Expand Up @@ -348,7 +364,7 @@ impl<R: Resolve> Future for HttpConnecting<R> {
// skip resolving the dns and start connecting right away.
if let Some(addrs) = dns::IpAddrs::try_parse(host, self.port) {
state = State::Connecting(ConnectingTcp::new(
local_addr, addrs, self.happy_eyeballs_timeout, self.reuse_address));
local_addr, addrs, self.connect_timeout, self.happy_eyeballs_timeout, self.reuse_address));
} else {
let name = dns::Name::new(mem::replace(host, String::new()));
state = State::Resolving(resolver.resolve(name), local_addr);
Expand All @@ -364,7 +380,7 @@ impl<R: Resolve> Future for HttpConnecting<R> {
.collect();
let addrs = dns::IpAddrs::new(addrs);
state = State::Connecting(ConnectingTcp::new(
local_addr, addrs, self.happy_eyeballs_timeout, self.reuse_address));
local_addr, addrs, self.connect_timeout, self.happy_eyeballs_timeout, self.reuse_address));
}
};
},
Expand Down Expand Up @@ -417,6 +433,7 @@ impl ConnectingTcp {
fn new(
local_addr: Option<IpAddr>,
remote_addrs: dns::IpAddrs,
connect_timeout: Option<Duration>,
fallback_timeout: Option<Duration>,
reuse_address: bool,
) -> ConnectingTcp {
Expand All @@ -425,25 +442,25 @@ impl ConnectingTcp {
if fallback_addrs.is_empty() {
return ConnectingTcp {
local_addr,
preferred: ConnectingTcpRemote::new(preferred_addrs),
preferred: ConnectingTcpRemote::new(preferred_addrs, connect_timeout),
fallback: None,
reuse_address,
};
}

ConnectingTcp {
local_addr,
preferred: ConnectingTcpRemote::new(preferred_addrs),
preferred: ConnectingTcpRemote::new(preferred_addrs, connect_timeout),
fallback: Some(ConnectingTcpFallback {
delay: Delay::new(Instant::now() + fallback_timeout),
remote: ConnectingTcpRemote::new(fallback_addrs),
remote: ConnectingTcpRemote::new(fallback_addrs, connect_timeout),
}),
reuse_address,
}
} else {
ConnectingTcp {
local_addr,
preferred: ConnectingTcpRemote::new(remote_addrs),
preferred: ConnectingTcpRemote::new(remote_addrs, connect_timeout),
fallback: None,
reuse_address,
}
Expand All @@ -458,13 +475,17 @@ struct ConnectingTcpFallback {

struct ConnectingTcpRemote {
addrs: dns::IpAddrs,
current: Option<ConnectFuture>,
connect_timeout: Option<Duration>,
current: Option<MaybeTimedConnectFuture>,
}

impl ConnectingTcpRemote {
fn new(addrs: dns::IpAddrs) -> Self {
fn new(addrs: dns::IpAddrs, connect_timeout: Option<Duration>) -> Self {
let connect_timeout = connect_timeout.map(|t| t / (addrs.len() as u32));

Self {
addrs,
connect_timeout,
current: None,
}
}
Expand All @@ -481,7 +502,18 @@ impl ConnectingTcpRemote {
let mut err = None;
loop {
if let Some(ref mut current) = self.current {
match current.poll() {
let poll: Poll<TcpStream, io::Error> = match current {
MaybeTimedConnectFuture::Timed(future) => match future.poll() {
Ok(tcp) => Ok(tcp),
Err(err) => if err.is_inner() {
Err(err.into_inner().unwrap())
} else {
Err(io::Error::new(io::ErrorKind::TimedOut, err.description()))
}
},
MaybeTimedConnectFuture::Untimed(future) => future.poll(),
};
match poll {
Ok(Async::Ready(tcp)) => {
debug!("connected to {:?}", tcp.peer_addr().ok());
return Ok(Async::Ready(tcp));
Expand All @@ -492,14 +524,14 @@ impl ConnectingTcpRemote {
err = Some(e);
if let Some(addr) = self.addrs.next() {
debug!("connecting to {}", addr);
*current = connect(&addr, local_addr, handle, reuse_address)?;
*current = connect(&addr, local_addr, handle, reuse_address, self.connect_timeout)?;
continue;
}
}
}
} else if let Some(addr) = self.addrs.next() {
debug!("connecting to {}", addr);
self.current = Some(connect(&addr, local_addr, handle, reuse_address)?);
self.current = Some(connect(&addr, local_addr, handle, reuse_address, self.connect_timeout)?);
continue;
}

Expand All @@ -508,7 +540,12 @@ impl ConnectingTcpRemote {
}
}

fn connect(addr: &SocketAddr, local_addr: &Option<IpAddr>, handle: &Option<Handle>, reuse_address: bool) -> io::Result<ConnectFuture> {
enum MaybeTimedConnectFuture {
Timed(Timeout<ConnectFuture>),
Untimed(ConnectFuture),
}

fn connect(addr: &SocketAddr, local_addr: &Option<IpAddr>, handle: &Option<Handle>, reuse_address: bool, connect_timeout: Option<Duration>) -> io::Result<MaybeTimedConnectFuture> {
let builder = match addr {
&SocketAddr::V4(_) => TcpBuilder::new_v4()?,
&SocketAddr::V6(_) => TcpBuilder::new_v6()?,
Expand Down Expand Up @@ -540,7 +577,13 @@ fn connect(addr: &SocketAddr, local_addr: &Option<IpAddr>, handle: &Option<Handl
None => Cow::Owned(Handle::default()),
};

Ok(TcpStream::connect_std(builder.to_tcp_stream()?, addr, &handle))
let stream = TcpStream::connect_std(builder.to_tcp_stream()?, addr, &handle);

if let Some(timeout) = connect_timeout {
Ok(MaybeTimedConnectFuture::Timed(Timeout::new(stream, timeout)))
} else {
Ok(MaybeTimedConnectFuture::Untimed(stream))
}
}

impl ConnectingTcp {
Expand Down Expand Up @@ -706,7 +749,7 @@ mod tests {
}

let addrs = hosts.iter().map(|host| (host.clone(), addr.port()).into()).collect();
let connecting_tcp = ConnectingTcp::new(None, dns::IpAddrs::new(addrs), Some(fallback_timeout), false);
let connecting_tcp = ConnectingTcp::new(None, dns::IpAddrs::new(addrs), None, Some(fallback_timeout), false);
let fut = ConnectingTcpFuture(connecting_tcp);

let start = Instant::now();
Expand Down