@@ -40,6 +40,7 @@ use std::collections::HashMap;
40
40
use std:: io:: Cursor ;
41
41
use std:: rc:: Rc ;
42
42
use std:: sync:: Arc ;
43
+ use std:: time:: Duration ;
43
44
44
45
use self :: tcp:: TcpStream ;
45
46
@@ -182,11 +183,12 @@ impl Client {
182
183
/// # Errors
183
184
/// Error is returned if an attempt to connect failed.
184
185
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
186
187
}
187
188
188
189
/// 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.
190
192
///
191
193
/// Takes explicit `config` in comparison to [`Client::connect`]
192
194
/// where default values are used.
@@ -197,8 +199,10 @@ impl Client {
197
199
url : & str ,
198
200
port : u16 ,
199
201
config : protocol:: Config ,
202
+ timeout : Option < Duration > ,
200
203
) -> 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)
202
206
. map_err ( |e| ClientError :: ConnectionClosed ( Arc :: new ( e. into ( ) ) ) ) ?;
203
207
let client = ClientInner :: new ( config, stream. clone ( ) ) ;
204
208
let client = Rc :: new ( NoYieldsRefCell :: new ( client) ) ;
@@ -477,12 +481,26 @@ mod tests {
477
481
creds : Some ( ( "test_user" . into ( ) , "password" . into ( ) ) ) ,
478
482
..Default :: default ( )
479
483
} ,
484
+ None ,
480
485
)
481
486
. timeout ( Duration :: from_secs ( 3 ) )
482
487
. await
483
488
. unwrap ( )
484
489
}
485
490
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
+
486
504
#[ crate :: test( tarantool = "crate" ) ]
487
505
async fn connect ( ) {
488
506
let _client = Client :: connect ( "localhost" , listen_port ( ) ) . await . unwrap ( ) ;
@@ -742,6 +760,7 @@ mod tests {
742
760
auth_method : AuthMethod :: Md5 ,
743
761
..Default :: default ( )
744
762
} ,
763
+ None ,
745
764
)
746
765
. timeout ( Duration :: from_secs ( 3 ) )
747
766
. await
@@ -765,6 +784,7 @@ mod tests {
765
784
auth_method : AuthMethod :: Md5 ,
766
785
..Default :: default ( )
767
786
} ,
787
+ None ,
768
788
)
769
789
. timeout ( Duration :: from_secs ( 3 ) )
770
790
. await
@@ -787,6 +807,7 @@ mod tests {
787
807
auth_method : AuthMethod :: ChapSha1 ,
788
808
..Default :: default ( )
789
809
} ,
810
+ None ,
790
811
)
791
812
. timeout ( Duration :: from_secs ( 3 ) )
792
813
. await
@@ -837,6 +858,7 @@ mod tests {
837
858
auth_method : AuthMethod :: Ldap ,
838
859
..Default :: default ( )
839
860
} ,
861
+ None ,
840
862
)
841
863
. timeout ( Duration :: from_secs ( 3 ) )
842
864
. await
@@ -860,6 +882,7 @@ mod tests {
860
882
auth_method : AuthMethod :: Ldap ,
861
883
..Default :: default ( )
862
884
} ,
885
+ None ,
863
886
)
864
887
. timeout ( Duration :: from_secs ( 3 ) )
865
888
. await
@@ -882,6 +905,7 @@ mod tests {
882
905
auth_method : AuthMethod :: ChapSha1 ,
883
906
..Default :: default ( )
884
907
} ,
908
+ None ,
885
909
)
886
910
. timeout ( Duration :: from_secs ( 3 ) )
887
911
. await
0 commit comments