@@ -40,6 +40,7 @@ use std::collections::HashMap;
4040use std:: io:: Cursor ;
4141use std:: rc:: Rc ;
4242use std:: sync:: Arc ;
43+ use std:: time:: Duration ;
4344
4445use self :: tcp:: TcpStream ;
4546
@@ -182,11 +183,12 @@ impl Client {
182183 /// # Errors
183184 /// Error is returned if an attempt to connect failed.
184185 pub async fn connect ( url : & str , port : u16 ) -> Result < Self , ClientError > {
185- Self :: connect_with_config ( url, port, Default :: default ( ) ) . await
186+ Self :: connect_with_config ( url, port, Default :: default ( ) , None ) . await
186187 }
187188
188189 /// Creates a new client and tries to establish connection
189- /// to `url:port`
190+ /// to `url:port` with given timeout. When timeout is None
191+ /// function behaves as a synchronous.
190192 ///
191193 /// Takes explicit `config` in comparison to [`Client::connect`]
192194 /// where default values are used.
@@ -197,8 +199,10 @@ impl Client {
197199 url : & str ,
198200 port : u16 ,
199201 config : protocol:: Config ,
202+ timeout : Option < Duration > ,
200203 ) -> Result < Self , ClientError > {
201- let stream = TcpStream :: connect ( url, port)
204+ let timeout = timeout. unwrap_or ( Duration :: MAX ) ;
205+ let stream = TcpStream :: connect_timeout ( url, port, timeout)
202206 . map_err ( |e| ClientError :: ConnectionClosed ( Arc :: new ( e. into ( ) ) ) ) ?;
203207 let client = ClientInner :: new ( config, stream. clone ( ) ) ;
204208 let client = Rc :: new ( NoYieldsRefCell :: new ( client) ) ;
@@ -477,12 +481,26 @@ mod tests {
477481 creds : Some ( ( "test_user" . into ( ) , "password" . into ( ) ) ) ,
478482 ..Default :: default ( )
479483 } ,
484+ None ,
480485 )
481486 . timeout ( Duration :: from_secs ( 3 ) )
482487 . await
483488 . unwrap ( )
484489 }
485490
491+ #[ crate :: test( tarantool = "crate" ) ]
492+ async fn connect_with_timeout ( ) {
493+ // Without timeout, the connection hangs on address like this
494+ Client :: connect_with_config (
495+ "123123" ,
496+ listen_port ( ) ,
497+ protocol:: Config :: default ( ) ,
498+ Some ( Duration :: from_secs ( 1 ) ) ,
499+ )
500+ . await
501+ . unwrap_err ( ) ;
502+ }
503+
486504 #[ crate :: test( tarantool = "crate" ) ]
487505 async fn connect ( ) {
488506 let _client = Client :: connect ( "localhost" , listen_port ( ) ) . await . unwrap ( ) ;
@@ -742,6 +760,7 @@ mod tests {
742760 auth_method : AuthMethod :: Md5 ,
743761 ..Default :: default ( )
744762 } ,
763+ None ,
745764 )
746765 . timeout ( Duration :: from_secs ( 3 ) )
747766 . await
@@ -765,6 +784,7 @@ mod tests {
765784 auth_method : AuthMethod :: Md5 ,
766785 ..Default :: default ( )
767786 } ,
787+ None ,
768788 )
769789 . timeout ( Duration :: from_secs ( 3 ) )
770790 . await
@@ -787,6 +807,7 @@ mod tests {
787807 auth_method : AuthMethod :: ChapSha1 ,
788808 ..Default :: default ( )
789809 } ,
810+ None ,
790811 )
791812 . timeout ( Duration :: from_secs ( 3 ) )
792813 . await
@@ -837,6 +858,7 @@ mod tests {
837858 auth_method : AuthMethod :: Ldap ,
838859 ..Default :: default ( )
839860 } ,
861+ None ,
840862 )
841863 . timeout ( Duration :: from_secs ( 3 ) )
842864 . await
@@ -860,6 +882,7 @@ mod tests {
860882 auth_method : AuthMethod :: Ldap ,
861883 ..Default :: default ( )
862884 } ,
885+ None ,
863886 )
864887 . timeout ( Duration :: from_secs ( 3 ) )
865888 . await
@@ -882,6 +905,7 @@ mod tests {
882905 auth_method : AuthMethod :: ChapSha1 ,
883906 ..Default :: default ( )
884907 } ,
908+ None ,
885909 )
886910 . timeout ( Duration :: from_secs ( 3 ) )
887911 . await
0 commit comments