diff --git a/Cargo.toml b/Cargo.toml index d52526d..98721f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,8 +30,7 @@ tls = [] with-rustls = ["tokio-rustls", "tls", "webpki-roots"] with-native-tls = ["native-tls", "tokio-native-tls", "tls"] - [dev-dependencies] -env_logger = "0.10" +env_logger = "0.11" futures = "^0.3.7" tokio = { version = "1.0", features = ["full"] } diff --git a/examples/monitor.rs b/examples/monitor.rs index 75bd978..73abe42 100644 --- a/examples/monitor.rs +++ b/examples/monitor.rs @@ -11,7 +11,6 @@ use std::env; use futures::{sink::SinkExt, stream::StreamExt}; - use redis_async::{client, resp_array}; #[tokio::main] diff --git a/src/client/builder.rs b/src/client/builder.rs index a07e4e0..637d10f 100644 --- a/src/client/builder.rs +++ b/src/client/builder.rs @@ -8,7 +8,8 @@ * except according to those terms. */ -use std::{sync::Arc, time::Duration}; +use std::sync::Arc; +use std::time::Duration; use crate::error; @@ -25,6 +26,9 @@ pub struct ConnectionBuilder { pub(crate) socket_timeout: Option, } +const DEFAULT_KEEPALIVE: Duration = Duration::from_secs(60); +const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30); + impl ConnectionBuilder { pub fn new(host: impl Into, port: u16) -> Result { Ok(Self { @@ -34,8 +38,8 @@ impl ConnectionBuilder { password: None, #[cfg(feature = "tls")] tls: false, - socket_keepalive: None, - socket_timeout: None, + socket_keepalive: Some(DEFAULT_KEEPALIVE), + socket_timeout: Some(DEFAULT_TIMEOUT), }) } @@ -58,14 +62,14 @@ impl ConnectionBuilder { } /// Set the socket keepalive duration - pub fn socket_keepalive(&mut self, duration: Duration) -> &mut Self { - self.socket_keepalive = Some(duration); + pub fn socket_keepalive(&mut self, duration: Option) -> &mut Self { + self.socket_keepalive = duration; self } /// Set the socket timeout duration - pub fn socket_timeout(&mut self, duration: Duration) -> &mut Self { - self.socket_timeout = Some(duration); + pub fn socket_timeout(&mut self, duration: Option) -> &mut Self { + self.socket_timeout = duration; self } } diff --git a/src/client/connect.rs b/src/client/connect.rs index d8e9ef9..c3f4830 100644 --- a/src/client/connect.rs +++ b/src/client/connect.rs @@ -170,7 +170,12 @@ pub async fn connect_tls( } #[cfg(feature = "with-native-tls")] -pub async fn connect_tls(host: &str, port: u16) -> Result { +pub async fn connect_tls( + host: &str, + port: u16, + socket_keepalive: Option, + socket_timeout: Option, +) -> Result { let cx = native_tls::TlsConnector::builder().build()?; let cx = tokio_native_tls::TlsConnector::from(cx); @@ -182,6 +187,7 @@ pub async fn connect_tls(host: &str, port: u16) -> Result