Skip to content

Commit

Permalink
ref(relay): Make tcp listen backlog configurable (#3899)
Browse files Browse the repository at this point in the history
The current default from the standard library is 128 and we've seen that
this is not enough.
  • Loading branch information
Dav1dde authored Aug 6, 2024
1 parent be606fe commit d7774e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Add `client_sample_rate` to spans, pulled from the trace context. ([#3872](https://github.com/getsentry/relay/pull/3872))
- Collect SDK information in profile chunks. ([#3882](https://github.com/getsentry/relay/pull/3882))
- Introduce `trim = "disabled"` type attribute to prevent trimming of spans. ([#3877](https://github.com/getsentry/relay/pull/3877))
- Make the tcp listen backlog configurable and raise the default to 1024. ([#3899](https://github.com/getsentry/relay/pull/3899))

## 24.7.1

Expand Down
14 changes: 14 additions & 0 deletions relay-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,14 @@ struct Limits {
///
/// By default keep-alive is set to a 5 seconds.
keepalive_timeout: u64,
/// The TCP listen backlog.
///
/// Configures the TCP listen backlog for the listening socket of Relay.
/// See [`man listen(2)`](https://man7.org/linux/man-pages/man2/listen.2.html)
/// for a more detailed description of the listen backlog.
///
/// Defaults to `1024`, a value [google has been using for a long time](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=19f92a030ca6d772ab44b22ee6a01378a8cb32d4).
pub tcp_listen_backlog: u32,
}

impl Default for Limits {
Expand Down Expand Up @@ -643,6 +651,7 @@ impl Default for Limits {
query_timeout: 30,
shutdown_timeout: 10,
keepalive_timeout: 5,
tcp_listen_backlog: 1024,
}
}
}
Expand Down Expand Up @@ -2286,6 +2295,11 @@ impl Config {
Duration::from_secs(self.values.limits.keepalive_timeout)
}

/// TCP listen backlog to configure on Relay's listening socket.
pub fn tcp_listen_backlog(&self) -> u32 {
self.values.limits.tcp_listen_backlog
}

/// Returns the number of cores to use for thread pools.
pub fn cpu_concurrency(&self) -> usize {
self.values.limits.max_thread_count
Expand Down
15 changes: 14 additions & 1 deletion relay-server/src/services/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io;
use std::net::{SocketAddr, TcpListener};
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -7,6 +8,7 @@ use axum::ServiceExt;
use axum_server::{AddrIncomingConfig, Handle, HttpConfig};
use relay_config::Config;
use relay_system::{Controller, Service, Shutdown};
use tokio::net::TcpSocket;
use tower::ServiceBuilder;
use tower_http::compression::predicate::SizeAbove;
use tower_http::compression::{CompressionLayer, DefaultPredicate, Predicate};
Expand Down Expand Up @@ -124,7 +126,8 @@ impl Service for HttpServer {
.build();

let handle = Handle::new();
match TcpListener::bind(config.listen_addr()) {

match create_listener(config.listen_addr(), config.tcp_listen_backlog()) {
Ok(listener) => {
listener.set_nonblocking(true).ok();
let server = axum_server::from_tcp(listener)
Expand Down Expand Up @@ -154,3 +157,13 @@ impl Service for HttpServer {
});
}
}

fn create_listener(addr: SocketAddr, backlog: u32) -> io::Result<TcpListener> {
let socket = match addr {
SocketAddr::V4(_) => TcpSocket::new_v4(),
SocketAddr::V6(_) => TcpSocket::new_v6(),
}?;
socket.bind(addr)?;

socket.listen(backlog)?.into_std()
}

0 comments on commit d7774e4

Please sign in to comment.