Skip to content

Commit

Permalink
feat(client): skip dns resolution when host is a valid ip addr (#1372)
Browse files Browse the repository at this point in the history
  • Loading branch information
srijs authored and seanmonstar committed Nov 13, 2017
1 parent e330d30 commit b1785c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
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

0 comments on commit b1785c6

Please sign in to comment.