add in to param parsing; update doc

This commit is contained in:
Paul Hemberger 2023-03-24 13:11:18 -04:00
parent b4c05f4f81
commit ee5a139028
5 changed files with 28 additions and 12 deletions

View File

@ -38,7 +38,7 @@ where
&config.host, &config.host,
config.port, config.port,
config.connect_timeout, config.connect_timeout,
config.user_timeout, config.tcp_user_timeout,
config.keepalive.as_ref(), config.keepalive.as_ref(),
) )
.await?; .await?;

View File

@ -156,7 +156,7 @@ pub(crate) struct SocketConfig {
pub host: Host, pub host: Host,
pub port: u16, pub port: u16,
pub connect_timeout: Option<Duration>, pub connect_timeout: Option<Duration>,
pub user_timeout: Option<Duration>, pub tcp_user_timeout: Option<Duration>,
pub keepalive: Option<KeepaliveConfig>, pub keepalive: Option<KeepaliveConfig>,
} }

View File

@ -96,6 +96,9 @@ pub enum Host {
/// omitted or the empty string. /// omitted or the empty string.
/// * `connect_timeout` - The time limit in seconds applied to each socket-level connection attempt. Note that hostnames /// * `connect_timeout` - The time limit in seconds applied to each socket-level connection attempt. Note that hostnames
/// can resolve to multiple IP addresses, and this limit is applied to each address. Defaults to no timeout. /// can resolve to multiple IP addresses, and this limit is applied to each address. Defaults to no timeout.
/// * `tcp_user_timeout` - The time limit that transmitted data may remain unacknowledged before a connection is forcibly closed.
/// This is ignored for Unix domain socket connections. It is only supported on systems where TCP_USER_TIMEOUT is available
/// and will default to the system default; on other systems, it has no effect.
/// * `keepalives` - Controls the use of TCP keepalive. A value of 0 disables keepalive and nonzero integers enable it. /// * `keepalives` - Controls the use of TCP keepalive. A value of 0 disables keepalive and nonzero integers enable it.
/// This option is ignored when connecting with Unix sockets. Defaults to on. /// This option is ignored when connecting with Unix sockets. Defaults to on.
/// * `keepalives_idle` - The number of seconds of inactivity after which a keepalive message is sent to the server. /// * `keepalives_idle` - The number of seconds of inactivity after which a keepalive message is sent to the server.
@ -160,7 +163,7 @@ pub struct Config {
pub(crate) host: Vec<Host>, pub(crate) host: Vec<Host>,
pub(crate) port: Vec<u16>, pub(crate) port: Vec<u16>,
pub(crate) connect_timeout: Option<Duration>, pub(crate) connect_timeout: Option<Duration>,
pub(crate) user_timeout: Option<Duration>, pub(crate) tcp_user_timeout: Option<Duration>,
pub(crate) keepalives: bool, pub(crate) keepalives: bool,
pub(crate) keepalive_config: KeepaliveConfig, pub(crate) keepalive_config: KeepaliveConfig,
pub(crate) target_session_attrs: TargetSessionAttrs, pub(crate) target_session_attrs: TargetSessionAttrs,
@ -191,7 +194,7 @@ impl Config {
host: vec![], host: vec![],
port: vec![], port: vec![],
connect_timeout: None, connect_timeout: None,
user_timeout: None, tcp_user_timeout: None,
keepalives: true, keepalives: true,
keepalive_config, keepalive_config,
target_session_attrs: TargetSessionAttrs::Any, target_session_attrs: TargetSessionAttrs::Any,
@ -343,15 +346,19 @@ impl Config {
} }
/// Sets the TCP user timeout. /// Sets the TCP user timeout.
pub fn user_timeout(&mut self, user_timeout: Duration) -> &mut Config { ///
self.user_timeout = Some(user_timeout); /// This is ignored for Unix domain socket connections. It is only supported on systems where
/// TCP_USER_TIMEOUT is available and will default to the system default; on other systems,
/// it has no effect.
pub fn tcp_user_timeout(&mut self, tcp_user_timeout: Duration) -> &mut Config {
self.tcp_user_timeout = Some(tcp_user_timeout);
self self
} }
/// Gets the TCP user timeout, if one has been set with the /// Gets the TCP user timeout, if one has been set with the
/// `user_timeout` method. /// `user_timeout` method.
pub fn get_user_timeout(&self) -> Option<&Duration> { pub fn get_tcp_user_timeout(&self) -> Option<&Duration> {
self.user_timeout.as_ref() self.tcp_user_timeout.as_ref()
} }
/// Controls the use of TCP keepalive. /// Controls the use of TCP keepalive.
@ -488,6 +495,14 @@ impl Config {
self.connect_timeout(Duration::from_secs(timeout as u64)); self.connect_timeout(Duration::from_secs(timeout as u64));
} }
} }
"tcp_user_timeout" => {
let timeout = value
.parse::<i64>()
.map_err(|_| Error::config_parse(Box::new(InvalidValue("tcp_user_timeout"))))?;
if timeout > 0 {
self.tcp_user_timeout(Duration::from_secs(timeout as u64));
}
}
"keepalives" => { "keepalives" => {
let keepalives = value let keepalives = value
.parse::<u64>() .parse::<u64>()
@ -609,6 +624,7 @@ impl fmt::Debug for Config {
.field("host", &self.host) .field("host", &self.host)
.field("port", &self.port) .field("port", &self.port)
.field("connect_timeout", &self.connect_timeout) .field("connect_timeout", &self.connect_timeout)
.field("tcp_user_timeout", &self.tcp_user_timeout)
.field("keepalives", &self.keepalives) .field("keepalives", &self.keepalives)
.field("keepalives_idle", &self.keepalive_config.idle) .field("keepalives_idle", &self.keepalive_config.idle)
.field("keepalives_interval", &self.keepalive_config.interval) .field("keepalives_interval", &self.keepalive_config.interval)

View File

@ -65,7 +65,7 @@ where
host, host,
port, port,
config.connect_timeout, config.connect_timeout,
config.user_timeout, config.tcp_user_timeout,
if config.keepalives { if config.keepalives {
Some(&config.keepalive_config) Some(&config.keepalive_config)
} else { } else {
@ -119,7 +119,7 @@ where
host: host.clone(), host: host.clone(),
port, port,
connect_timeout: config.connect_timeout, connect_timeout: config.connect_timeout,
user_timeout: config.user_timeout, tcp_user_timeout: config.tcp_user_timeout,
keepalive: if config.keepalives { keepalive: if config.keepalives {
Some(config.keepalive_config.clone()) Some(config.keepalive_config.clone())
} else { } else {

View File

@ -14,7 +14,7 @@ pub(crate) async fn connect_socket(
host: &Host, host: &Host,
port: u16, port: u16,
connect_timeout: Option<Duration>, connect_timeout: Option<Duration>,
user_timeout: Option<Duration>, tcp_user_timeout: Option<Duration>,
keepalive_config: Option<&KeepaliveConfig>, keepalive_config: Option<&KeepaliveConfig>,
) -> Result<Socket, Error> { ) -> Result<Socket, Error> {
match host { match host {
@ -41,7 +41,7 @@ pub(crate) async fn connect_socket(
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
sock_ref sock_ref
.set_tcp_user_timeout(user_timeout) .set_tcp_user_timeout(tcp_user_timeout)
.map_err(Error::timeout)?; .map_err(Error::timeout)?;
} }