diff --git a/src/conn/mod.rs b/src/conn/mod.rs index 9188941..49b3918 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -957,9 +957,7 @@ impl Conn { #[cfg(not(unix))] return Err(crate::DriverError::NamedPipesDisabled.into()); } else { - let keepalive = opts - .tcp_keepalive() - .map(|x| std::time::Duration::from_millis(x.into())); + let keepalive = opts.tcp_keepalive(); Stream::connect_tcp(opts.hostport_or_url(), keepalive).await? }; diff --git a/src/io/mod.rs b/src/io/mod.rs index e5705a9..c9ce68b 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -472,13 +472,16 @@ impl stream::Stream for Stream { #[cfg(test)] mod test { + use std::time::Duration; + #[cfg(unix)] // no sane way to retrieve current keepalive value on windows #[tokio::test] async fn should_connect_with_keepalive() { use crate::{test_misc::get_opts, Conn}; + let duration = Duration::from_millis(42_000); let opts = get_opts() - .tcp_keepalive(Some(42_000_u32)) + .tcp_keepalive(Some(duration)) .prefer_socket(false); let mut conn: Conn = Conn::new(opts).await.unwrap(); let stream = conn.stream_mut().unwrap(); @@ -497,10 +500,7 @@ mod test { socket2::Socket::from_raw_fd(raw) }; - assert_eq!( - sock.keepalive_time().unwrap(), - std::time::Duration::from_millis(42_000), - ); + assert_eq!(sock.keepalive_time().unwrap(), duration); std::mem::forget(sock); diff --git a/src/opts/mod.rs b/src/opts/mod.rs index 1923756..536ee6d 100644 --- a/src/opts/mod.rs +++ b/src/opts/mod.rs @@ -575,8 +575,8 @@ pub(crate) struct MysqlOpts { /// Database name (defaults to `None`). db_name: Option, - /// TCP keep alive timeout in milliseconds (defaults to `None`). - tcp_keepalive: Option, + /// TCP keep alive timeout (defaults to `None`). + tcp_keepalive: Option, /// Whether to enable `TCP_NODELAY` (defaults to `true`). /// @@ -801,10 +801,10 @@ impl Opts { /// # use mysql_async::*; /// # fn main() -> Result<()> { /// let opts = Opts::from_url("mysql://localhost/db?tcp_keepalive=10000")?; - /// assert_eq!(opts.tcp_keepalive(), Some(10_000)); + /// assert_eq!(opts.tcp_keepalive(), Some(std::time::Duration::from_secs(10))); /// # Ok(()) } /// ``` - pub fn tcp_keepalive(&self) -> Option { + pub fn tcp_keepalive(&self) -> Option { self.inner.mysql_opts.tcp_keepalive } @@ -1353,8 +1353,8 @@ impl OptsBuilder { } /// Defines `tcp_keepalive` option. See [`Opts::tcp_keepalive`]. - pub fn tcp_keepalive>(mut self, tcp_keepalive: Option) -> Self { - self.opts.tcp_keepalive = tcp_keepalive.map(Into::into); + pub fn tcp_keepalive(mut self, tcp_keepalive: Option) -> Self { + self.opts.tcp_keepalive = tcp_keepalive; self } @@ -1773,7 +1773,7 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result { } } else if key == "tcp_keepalive" { match u32::from_str(&value) { - Ok(value) => opts.tcp_keepalive = Some(value), + Ok(value) => opts.tcp_keepalive = Some(Duration::from_millis(value.into())), _ => { return Err(UrlError::InvalidParamValue { param: "tcp_keepalive_ms".into(),