-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(server): make idle time of a http connection configurable (#4248)
With the idle time configurable we can prevent a pile up of open connections which never see any activity. See also on the hyper issue tracker: ``` hyperium/hyper#3743 hyperium/hyper#1628 hyperium/hyper#2355 hyperium/hyper#2827 ```
- Loading branch information
Showing
6 changed files
with
391 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::io; | ||
use std::time::Duration; | ||
|
||
use axum_server::accept::Accept; | ||
use socket2::TcpKeepalive; | ||
use tokio::net::TcpStream; | ||
|
||
use crate::services::server::io::IdleTimeout; | ||
use crate::statsd::RelayCounters; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct RelayAcceptor { | ||
tcp_keepalive: Option<TcpKeepalive>, | ||
idle_timeout: Option<Duration>, | ||
} | ||
|
||
impl RelayAcceptor { | ||
/// Creates a new [`RelayAcceptor`] which only configures `TCP_NODELAY`. | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Configures the acceptor to enable TCP keep-alive. | ||
/// | ||
/// The `timeout` is used to configure the keep-alive time as well as interval. | ||
/// A zero duration timeout disables TCP keep-alive. | ||
/// | ||
/// `retries` configures the amount of keep-alive probes. | ||
pub fn tcp_keepalive(mut self, timeout: Duration, retries: u32) -> Self { | ||
if timeout.is_zero() { | ||
self.tcp_keepalive = None; | ||
return self; | ||
} | ||
|
||
let mut keepalive = socket2::TcpKeepalive::new().with_time(timeout); | ||
#[cfg(not(any(target_os = "openbsd", target_os = "redox", target_os = "solaris")))] | ||
{ | ||
keepalive = keepalive.with_interval(timeout); | ||
} | ||
#[cfg(not(any( | ||
target_os = "openbsd", | ||
target_os = "redox", | ||
target_os = "solaris", | ||
target_os = "windows" | ||
)))] | ||
{ | ||
keepalive = keepalive.with_retries(retries); | ||
} | ||
self.tcp_keepalive = Some(keepalive); | ||
|
||
self | ||
} | ||
|
||
/// Configures an idle timeout for the connection. | ||
/// | ||
/// Whenever there is no activity on a connection for the specified timeout, | ||
/// the connection is closed. | ||
/// | ||
/// Note: This limits the total idle time of a duration and unlike read and write timeouts | ||
/// also limits the time a connection is kept alive without requests. | ||
pub fn idle_timeout(mut self, idle_timeout: Option<Duration>) -> Self { | ||
self.idle_timeout = idle_timeout; | ||
self | ||
} | ||
} | ||
|
||
impl<S> Accept<TcpStream, S> for RelayAcceptor { | ||
type Stream = IdleTimeout<TcpStream>; | ||
type Service = S; | ||
type Future = std::future::Ready<io::Result<(Self::Stream, Self::Service)>>; | ||
|
||
fn accept(&self, stream: TcpStream, service: S) -> Self::Future { | ||
let mut keepalive = "ok"; | ||
let mut nodelay = "ok"; | ||
|
||
if let Some(tcp_keepalive) = &self.tcp_keepalive { | ||
let sock_ref = socket2::SockRef::from(&stream); | ||
if let Err(e) = sock_ref.set_tcp_keepalive(tcp_keepalive) { | ||
relay_log::trace!("error trying to set TCP keepalive: {e}"); | ||
keepalive = "error"; | ||
} | ||
} | ||
|
||
if let Err(e) = stream.set_nodelay(true) { | ||
relay_log::trace!("failed to set TCP_NODELAY: {e}"); | ||
nodelay = "error"; | ||
} | ||
|
||
relay_statsd::metric!( | ||
counter(RelayCounters::ServerSocketAccept) += 1, | ||
keepalive = keepalive, | ||
nodelay = nodelay | ||
); | ||
|
||
let stream = IdleTimeout::new(stream, self.idle_timeout); | ||
std::future::ready(Ok((stream, service))) | ||
} | ||
} |
Oops, something went wrong.