From a1282cdec5e19d5e8f60de05adf9bfe85dacf4a3 Mon Sep 17 00:00:00 2001 From: Fedor Smirnov Date: Tue, 28 May 2024 16:32:16 +0200 Subject: [PATCH] Option to configure TCP no_delay (#872) * feat(mqtt-client): add option to set nodelay flag for TCP connection This extension allows users to specify whether the `nodelay` flag is set for the TCP connection from the MQTT client to the broker. This flag can help reduce latency by disabling Nagle's algorithm, which is beneficial in scenarios requiring minimal delay. Signed off: FedorSmirnov89 Issue: https://github.com/bytebeamio/rumqtt/issues/871 * Apply suggestions from code review Co-authored-by: Devdutt Shenoi * Removed the nodelay example following review suggestion --------- Co-authored-by: Devdutt Shenoi --- rumqttc/CHANGELOG.md | 1 + rumqttc/src/eventloop.rs | 2 ++ rumqttc/src/lib.rs | 6 ++++++ 3 files changed, 9 insertions(+) diff --git a/rumqttc/CHANGELOG.md b/rumqttc/CHANGELOG.md index 4353bf4a4..ce94f0052 100644 --- a/rumqttc/CHANGELOG.md +++ b/rumqttc/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `ConnectionAborted` variant on `StateError` type to denote abrupt end to a connection * `set_session_expiry_interval` and `session_expiry_interval` methods on `MqttOptions`. * `Auth` packet as per MQTT5 standards +* Allow configuring the `nodelay` property of underlying TCP client with the `tcp_nodelay` field in `NetworkOptions` ### Changed diff --git a/rumqttc/src/eventloop.rs b/rumqttc/src/eventloop.rs index 1527c8603..f6730fe33 100644 --- a/rumqttc/src/eventloop.rs +++ b/rumqttc/src/eventloop.rs @@ -331,6 +331,8 @@ pub(crate) async fn socket_connect( SocketAddr::V6(_) => TcpSocket::new_v6()?, }; + socket.set_nodelay(network_options.tcp_nodelay)?; + if let Some(send_buff_size) = network_options.tcp_send_buffer_size { socket.set_send_buffer_size(send_buff_size).unwrap(); } diff --git a/rumqttc/src/lib.rs b/rumqttc/src/lib.rs index 29cad1a34..93887bb11 100644 --- a/rumqttc/src/lib.rs +++ b/rumqttc/src/lib.rs @@ -369,6 +369,7 @@ impl From for TlsConfiguration { pub struct NetworkOptions { tcp_send_buffer_size: Option, tcp_recv_buffer_size: Option, + tcp_nodelay: bool, conn_timeout: u64, #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))] bind_device: Option, @@ -379,12 +380,17 @@ impl NetworkOptions { NetworkOptions { tcp_send_buffer_size: None, tcp_recv_buffer_size: None, + tcp_nodelay: false, conn_timeout: 5, #[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))] bind_device: None, } } + pub fn set_tcp_nodelay(&mut self, nodelay: bool) { + self.tcp_nodelay = nodelay; + } + pub fn set_tcp_send_buffer_size(&mut self, size: u32) { self.tcp_send_buffer_size = Some(size); }