Skip to content

Commit 18ee7eb

Browse files
committed
add tcp user timeout config
1 parent 9c90862 commit 18ee7eb

File tree

7 files changed

+37
-2
lines changed

7 files changed

+37
-2
lines changed

tokio-postgres/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ phf = "0.11"
5656
postgres-protocol = { version = "0.6.4", path = "../postgres-protocol" }
5757
postgres-types = { version = "0.2.4", path = "../postgres-types" }
5858
serde = { version = "1.0", optional = true }
59-
socket2 = "0.4"
59+
socket2 = { version = "0.4.7", features = ["all"] }
6060
tokio = { version = "1.0", features = ["io-util"] }
6161
tokio-util = { version = "0.7", features = ["codec"] }
6262

tokio-postgres/src/cancel_query.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ where
3838
&config.host,
3939
config.port,
4040
config.connect_timeout,
41+
config.user_timeout,
4142
config.keepalive.as_ref(),
4243
)
4344
.await?;

tokio-postgres/src/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ pub(crate) struct SocketConfig {
158158
pub host: Host,
159159
pub port: u16,
160160
pub connect_timeout: Option<Duration>,
161+
pub user_timeout: Option<Duration>,
161162
pub keepalive: Option<KeepaliveConfig>,
162163
}
163164

tokio-postgres/src/config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ pub struct Config {
183183
pub(crate) host: Vec<Host>,
184184
pub(crate) port: Vec<u16>,
185185
pub(crate) connect_timeout: Option<Duration>,
186+
pub(crate) user_timeout: Option<Duration>,
186187
pub(crate) keepalives: bool,
187188
pub(crate) keepalive_config: KeepaliveConfig,
188189
pub(crate) target_session_attrs: TargetSessionAttrs,
@@ -217,6 +218,7 @@ impl Config {
217218
host: vec![],
218219
port: vec![],
219220
connect_timeout: None,
221+
user_timeout: None,
220222
keepalives: true,
221223
keepalive_config,
222224
target_session_attrs: TargetSessionAttrs::Any,
@@ -407,6 +409,18 @@ impl Config {
407409
self.connect_timeout.as_ref()
408410
}
409411

412+
/// Sets the TCP user timeout.
413+
pub fn user_timeout(&mut self, user_timeout: Duration) -> &mut Config {
414+
self.user_timeout = Some(user_timeout);
415+
self
416+
}
417+
418+
/// Gets the TCP user timeout, if one has been set with the
419+
/// `user_timeout` method.
420+
pub fn get_user_timeout(&self) -> Option<&Duration> {
421+
self.user_timeout.as_ref()
422+
}
423+
410424
/// Controls the use of TCP keepalive.
411425
///
412426
/// This is ignored for Unix domain socket connections. Defaults to `true`.

tokio-postgres/src/connect.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ where
6565
host,
6666
port,
6767
config.connect_timeout,
68+
config.user_timeout,
6869
if config.keepalives {
6970
Some(&config.keepalive_config)
7071
} else {
@@ -118,6 +119,7 @@ where
118119
host: host.clone(),
119120
port,
120121
connect_timeout: config.connect_timeout,
122+
user_timeout: config.user_timeout,
121123
keepalive: if config.keepalives {
122124
Some(config.keepalive_config.clone())
123125
} else {

tokio-postgres/src/connect_socket.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ use tokio::net::UnixStream;
1010
use tokio::net::{self, TcpStream};
1111
use tokio::time;
1212

13+
#[allow(unused_variables)]
1314
pub(crate) async fn connect_socket(
1415
host: &Host,
1516
port: u16,
1617
connect_timeout: Option<Duration>,
18+
user_timeout: Option<Duration>,
1719
keepalive_config: Option<&KeepaliveConfig>,
1820
) -> Result<Socket, Error> {
1921
match host {
@@ -35,8 +37,18 @@ pub(crate) async fn connect_socket(
3537
};
3638

3739
stream.set_nodelay(true).map_err(Error::connect)?;
40+
41+
let sock_ref = SockRef::from(&stream);
42+
43+
#[cfg(target_os = "linux")]
44+
{
45+
sock_ref
46+
.set_tcp_user_timeout(user_timeout)
47+
.map_err(Error::timeout)?;
48+
}
49+
3850
if let Some(keepalive_config) = keepalive_config {
39-
SockRef::from(&stream)
51+
sock_ref
4052
.set_tcp_keepalive(&TcpKeepalive::from(keepalive_config))
4153
.map_err(Error::connect)?;
4254
}

tokio-postgres/src/error/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ impl Error {
503503
Error::new(Kind::Connect, Some(Box::new(e)))
504504
}
505505

506+
#[allow(dead_code)]
507+
pub(crate) fn timeout(e: io::Error) -> Error {
508+
Error::new(Kind::Timeout, Some(Box::new(e)))
509+
}
510+
506511
#[doc(hidden)]
507512
pub fn __private_api_timeout() -> Error {
508513
Error::new(Kind::Timeout, None)

0 commit comments

Comments
 (0)