From 967f2c0d7f56702059f433c248c1d2640dc26fde Mon Sep 17 00:00:00 2001 From: David Steele Date: Wed, 1 Apr 2020 16:56:15 -0400 Subject: [PATCH] Enable TCP_NODELAY. PostgreSQL enables this option when available which seems like a good idea since we also buffer transmissions. Note that as in PostgreSQL there is no way to disable this option. --- src/common/io/socket/common.c | 9 +++++++++ test/src/module/common/ioTlsTest.c | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/common/io/socket/common.c b/src/common/io/socket/common.c index 47579db07..ee563adf6 100644 --- a/src/common/io/socket/common.c +++ b/src/common/io/socket/common.c @@ -63,6 +63,15 @@ sckOptionSet(int fd) ASSERT(socketLocal.init); + // Disable the Nagle algorithm. This means that segments are always sent as soon as possible, even if there is only a small + // amount of data. Our internal buffering minimizes the benefit of this optimization so lower latency is preferred. +#ifdef TCP_NODELAY + int socketValue = 1; + + THROW_ON_SYS_ERROR( + setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &socketValue, sizeof(int)) == -1, ProtocolError, "unable set TCP_NODELAY"); +#endif + // Enable TCP keepalives if (socketLocal.keepAlive) { diff --git a/test/src/module/common/ioTlsTest.c b/test/src/module/common/ioTlsTest.c index 8aa501b08..534bfc2ae 100644 --- a/test/src/module/common/ioTlsTest.c +++ b/test/src/module/common/ioTlsTest.c @@ -125,12 +125,20 @@ testRun(void) THROW_ON_SYS_ERROR(fd == -1, HostConnectError, "unable to create socket"); // --------------------------------------------------------------------------------------------------------------------- - TEST_TITLE("enable keep-alive and options"); + TEST_TITLE("enable options"); sckInit(true, 32, 3113, 818); sckOptionSet(fd); socklen_t socketValueSize = sizeof(int); + int noDelayValue = 0; + + THROW_ON_SYS_ERROR( + getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &noDelayValue, &socketValueSize) == -1, ProtocolError, + "unable get TCP_NO_DELAY"); + + TEST_RESULT_INT(noDelayValue, 1, "check TCP_NODELAY"); + int keepAliveValue = 0; THROW_ON_SYS_ERROR(