diff options
author | Gregory Maxwell <greg@xiph.org> | 2015-10-21 23:52:29 +0000 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2015-10-23 10:05:06 +0200 |
commit | 5297194bbd6e0d6730515567248caf9c135e296c (patch) | |
tree | 405a9815f86e380a779a7f9df95a5e45cf0b1d9f | |
parent | 7e9a9874f3ccaee9dced3477adec3aaf918afc03 (diff) | |
download | bitcoin-5297194bbd6e0d6730515567248caf9c135e296c.tar.xz |
Set TCP_NODELAY on P2P sockets.
Nagle appears to be a significant contributor to latency now that the static
sleeps are gone. Most of our messages are relatively large compared to
IP + TCP so I do not expect this to create enormous overhead.
This may also reduce traffic burstyness somewhat.
Conflicts:
src/net.cpp
Rebased-From: a4e28b3d1e5c95eb0c87f144851cd65048c3e0bc
Github-Pull: #6867
-rw-r--r-- | src/compat.h | 1 | ||||
-rw-r--r-- | src/net.cpp | 12 | ||||
-rw-r--r-- | src/netbase.cpp | 9 |
3 files changed, 21 insertions, 1 deletions
diff --git a/src/compat.h b/src/compat.h index e08e348ed8..651e641daa 100644 --- a/src/compat.h +++ b/src/compat.h @@ -38,6 +38,7 @@ #include <sys/types.h> #include <net/if.h> #include <netinet/in.h> +#include <netinet/tcp.h> #include <arpa/inet.h> #include <ifaddrs.h> #include <limits.h> diff --git a/src/net.cpp b/src/net.cpp index 375c00308c..0fe52556a8 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -894,6 +894,15 @@ void ThreadSocketHandler() } else { + // According to the internet TCP_NODELAY is not carried into accepted sockets + // on all platforms. Set it again here just to be sure. + int set = 1; +#ifdef WIN32 + setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int)); +#else + setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int)); +#endif + CNode* pnode = new CNode(hSocket, addr, "", true); pnode->AddRef(); pnode->fWhitelisted = whitelisted; @@ -1530,8 +1539,11 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste // Allow binding if the port is still in TIME_WAIT state after // the program was closed and restarted. setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int)); + // Disable Nagle's algorithm + setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&nOne, sizeof(int)); #else setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int)); + setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int)); #endif // Set to non-blocking, incoming connections will also inherit this diff --git a/src/netbase.cpp b/src/netbase.cpp index ca864104c0..0b38e4dba5 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -407,12 +407,19 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe if (hSocket == INVALID_SOCKET) return false; -#ifdef SO_NOSIGPIPE int set = 1; +#ifdef SO_NOSIGPIPE // Different way of disabling SIGPIPE on BSD setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int)); #endif + //Disable Nagle's algorithm +#ifdef WIN32 + setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int)); +#else + setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int)); +#endif + // Set to non-blocking if (!SetSocketNonBlocking(hSocket, true)) return error("ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError())); |