From b1d0f8d20b8da6e5eefe42d6f70e9a2f4879d89a Mon Sep 17 00:00:00 2001 From: Walt Steverson Date: Mon, 15 Feb 2016 21:01:24 -0600 Subject: [PATCH] Add config option for enabling the TCP_NODELAY option on client sockets. --- src/civetweb.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/civetweb.c b/src/civetweb.c index 42e57d2f..fc7311e9 100644 --- a/src/civetweb.c +++ b/src/civetweb.c @@ -397,6 +397,7 @@ struct pollfd { #include #include #include +#include typedef const void *SOCK_OPT_TYPE; #if defined(ANDROID) @@ -1084,6 +1085,7 @@ enum { #endif ACCESS_CONTROL_ALLOW_ORIGIN, ERROR_PAGES, + CONFIG_TCP_NODELAY, /* Prepended CONFIG_ to avoid conflict with the socket option typedef TCP_NODELAY */ NUM_OPTIONS }; @@ -1153,6 +1155,7 @@ static struct mg_option config_options[] = { #endif {"access_control_allow_origin", CONFIG_TYPE_STRING, "*"}, {"error_pages", CONFIG_TYPE_DIRECTORY, NULL}, + {"tcp_nodelay", CONFIG_TYPE_BOOLEAN, "no"}, {NULL, CONFIG_TYPE_UNKNOWN, NULL}}; @@ -11999,6 +12002,27 @@ accept_new_connection(const struct socket *listener, struct mg_context *ctx) strerror(ERRNO)); } + + /* Disable TCP Nagle's algorithm. Normally TCP packets are coalesced + * to effectively fill up the underlying IP packet payload and reduce + * the overhead of sending lots of small buffers. However this hurts + * the server's throughput (ie. operations per second) when HTTP 1.1 + * persistent connections are used and the responses are relatively + * small (eg. less than 1400 bytes). + */ + if (ctx && mg_strcasecmp(ctx->config[CONFIG_TCP_NODELAY], "yes") == 0) { + if (setsockopt(so.sock, + IPPROTO_TCP, + TCP_NODELAY, + (SOCK_OPT_TYPE)&on, + sizeof(on)) != 0) { + mg_cry(fc(ctx), + "%s: setsockopt(IPPROTO_TCP TCP_NODELAY) failed: %s", + __func__, + strerror(ERRNO)); + } + } + if (ctx && ctx->config[REQUEST_TIMEOUT]) { timeout = atoi(ctx->config[REQUEST_TIMEOUT]); } else {