Skip to content

Commit

Permalink
Set keep-alive on TCP sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastrac committed Jun 25, 2024
1 parent 0937298 commit 1211363
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions edgedb-tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ once_cell = "1.9.0"
tokio-stream = { version = "0.1.11", optional = true }
base64 = "0.22.1"
crc16 = "0.4.0"
socket2 = "0.5"

[target.'cfg(target_family="unix")'.dev-dependencies]
command-fds = "0.3.0"
Expand Down
17 changes: 17 additions & 0 deletions edgedb-tokio/src/raw/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use bytes::{Bytes, BytesMut};
use rand::{thread_rng, Rng};
use rustls::pki_types::DnsName;
use scram::ScramClient;
use socket2::TcpKeepalive;
use tls_api::TlsConnectorBuilder;
use tls_api::{TlsConnector, TlsConnectorBox, TlsStream, TlsStreamDyn};
use tls_api_not_tls::TlsConnector as PlainConnector;
Expand Down Expand Up @@ -335,6 +336,22 @@ async fn connect3(cfg: &Config, tls: &TlsConnectorBox) -> Result<TlsStream, Erro
let conn = TcpStream::connect(addr)
.await
.map_err(ClientConnectionError::with_source)?;

// Set keep-alive on the socket, but don't fail if this isn't successful
let sock = socket2::SockRef::from(&conn);
#[cfg(target_os = "openbsd")]
if let Err(e) = sock.set_keepalive(true) {
log::warn!("Failed to set keepalive: {e:?}");
}
#[cfg(not(target_os = "openbsd"))]
if let Err(e) = sock.set_tcp_keepalive(
&TcpKeepalive::new()
.with_interval(Duration::from_secs(60))
.with_time(Duration::from_secs(60)),
) {
log::warn!("Failed to set keepalive: {e:?}");
}

let host = match &cfg.0.tls_server_name {
Some(server_name) => Cow::from(server_name),
None => {
Expand Down

0 comments on commit 1211363

Please sign in to comment.