Skip to content

Commit

Permalink
[core] disable Nagle algorithm (TCP_NODELAY)
Browse files Browse the repository at this point in the history
disable Nagle algorithm (TCP_NODELAY) on client sockets
  • Loading branch information
gstrauss committed Jun 30, 2016
1 parent eefb94b commit 416b572
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/connections.c
Expand Up @@ -909,6 +909,9 @@ connection *connection_accept(server *srv, server_socket *srv_socket) {
}
return NULL;
} else {
if (cnt_addr.plain.sa_family != AF_UNIX) {
network_accept_tcp_nagle_disable(cnt);
}
return connection_accepted(srv, srv_socket, &cnt_addr, cnt);
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/network.c
Expand Up @@ -50,6 +50,28 @@ static void ssl_info_callback(const SSL *ssl, int where, int ret) {
}
#endif

void
network_accept_tcp_nagle_disable (const int fd)
{
static int noinherit_tcpnodelay = -1;
int opt;

if (!noinherit_tcpnodelay) /* TCP_NODELAY inherited from listen socket */
return;

if (noinherit_tcpnodelay < 0) {
socklen_t optlen = sizeof(opt);
if (0 == getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen)) {
noinherit_tcpnodelay = !opt;
if (opt) /* TCP_NODELAY inherited from listen socket */
return;
}
}

opt = 1;
(void)setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt));
}

static handler_t network_server_handle_fdevent(server *srv, void *context, int revents) {
server_socket *srv_socket = (server_socket *)context;
connection *con;
Expand Down Expand Up @@ -408,6 +430,14 @@ static int network_server_init(server *srv, buffer *host_token, specific_config
goto error_free_socket;
}

if (srv_socket->addr.plain.sa_family != AF_UNIX) {
val = 1;
if (setsockopt(srv_socket->fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) < 0) {
log_error_write(srv, __FILE__, __LINE__, "ss", "socketsockopt(TCP_NODELAY) failed:", strerror(errno));
goto error_free_socket;
}
}

if (0 != bind(srv_socket->fd, (struct sockaddr *) &(srv_socket->addr), addr_len)) {
switch(srv_socket->addr.plain.sa_family) {
case AF_UNIX:
Expand Down
2 changes: 2 additions & 0 deletions src/network.h
Expand Up @@ -4,6 +4,8 @@

#include "server.h"

void network_accept_tcp_nagle_disable(int fd);

int network_write_chunkqueue(server *srv, connection *con, chunkqueue *c, off_t max_bytes);

int network_init(server *srv);
Expand Down
4 changes: 4 additions & 0 deletions src/server.c
Expand Up @@ -530,6 +530,10 @@ static int server_oneshot_init(server *srv, int fd) {
return 0;
}

if (cnt_addr.plain.sa_family != AF_UNIX) {
network_accept_tcp_nagle_disable(fd);
}

con = connection_accepted(srv, srv_socket, &cnt_addr, fd);
if (NULL == con) return 0;

Expand Down

0 comments on commit 416b572

Please sign in to comment.