Skip to content

Commit e3a25ad

Browse files
committed
Skip DNS lookup for IP addrs
1 parent d2723f5 commit e3a25ad

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

tokio-postgres/src/proto/connect_socket.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use futures_cpupool::{CpuFuture, CpuPool};
33
use lazy_static::lazy_static;
44
use state_machine_future::{transition, RentToOwn, StateMachineFuture};
55
use std::io;
6-
use std::net::{SocketAddr, ToSocketAddrs};
6+
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
77
use std::time::Instant;
88
use std::vec;
99
use tokio_tcp::TcpStream;
@@ -23,8 +23,14 @@ lazy_static! {
2323
#[derive(StateMachineFuture)]
2424
pub enum ConnectSocket {
2525
#[state_machine_future(start)]
26-
#[cfg_attr(unix, state_machine_future(transitions(ConnectingUnix, ResolvingDns)))]
27-
#[cfg_attr(not(unix), state_machine_future(transitions(ResolvingDns)))]
26+
#[cfg_attr(
27+
unix,
28+
state_machine_future(transitions(ConnectingUnix, ConnectingTcp, ResolvingDns))
29+
)]
30+
#[cfg_attr(
31+
not(unix),
32+
state_machine_future(transitions(ConnectingTcp, ResolvingDns))
33+
)]
2834
Start { config: Config, idx: usize },
2935
#[cfg(unix)]
3036
#[state_machine_future(transitions(Finished))]
@@ -63,13 +69,25 @@ impl PollConnectSocket for ConnectSocket {
6369
.unwrap_or(&5432);
6470

6571
match &state.config.0.host[state.idx] {
66-
Host::Tcp(host) => transition!(ResolvingDns {
67-
future: DNS_POOL.spawn_fn({
68-
let host = host.clone();
69-
move || (&*host, port).to_socket_addrs()
72+
Host::Tcp(host) => match host.parse::<IpAddr>() {
73+
Ok(addr) => transition!(ConnectingTcp {
74+
future: TcpStream::connect(&SocketAddr::new(addr, port)),
75+
timeout: state
76+
.config
77+
.0
78+
.connect_timeout
79+
.map(|d| Delay::new(Instant::now() + d)),
80+
addrs: vec![].into_iter(),
81+
config: state.config,
7082
}),
71-
config: state.config,
72-
}),
83+
Err(_) => transition!(ResolvingDns {
84+
future: DNS_POOL.spawn_fn({
85+
let host = host.clone();
86+
move || (&*host, port).to_socket_addrs()
87+
}),
88+
config: state.config,
89+
}),
90+
},
7391
#[cfg(unix)]
7492
Host::Unix(host) => {
7593
let path = host.join(format!(".s.PGSQL.{}", port));

0 commit comments

Comments
 (0)