Prefer IPv4 over IPv6 when possible

This commit is contained in:
Dennis Schwerdel 2016-08-12 08:39:04 +02:00
parent 6c61ca61a8
commit 7a4efa9257
1 changed files with 5 additions and 0 deletions

View File

@ -127,6 +127,11 @@ pub fn resolve<Addr: ToSocketAddrs+fmt::Display>(addr: Addr) -> Result<Vec<Socke
let addrs = try!(addr.to_socket_addrs().map_err(|_| Error::Name(format!("{}", addr)))); let addrs = try!(addr.to_socket_addrs().map_err(|_| Error::Name(format!("{}", addr))));
// Remove duplicates in addrs (why are there duplicates???) // Remove duplicates in addrs (why are there duplicates???)
let mut addrs = addrs.collect::<Vec<_>>(); let mut addrs = addrs.collect::<Vec<_>>();
// Try IPv4 first as it usually is faster
addrs.sort_by_key(|addr| match *addr {
SocketAddr::V4(_) => 4,
SocketAddr::V6(_) => 6
});
addrs.dedup(); addrs.dedup();
Ok(addrs) Ok(addrs)
} }