Skip to content

Commit f51194e

Browse files
committed
Change tcp_keepalive to take a std::Duration
1 parent 01efa4c commit f51194e

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

src/conn/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,7 @@ impl Conn {
957957
#[cfg(not(unix))]
958958
return Err(crate::DriverError::NamedPipesDisabled.into());
959959
} else {
960-
let keepalive = opts
961-
.tcp_keepalive()
962-
.map(|x| std::time::Duration::from_millis(x.into()));
960+
let keepalive = opts.tcp_keepalive();
963961
Stream::connect_tcp(opts.hostport_or_url(), keepalive).await?
964962
};
965963

src/io/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,16 @@ impl stream::Stream for Stream {
472472

473473
#[cfg(test)]
474474
mod test {
475+
use std::time::Duration;
476+
475477
#[cfg(unix)] // no sane way to retrieve current keepalive value on windows
476478
#[tokio::test]
477479
async fn should_connect_with_keepalive() {
478480
use crate::{test_misc::get_opts, Conn};
479481

482+
let duration = Duration::from_millis(42_000);
480483
let opts = get_opts()
481-
.tcp_keepalive(Some(42_000_u32))
484+
.tcp_keepalive(Some(duration))
482485
.prefer_socket(false);
483486
let mut conn: Conn = Conn::new(opts).await.unwrap();
484487
let stream = conn.stream_mut().unwrap();
@@ -497,10 +500,7 @@ mod test {
497500
socket2::Socket::from_raw_fd(raw)
498501
};
499502

500-
assert_eq!(
501-
sock.keepalive_time().unwrap(),
502-
std::time::Duration::from_millis(42_000),
503-
);
503+
assert_eq!(sock.keepalive_time().unwrap(), duration);
504504

505505
std::mem::forget(sock);
506506

src/opts/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,8 @@ pub(crate) struct MysqlOpts {
575575
/// Database name (defaults to `None`).
576576
db_name: Option<String>,
577577

578-
/// TCP keep alive timeout in milliseconds (defaults to `None`).
579-
tcp_keepalive: Option<u32>,
578+
/// TCP keep alive timeout (defaults to `None`).
579+
tcp_keepalive: Option<Duration>,
580580

581581
/// Whether to enable `TCP_NODELAY` (defaults to `true`).
582582
///
@@ -801,10 +801,10 @@ impl Opts {
801801
/// # use mysql_async::*;
802802
/// # fn main() -> Result<()> {
803803
/// let opts = Opts::from_url("mysql://localhost/db?tcp_keepalive=10000")?;
804-
/// assert_eq!(opts.tcp_keepalive(), Some(10_000));
804+
/// assert_eq!(opts.tcp_keepalive(), Some(std::time::Duration::from_secs(10)));
805805
/// # Ok(()) }
806806
/// ```
807-
pub fn tcp_keepalive(&self) -> Option<u32> {
807+
pub fn tcp_keepalive(&self) -> Option<Duration> {
808808
self.inner.mysql_opts.tcp_keepalive
809809
}
810810

@@ -1353,8 +1353,8 @@ impl OptsBuilder {
13531353
}
13541354

13551355
/// Defines `tcp_keepalive` option. See [`Opts::tcp_keepalive`].
1356-
pub fn tcp_keepalive<T: Into<u32>>(mut self, tcp_keepalive: Option<T>) -> Self {
1357-
self.opts.tcp_keepalive = tcp_keepalive.map(Into::into);
1356+
pub fn tcp_keepalive(mut self, tcp_keepalive: Option<Duration>) -> Self {
1357+
self.opts.tcp_keepalive = tcp_keepalive;
13581358
self
13591359
}
13601360

@@ -1773,7 +1773,7 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
17731773
}
17741774
} else if key == "tcp_keepalive" {
17751775
match u32::from_str(&value) {
1776-
Ok(value) => opts.tcp_keepalive = Some(value),
1776+
Ok(value) => opts.tcp_keepalive = Some(Duration::from_millis(value.into())),
17771777
_ => {
17781778
return Err(UrlError::InvalidParamValue {
17791779
param: "tcp_keepalive_ms".into(),

0 commit comments

Comments
 (0)