Skip to content

Commit

Permalink
Option to configure TCP no_delay (#872)
Browse files Browse the repository at this point in the history
* 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: #871

* Apply suggestions from code review

Co-authored-by: Devdutt Shenoi <[email protected]>

* Removed the nodelay example following review suggestion

---------

Co-authored-by: Devdutt Shenoi <[email protected]>
  • Loading branch information
FedorSmirnov89 and de-sh authored May 28, 2024
1 parent 005c60e commit a1282cd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions rumqttc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions rumqttc/src/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 6 additions & 0 deletions rumqttc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ impl From<ClientConfig> for TlsConfiguration {
pub struct NetworkOptions {
tcp_send_buffer_size: Option<u32>,
tcp_recv_buffer_size: Option<u32>,
tcp_nodelay: bool,
conn_timeout: u64,
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
bind_device: Option<String>,
Expand All @@ -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);
}
Expand Down

0 comments on commit a1282cd

Please sign in to comment.