Skip to content

Commit 407270b

Browse files
kusanchoAlexander Kurdakov
authored andcommitted
feat: add timeout argument to connect_with_config function
1 parent 67c906f commit 407270b

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- `network::protocol::codec::{LegacyCall, Nop, Prepare, Begin, Commit, Rollback}` variants
1313
- `network::protocol::codec::Header::encode_from_parts` function
1414
- `network::protocol::codec::iproto_key::SQL_INFO` constant
15+
- Added optional argument timeout to `network::client::Client::connect_with_config`
1516

1617
### Changed
1718

tarantool/src/network/client/mod.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use std::collections::HashMap;
4040
use std::io::Cursor;
4141
use std::rc::Rc;
4242
use std::sync::Arc;
43+
use std::time::Duration;
4344

4445
use 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

tarantool/src/network/client/reconnect.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ impl Client {
5252
self.reconnect_count.fetch_add(1, Ordering::Relaxed);
5353
}
5454

55-
let res =
56-
super::Client::connect_with_config(&self.url, self.port, self.protocol_config.clone())
57-
.await;
55+
let res = super::Client::connect_with_config(
56+
&self.url,
57+
self.port,
58+
self.protocol_config.clone(),
59+
None,
60+
)
61+
.await;
5862
match res {
5963
Ok(new_client) => {
6064
*client = Some(Ok(new_client.clone()));

0 commit comments

Comments
 (0)