Skip to content

Commit eb6890c

Browse files
feat: make TCP nodelay configurable
1 parent 27c912e commit eb6890c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

watermelon/src/client/builder.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{core::Client, handler::ConnectHandlerError};
1212
/// Obtained from [`Client::builder`].
1313
#[derive(Debug)]
1414
pub struct ClientBuilder {
15+
pub(crate) tcp_nodelay: bool,
1516
pub(crate) auth_method: Option<AuthenticationMethod>,
1617
pub(crate) connect_timeout: Duration,
1718
pub(crate) write_delay: Duration,
@@ -39,6 +40,7 @@ pub enum Echo {
3940
impl ClientBuilder {
4041
pub(super) fn new() -> Self {
4142
Self {
43+
tcp_nodelay: true,
4244
auth_method: None,
4345
connect_timeout: Duration::from_secs(30),
4446
write_delay: Duration::ZERO,
@@ -104,6 +106,23 @@ impl ClientBuilder {
104106
this
105107
}
106108

109+
/// Controls the Nagle algorithm for kernel-level bandwidth vs latency optimization
110+
///
111+
/// Setting this to `true` disables Nagle's algorithm. The kernel
112+
/// will send packets immediately, reducing latency but potentially increasing
113+
/// bandwidth usage due to smaller but more frequent packets.
114+
///
115+
/// Setting this to `false` enables the Nagle algorithm. The kernel
116+
/// will delay small writes while unacknowledged packets are in flight,
117+
/// increasing bandwidth efficiency and latency.
118+
///
119+
/// Default: true
120+
#[must_use]
121+
pub fn tcp_nodelay(mut self, tcp_nodelay: bool) -> Self {
122+
self.tcp_nodelay = tcp_nodelay;
123+
self
124+
}
125+
107126
/// Define an authentication method
108127
#[must_use]
109128
pub fn authentication_method(mut self, auth_method: Option<AuthenticationMethod>) -> Self {
@@ -127,6 +146,11 @@ impl ClientBuilder {
127146
/// a value greater than a few seconds may break the client in
128147
/// unexpected ways.
129148
///
149+
/// Compared to [`ClientBuilder::tcp_nodelay`], buffering happens
150+
/// at the message serialization layer, improving the bandwidth
151+
/// efficiency of TLS connections and
152+
/// [`ClientBuilder::non_standard_zstd_compression_level`].
153+
///
130154
/// Setting this to [`Duration::ZERO`] causes the client to send messages
131155
/// as fast as the network will allow, trading off smaller packets for
132156
/// lower latency.

watermelon/src/handler/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ impl Handler {
148148
mut recycle: RecycledHandler,
149149
) -> Result<Option<Self>, (ConnectHandlerError, RecycledHandler)> {
150150
let mut flags = ConnectFlags::default();
151+
flags.tcp_nodelay = builder.tcp_nodelay;
151152
flags.echo = matches!(builder.echo, Echo::Allow);
152153
#[cfg(feature = "non-standard-zstd")]
153154
{

0 commit comments

Comments
 (0)