Skip to content

Commit

Permalink
Default keepalive and timeout options
Browse files Browse the repository at this point in the history
  • Loading branch information
benashford committed Feb 12, 2024
1 parent c841a5a commit deacfcd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
1 change: 0 additions & 1 deletion examples/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use std::env;

use futures::{sink::SinkExt, stream::StreamExt};

use redis_async::{client, resp_array};

#[tokio::main]
Expand Down
18 changes: 11 additions & 7 deletions src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,6 +26,9 @@ pub struct ConnectionBuilder {
pub(crate) socket_timeout: Option<Duration>,
}

const DEFAULT_KEEPALIVE: Duration = Duration::from_secs(60);
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

impl ConnectionBuilder {
pub fn new(host: impl Into<String>, port: u16) -> Result<Self, error::Error> {
Ok(Self {
Expand All @@ -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),
})
}

Expand All @@ -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<Duration>) -> &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<Duration>) -> &mut Self {
self.socket_timeout = duration;
self
}
}
8 changes: 7 additions & 1 deletion src/client/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ pub async fn connect_tls(
}

#[cfg(feature = "with-native-tls")]
pub async fn connect_tls(host: &str, port: u16) -> Result<RespConnection, error::Error> {
pub async fn connect_tls(
host: &str,
port: u16,
socket_keepalive: Option<Duration>,
socket_timeout: Option<Duration>,
) -> Result<RespConnection, error::Error> {
let cx = native_tls::TlsConnector::builder().build()?;
let cx = tokio_native_tls::TlsConnector::from(cx);

Expand All @@ -182,6 +187,7 @@ pub async fn connect_tls(host: &str, port: u16) -> Result<RespConnection, error:
error::ConnectionReason::ConnectionFailed,
))?;
let tcp_stream = TcpStream::connect(addr).await?;
apply_keepalive_and_timeouts(&tcp_stream, socket_keepalive, socket_timeout)?;
let stream = cx.connect(host, tcp_stream).await?;

Ok(RespCodec.framed(RespConnectionInner::Tls { stream }))
Expand Down

0 comments on commit deacfcd

Please sign in to comment.