@@ -12,6 +12,7 @@ use crate::{core::Client, handler::ConnectHandlerError};
1212/// Obtained from [`Client::builder`].
1313#[ derive( Debug ) ]
1414pub 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 {
3940impl 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.
0 commit comments