Skip to content

Commit

Permalink
Supports configuration of all TCP Keepalive parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
hansonchar committed Nov 2, 2022
1 parent 04bbd9f commit 7b63fad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/configure_addr_incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ async fn main() {
.tcp_nodelay(true)
.tcp_sleep_on_accept_errors(true)
.tcp_keepalive(Some(Duration::from_secs(32)))
.tcp_keepalive_interval(Some(Duration::from_secs(1)))
.tcp_keepalive_retries(Some(1))
.build();

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
Expand Down
23 changes: 22 additions & 1 deletion src/addr_incoming_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::time::Duration;
pub struct AddrIncomingConfig {
pub(crate) tcp_sleep_on_accept_errors: bool,
pub(crate) tcp_keepalive: Option<Duration>,
pub(crate) tcp_keepalive_interval: Option<Duration>,
pub(crate) tcp_keepalive_retries: Option<u32>,
pub(crate) tcp_nodelay: bool,
}

Expand All @@ -20,6 +22,8 @@ impl AddrIncomingConfig {
Self {
tcp_sleep_on_accept_errors: true,
tcp_keepalive: None,
tcp_keepalive_interval: None,
tcp_keepalive_retries: None,
tcp_nodelay: false,
}
}
Expand All @@ -39,12 +43,29 @@ impl AddrIncomingConfig {

/// Set how often to send TCP keepalive probes.
///
/// Default is `false`.
/// By default TCP keepalive probes is disabled.
pub fn tcp_keepalive(&mut self, val: Option<Duration>) -> &mut Self {
self.tcp_keepalive = val;
self
}

/// Set the duration between two successive TCP keepalive retransmissions,
/// if acknowledgement to the previous keepalive transmission is not received.
///
/// Default is no interval.
pub fn tcp_keepalive_interval(&mut self, val: Option<Duration>) -> &mut Self {
self.tcp_keepalive_interval = val;
self
}

/// Set the number of retransmissions to be carried out before declaring that remote end is not available.
///
/// Default is no retry.
pub fn tcp_keepalive_retries(&mut self, val: Option<u32>) -> &mut Self {
self.tcp_keepalive_retries = val;
self
}

/// Set the value of `TCP_NODELAY` option for accepted connections.
///
/// Default is `false`.
Expand Down
2 changes: 2 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ async fn bind_incoming(

incoming.set_sleep_on_errors(addr_incoming_conf.tcp_sleep_on_accept_errors);
incoming.set_keepalive(addr_incoming_conf.tcp_keepalive);
incoming.set_keepalive_interval(addr_incoming_conf.tcp_keepalive_interval);
incoming.set_keepalive_retries(addr_incoming_conf.tcp_keepalive_retries);
incoming.set_nodelay(addr_incoming_conf.tcp_nodelay);

Ok(incoming)
Expand Down

0 comments on commit 7b63fad

Please sign in to comment.