Skip to content

feat(client): skip dns resolution when host is a valid ip addr #1372

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

Merged
merged 1 commit into from
Nov 13, 2017
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
15 changes: 12 additions & 3 deletions src/client/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,18 @@ impl Future for HttpConnecting {
let state;
match self.state {
State::Lazy(ref executor, ref mut host, port) => {
let host = mem::replace(host, String::new());
let work = dns::Work::new(host, port);
state = State::Resolving(oneshot::spawn(work, executor));
// If the host is already an IP addr (v4 or v6),
// skip resolving the dns and start connecting right away.
if let Some(addrs) = dns::IpAddrs::try_parse(host, port) {
state = State::Connecting(ConnectingTcp {
addrs: addrs,
current: None
})
} else {
let host = mem::replace(host, String::new());
let work = dns::Work::new(host, port);
state = State::Resolving(oneshot::spawn(work, executor));
}
},
State::Resolving(ref mut future) => {
match try!(future.poll()) {
Expand Down
20 changes: 19 additions & 1 deletion src/client/dns.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::io;
use std::net::{SocketAddr, ToSocketAddrs};
use std::net::{
Ipv4Addr, Ipv6Addr,
SocketAddr, ToSocketAddrs,
SocketAddrV4, SocketAddrV6,
};
use std::vec;

use ::futures::{Async, Future, Poll};
Expand Down Expand Up @@ -30,6 +34,20 @@ pub struct IpAddrs {
iter: vec::IntoIter<SocketAddr>,
}

impl IpAddrs {
pub fn try_parse(host: &str, port: u16) -> Option<IpAddrs> {
if let Ok(addr) = host.parse::<Ipv4Addr>() {
let addr = SocketAddrV4::new(addr, port);
return Some(IpAddrs { iter: vec![SocketAddr::V4(addr)].into_iter() })
}
if let Ok(addr) = host.parse::<Ipv6Addr>() {
let addr = SocketAddrV6::new(addr, port, 0, 0);
return Some(IpAddrs { iter: vec![SocketAddr::V6(addr)].into_iter() })
}
None
}
}

impl Iterator for IpAddrs {
type Item = SocketAddr;
#[inline]
Expand Down