Skip to content

Commit 092631f

Browse files
committed
Condition variable for outbound connection slots
Keep a global counter for nOutbound, protected with its own waitable critical section, and wait when all outbound slots are filled, rather than polling. This removes the (on average) 1 second delay between a lost connection and a new connection attempt, and may speed up shutdowns.
1 parent 712fd18 commit 092631f

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/net.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ map<CInv, int64> mapAlreadyAskedFor;
6464
set<CNetAddr> setservAddNodeAddresses;
6565
CCriticalSection cs_setservAddNodeAddresses;
6666

67+
static CWaitableCriticalSection csOutbound;
68+
static int nOutbound = 0;
69+
static CConditionVariable condOutbound;
6770

6871

6972
unsigned short GetListenPort()
@@ -460,6 +463,8 @@ CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
460463
pnode->AddRef();
461464
CRITICAL_BLOCK(cs_vNodes)
462465
vNodes.push_back(pnode);
466+
WAITABLE_CRITICAL_BLOCK(csOutbound)
467+
nOutbound++;
463468

464469
pnode->nTimeConnected = GetTime();
465470
return pnode;
@@ -610,6 +615,15 @@ void ThreadSocketHandler2(void* parg)
610615
// remove from vNodes
611616
vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
612617

618+
if (!pnode->fInbound)
619+
WAITABLE_CRITICAL_BLOCK(csOutbound)
620+
{
621+
nOutbound--;
622+
623+
// Connection slot(s) were removed, notify connection creator(s)
624+
NOTIFY(condOutbound);
625+
}
626+
613627
// close socket and cleanup
614628
pnode->CloseSocketDisconnect();
615629
pnode->Cleanup();
@@ -1278,32 +1292,20 @@ void ThreadOpenConnections2(void* parg)
12781292
int64 nStart = GetTime();
12791293
loop
12801294
{
1281-
int nOutbound = 0;
1282-
12831295
vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
12841296
Sleep(500);
12851297
vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
12861298
if (fShutdown)
12871299
return;
12881300

12891301
// Limit outbound connections
1290-
loop
1291-
{
1292-
nOutbound = 0;
1293-
CRITICAL_BLOCK(cs_vNodes)
1294-
BOOST_FOREACH(CNode* pnode, vNodes)
1295-
if (!pnode->fInbound)
1296-
nOutbound++;
1297-
int nMaxOutboundConnections = MAX_OUTBOUND_CONNECTIONS;
1298-
nMaxOutboundConnections = min(nMaxOutboundConnections, (int)GetArg("-maxconnections", 125));
1299-
if (nOutbound < nMaxOutboundConnections)
1300-
break;
1301-
vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
1302-
Sleep(2000);
1303-
vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
1304-
if (fShutdown)
1305-
return;
1306-
}
1302+
int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, (int)GetArg("-maxconnections", 125));
1303+
vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
1304+
WAITABLE_CRITICAL_BLOCK(csOutbound)
1305+
WAIT(condOutbound, fShutdown || nOutbound < nMaxOutbound);
1306+
vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
1307+
if (fShutdown)
1308+
return;
13071309

13081310
bool fAddSeeds = false;
13091311

@@ -1752,6 +1754,7 @@ bool StopNode()
17521754
fShutdown = true;
17531755
nTransactionsUpdated++;
17541756
int64 nStart = GetTime();
1757+
NOTIFY_ALL(condOutbound);
17551758
do
17561759
{
17571760
int nThreadsRunning = 0;

0 commit comments

Comments
 (0)