Skip to content

Commit b4c05f4

Browse files
committed
wire through knob for TCP user timeout
1 parent 7061671 commit b4c05f4

File tree

6 files changed

+30
-2
lines changed

6 files changed

+30
-2
lines changed

tokio-postgres/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pin-project-lite = "0.2"
5555
phf = "0.11"
5656
postgres-protocol = { version = "0.6.4", path = "../postgres-protocol" }
5757
postgres-types = { version = "0.2.4", path = "../postgres-types" }
58-
socket2 = "0.4"
58+
socket2 = { version = "0.4", features = ["all"] }
5959
tokio = { version = "1.0", features = ["io-util"] }
6060
tokio-util = { version = "0.7", features = ["codec"] }
6161

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
@@ -156,6 +156,7 @@ pub(crate) struct SocketConfig {
156156
pub host: Host,
157157
pub port: u16,
158158
pub connect_timeout: Option<Duration>,
159+
pub user_timeout: Option<Duration>,
159160
pub keepalive: Option<KeepaliveConfig>,
160161
}
161162

tokio-postgres/src/config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ pub struct Config {
160160
pub(crate) host: Vec<Host>,
161161
pub(crate) port: Vec<u16>,
162162
pub(crate) connect_timeout: Option<Duration>,
163+
pub(crate) user_timeout: Option<Duration>,
163164
pub(crate) keepalives: bool,
164165
pub(crate) keepalive_config: KeepaliveConfig,
165166
pub(crate) target_session_attrs: TargetSessionAttrs,
@@ -190,6 +191,7 @@ impl Config {
190191
host: vec![],
191192
port: vec![],
192193
connect_timeout: None,
194+
user_timeout: None,
193195
keepalives: true,
194196
keepalive_config,
195197
target_session_attrs: TargetSessionAttrs::Any,
@@ -340,6 +342,18 @@ impl Config {
340342
self.connect_timeout.as_ref()
341343
}
342344

345+
/// Sets the TCP user timeout.
346+
pub fn user_timeout(&mut self, user_timeout: Duration) -> &mut Config {
347+
self.user_timeout = Some(user_timeout);
348+
self
349+
}
350+
351+
/// Gets the TCP user timeout, if one has been set with the
352+
/// `user_timeout` method.
353+
pub fn get_user_timeout(&self) -> Option<&Duration> {
354+
self.user_timeout.as_ref()
355+
}
356+
343357
/// Controls the use of TCP keepalive.
344358
///
345359
/// 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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub(crate) async fn connect_socket(
1414
host: &Host,
1515
port: u16,
1616
connect_timeout: Option<Duration>,
17+
user_timeout: Option<Duration>,
1718
keepalive_config: Option<&KeepaliveConfig>,
1819
) -> Result<Socket, Error> {
1920
match host {
@@ -35,8 +36,17 @@ pub(crate) async fn connect_socket(
3536
};
3637

3738
stream.set_nodelay(true).map_err(Error::connect)?;
39+
40+
let sock_ref = SockRef::from(&stream);
41+
#[cfg(target_os = "linux")]
42+
{
43+
sock_ref
44+
.set_tcp_user_timeout(user_timeout)
45+
.map_err(Error::timeout)?;
46+
}
47+
3848
if let Some(keepalive_config) = keepalive_config {
39-
SockRef::from(&stream)
49+
sock_ref
4050
.set_tcp_keepalive(&TcpKeepalive::from(keepalive_config))
4151
.map_err(Error::connect)?;
4252
}

0 commit comments

Comments
 (0)