Skip to content

Commit

Permalink
Add set_tcp_nodelay option to disable Nagle's algorithm.
Browse files Browse the repository at this point in the history
Bug: #433
  • Loading branch information
ralight committed Feb 14, 2018
1 parent 81cb7ab commit ec63d7b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Broker:
- Fix upgrade_outgoing_qos for retained message. Closes #534.
- Fix CONNACK message not being sent for unauthorised connect on websockets.
Closes #8.
- Add set_tcp_nodelay option to allow Nagle's algorithm to be disabled on
client sockets. Closes #433.

Client library:
- Outgoing messages with QoS>1 are no longer retried after a timeout period.
Expand Down
11 changes: 11 additions & 0 deletions man/mosquitto.conf.5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,17 @@
<para>Reloaded on reload signal.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>set_tcp_nodelay</option> [ true | false ]</term>
<listitem>
<para>If set to true, the TCP_NODELAY option will be set on
client sockets to disable Nagle's algorithm. This
has the effect of reducing latency of some messages
at potentially increasing the number of TCP packets
being sent. Defaults to false.</para>
<para>Reloaded on reload signal.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>store_clean_interval</option> <replaceable>seconds</replaceable></term>
<listitem>
Expand Down
5 changes: 5 additions & 0 deletions mosquitto.conf
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@
# This is a non-standard option explicitly disallowed by the spec.
#upgrade_outgoing_qos false

# Disable Nagle's algorithm on client sockets. This has the effect of reducing
# latency of individual messages at the potential cost of increasing the number
# of packets being sent.
#set_tcp_nodelay false

# =================================================================
# Default listener
# =================================================================
Expand Down
3 changes: 3 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ static void config__init_reload(struct mosquitto__config *config)
mosquitto__free(config->psk_file);
config->psk_file = NULL;
config->queue_qos0_messages = false;
config->set_tcp_nodelay = false;
config->sys_interval = 10;
config->upgrade_outgoing_qos = false;
if(config->auth_plugins){
Expand Down Expand Up @@ -1575,6 +1576,8 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, const
#else
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available.");
#endif
}else if(!strcmp(token, "set_tcp_nodelay")){
if(conf__parse_bool(&token, "set_tcp_nodelay", &config->set_tcp_nodelay, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "start_type")){
#ifdef WITH_BRIDGE
if(reload) continue; // FIXME
Expand Down
1 change: 1 addition & 0 deletions src/mosquitto_broker_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ struct mosquitto__config {
char *pid_file;
char *psk_file;
bool queue_qos0_messages;
bool set_tcp_nodelay;
int sys_interval;
bool upgrade_outgoing_qos;
char *user;
Expand Down
7 changes: 7 additions & 0 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and the Eclipse Distribution License is available at
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
Expand Down Expand Up @@ -120,6 +121,12 @@ int net__socket_accept(struct mosquitto_db *db, mosq_sock_t listensock)
return -1;
}
#endif

if(db->config->set_tcp_nodelay){
int flag = 1;
setsockopt(new_sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
}

new_context = context__init(db, new_sock);
if(!new_context){
COMPAT_CLOSE(new_sock);
Expand Down

0 comments on commit ec63d7b

Please sign in to comment.