Browse Source

[core] disable Nagle algorithm (TCP_NODELAY)

disable Nagle algorithm (TCP_NODELAY) on client sockets
personal/stbuehler/mod-csrf-old
Glenn Strauss 5 years ago
parent
commit
416b5729fb
  1. 3
      src/connections.c
  2. 30
      src/network.c
  3. 2
      src/network.h
  4. 4
      src/server.c

3
src/connections.c

@ -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);
}
}

30
src/network.c

@ -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;
@ -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:

2
src/network.h

@ -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);

4
src/server.c

@ -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;

Loading…
Cancel
Save