@@ -3,7 +3,7 @@ use futures_cpupool::{CpuFuture, CpuPool};
3
3
use lazy_static:: lazy_static;
4
4
use state_machine_future:: { transition, RentToOwn , StateMachineFuture } ;
5
5
use std:: io;
6
- use std:: net:: { SocketAddr , ToSocketAddrs } ;
6
+ use std:: net:: { IpAddr , SocketAddr , ToSocketAddrs } ;
7
7
use std:: time:: Instant ;
8
8
use std:: vec;
9
9
use tokio_tcp:: TcpStream ;
@@ -23,8 +23,14 @@ lazy_static! {
23
23
#[ derive( StateMachineFuture ) ]
24
24
pub enum ConnectSocket {
25
25
#[ 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
+ ) ]
28
34
Start { config : Config , idx : usize } ,
29
35
#[ cfg( unix) ]
30
36
#[ state_machine_future( transitions( Finished ) ) ]
@@ -63,13 +69,25 @@ impl PollConnectSocket for ConnectSocket {
63
69
. unwrap_or ( & 5432 ) ;
64
70
65
71
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,
70
82
} ) ,
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
+ } ,
73
91
#[ cfg( unix) ]
74
92
Host :: Unix ( host) => {
75
93
let path = host. join ( format ! ( ".s.PGSQL.{}" , port) ) ;
0 commit comments