Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
};

Expand Down
10 changes: 5 additions & 5 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions src/opts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ pub(crate) struct MysqlOpts {
/// Database name (defaults to `None`).
db_name: Option<String>,

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

/// Whether to enable `TCP_NODELAY` (defaults to `true`).
///
Expand Down Expand Up @@ -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<u32> {
pub fn tcp_keepalive(&self) -> Option<Duration> {
self.inner.mysql_opts.tcp_keepalive
}

Expand Down Expand Up @@ -1353,8 +1353,8 @@ impl OptsBuilder {
}

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

Expand Down Expand Up @@ -1773,7 +1773,7 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
}
} 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(),
Expand Down
Loading