aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
authorJeff Garzik <jgarzik@exmulti.com>2012-11-15 18:20:26 -0500
committerPieter Wuille <sipa@ulyssis.org>2013-03-29 23:56:25 +0100
commitb9ff2970b9fbb24e2fffc449b4ef478d019633d8 (patch)
treefbcd6be3b8d3f6ae43b7a3516a4b053211e3aa62 /src/net.cpp
parentbc2f5aa72cfb3f456280a6d34c5d425bf24b009c (diff)
P2P: improve RX/TX flow control
1) "optimistic write": Push each message to kernel socket buffer immediately. 2) If there is write data at select time, that implies send() blocked during optimistic write. Drain write queue, before receiving any more messages. This avoids needlessly queueing received data, if the remote peer is not themselves receiving data. Result: write buffer (and thus memory usage) is kept small, DoS potential is slightly lower, and TCP flow control signalling is properly utilized. The kernel will queue data into the socket buffer, then signal the remote peer to stop sending data, until we resume reading again.
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 96719367ce..eafb335642 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -855,14 +855,18 @@ void ThreadSocketHandler2(void* parg)
{
if (pnode->hSocket == INVALID_SOCKET)
continue;
- FD_SET(pnode->hSocket, &fdsetRecv);
- FD_SET(pnode->hSocket, &fdsetError);
- hSocketMax = max(hSocketMax, pnode->hSocket);
- have_fds = true;
{
TRY_LOCK(pnode->cs_vSend, lockSend);
- if (lockSend && !pnode->vSend.empty())
- FD_SET(pnode->hSocket, &fdsetSend);
+ if (lockSend) {
+ // do not read, if draining write queue
+ if (!pnode->vSend.empty())
+ FD_SET(pnode->hSocket, &fdsetSend);
+ else
+ FD_SET(pnode->hSocket, &fdsetRecv);
+ FD_SET(pnode->hSocket, &fdsetError);
+ hSocketMax = max(hSocketMax, pnode->hSocket);
+ have_fds = true;
+ }
}
}
}