11use crate :: config:: Host ;
22use crate :: { Error , Socket } ;
3- use futures:: channel:: oneshot;
4- use futures:: future;
53use std:: future:: Future ;
6- use std:: net :: { IpAddr , SocketAddr , ToSocketAddrs } ;
4+ use std:: io ;
75use std:: time:: Duration ;
8- use std:: vec;
9- use std:: { io, thread} ;
106use tokio:: net:: TcpStream ;
117#[ cfg( unix) ]
128use tokio:: net:: UnixStream ;
139use tokio:: timer:: Timeout ;
14- use tokio_executor:: threadpool;
1510
1611pub ( crate ) async fn connect_socket (
1712 host : & Host ,
@@ -22,40 +17,16 @@ pub(crate) async fn connect_socket(
2217) -> Result < Socket , Error > {
2318 match host {
2419 Host :: Tcp ( host) => {
25- let addrs = match host. parse :: < IpAddr > ( ) {
26- Ok ( ip) => {
27- // avoid dealing with blocking DNS entirely if possible
28- vec ! [ SocketAddr :: new( ip, port) ] . into_iter ( )
29- }
30- Err ( _) => dns ( host, port) . await . map_err ( Error :: connect) ?,
31- } ;
32-
33- let mut error = None ;
34- for addr in addrs {
35- let new_error =
36- match connect_with_timeout ( TcpStream :: connect ( & addr) , connect_timeout) . await {
37- Ok ( socket) => {
38- socket. set_nodelay ( true ) . map_err ( Error :: connect) ?;
39- if keepalives {
40- socket
41- . set_keepalive ( Some ( keepalives_idle) )
42- . map_err ( Error :: connect) ?;
43- }
44-
45- return Ok ( Socket :: new_tcp ( socket) ) ;
46- }
47- Err ( e) => e,
48- } ;
49- error = Some ( new_error) ;
20+ let socket =
21+ connect_with_timeout ( TcpStream :: connect ( ( & * * host, port) ) , connect_timeout) . await ?;
22+ socket. set_nodelay ( true ) . map_err ( Error :: connect) ?;
23+ if keepalives {
24+ socket
25+ . set_keepalive ( Some ( keepalives_idle) )
26+ . map_err ( Error :: connect) ?;
5027 }
5128
52- let error = error. unwrap_or_else ( || {
53- Error :: connect ( io:: Error :: new (
54- io:: ErrorKind :: InvalidData ,
55- "resolved 0 addresses" ,
56- ) )
57- } ) ;
58- Err ( error)
29+ Ok ( Socket :: new_tcp ( socket) )
5930 }
6031 #[ cfg( unix) ]
6132 Host :: Unix ( path) => {
@@ -65,25 +36,6 @@ pub(crate) async fn connect_socket(
6536 }
6637}
6738
68- async fn dns ( host : & str , port : u16 ) -> io:: Result < vec:: IntoIter < SocketAddr > > {
69- // if we're running on a threadpool, use its blocking support
70- if let Ok ( r) =
71- future:: poll_fn ( |_| threadpool:: blocking ( || ( host, port) . to_socket_addrs ( ) ) ) . await
72- {
73- return r;
74- }
75-
76- // FIXME what should we do here?
77- let ( tx, rx) = oneshot:: channel ( ) ;
78- let host = host. to_string ( ) ;
79- thread:: spawn ( move || {
80- let addrs = ( & * host, port) . to_socket_addrs ( ) ;
81- let _ = tx. send ( addrs) ;
82- } ) ;
83-
84- rx. await . unwrap ( )
85- }
86-
8739async fn connect_with_timeout < F , T > ( connect : F , timeout : Option < Duration > ) -> Result < T , Error >
8840where
8941 F : Future < Output = io:: Result < T > > ,
0 commit comments