Skip to content

Commit c85b2c1

Browse files
authored
Merge pull request #1007 from pH14/add-tcp-user-timeout
wire through knob for TCP user timeout
2 parents 7061671 + 62a4432 commit c85b2c1

File tree

6 files changed

+46
-2
lines changed

6 files changed

+46
-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.tcp_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 tcp_user_timeout: Option<Duration>,
159160
pub keepalive: Option<KeepaliveConfig>,
160161
}
161162

tokio-postgres/src/config.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ pub enum Host {
9696
/// omitted or the empty string.
9797
/// * `connect_timeout` - The time limit in seconds applied to each socket-level connection attempt. Note that hostnames
9898
/// can resolve to multiple IP addresses, and this limit is applied to each address. Defaults to no timeout.
99+
/// * `tcp_user_timeout` - The time limit that transmitted data may remain unacknowledged before a connection is forcibly closed.
100+
/// This is ignored for Unix domain socket connections. It is only supported on systems where TCP_USER_TIMEOUT is available
101+
/// and will default to the system default if omitted or set to 0; on other systems, it has no effect.
99102
/// * `keepalives` - Controls the use of TCP keepalive. A value of 0 disables keepalive and nonzero integers enable it.
100103
/// This option is ignored when connecting with Unix sockets. Defaults to on.
101104
/// * `keepalives_idle` - The number of seconds of inactivity after which a keepalive message is sent to the server.
@@ -160,6 +163,7 @@ pub struct Config {
160163
pub(crate) host: Vec<Host>,
161164
pub(crate) port: Vec<u16>,
162165
pub(crate) connect_timeout: Option<Duration>,
166+
pub(crate) tcp_user_timeout: Option<Duration>,
163167
pub(crate) keepalives: bool,
164168
pub(crate) keepalive_config: KeepaliveConfig,
165169
pub(crate) target_session_attrs: TargetSessionAttrs,
@@ -190,6 +194,7 @@ impl Config {
190194
host: vec![],
191195
port: vec![],
192196
connect_timeout: None,
197+
tcp_user_timeout: None,
193198
keepalives: true,
194199
keepalive_config,
195200
target_session_attrs: TargetSessionAttrs::Any,
@@ -340,6 +345,22 @@ impl Config {
340345
self.connect_timeout.as_ref()
341346
}
342347

348+
/// Sets the TCP user timeout.
349+
///
350+
/// This is ignored for Unix domain socket connections. It is only supported on systems where
351+
/// TCP_USER_TIMEOUT is available and will default to the system default if omitted or set to 0;
352+
/// on other systems, it has no effect.
353+
pub fn tcp_user_timeout(&mut self, tcp_user_timeout: Duration) -> &mut Config {
354+
self.tcp_user_timeout = Some(tcp_user_timeout);
355+
self
356+
}
357+
358+
/// Gets the TCP user timeout, if one has been set with the
359+
/// `user_timeout` method.
360+
pub fn get_tcp_user_timeout(&self) -> Option<&Duration> {
361+
self.tcp_user_timeout.as_ref()
362+
}
363+
343364
/// Controls the use of TCP keepalive.
344365
///
345366
/// This is ignored for Unix domain socket connections. Defaults to `true`.
@@ -474,6 +495,14 @@ impl Config {
474495
self.connect_timeout(Duration::from_secs(timeout as u64));
475496
}
476497
}
498+
"tcp_user_timeout" => {
499+
let timeout = value
500+
.parse::<i64>()
501+
.map_err(|_| Error::config_parse(Box::new(InvalidValue("tcp_user_timeout"))))?;
502+
if timeout > 0 {
503+
self.tcp_user_timeout(Duration::from_secs(timeout as u64));
504+
}
505+
}
477506
"keepalives" => {
478507
let keepalives = value
479508
.parse::<u64>()
@@ -595,6 +624,7 @@ impl fmt::Debug for Config {
595624
.field("host", &self.host)
596625
.field("port", &self.port)
597626
.field("connect_timeout", &self.connect_timeout)
627+
.field("tcp_user_timeout", &self.tcp_user_timeout)
598628
.field("keepalives", &self.keepalives)
599629
.field("keepalives_idle", &self.keepalive_config.idle)
600630
.field("keepalives_interval", &self.keepalive_config.interval)

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.tcp_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+
tcp_user_timeout: config.tcp_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+
tcp_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(tcp_user_timeout)
45+
.map_err(Error::connect)?;
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)