Skip to content

Commit b042181

Browse files
authored
Merge pull request #2426 from jamescowens/implement_max_connections_clamp
net: Implement an upper limit of 950 for max network connections
2 parents dd37c68 + b38c17a commit b042181

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ void SetupServerArgs()
479479
ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
480480
argsman.AddArg("-port=<port>", "Listen for connections on <port> (default: 32749 or testnet: 32748)",
481481
ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
482-
argsman.AddArg("-maxconnections=<n>", "Maintain at most <n> connections to peers (default: 125)",
482+
argsman.AddArg("-maxconnections=<n>", "Maintain at most <n> connections to peers (default: 125, upper limit of 950)",
483483
ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
484484
argsman.AddArg("-maxoutboundconnections=<n>", "Maximum number of outbound connections (default: 8)",
485485
ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);

src/net.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,8 @@ void ThreadSocketHandler2(void* parg)
927927
CAddress addr;
928928
int nInbound = 0;
929929

930+
int max_connections = std::min<int>(gArgs.GetArg("-maxconnections", 125), 950);
931+
930932
if (hSocket != INVALID_SOCKET)
931933
if (!addr.SetSockAddr((const struct sockaddr*)&sockaddr))
932934
LogPrintf("Warning: Unknown socket family");
@@ -944,12 +946,11 @@ void ThreadSocketHandler2(void* parg)
944946
if (nErr != WSAEWOULDBLOCK)
945947
LogPrintf("socket error accept INVALID_SOCKET: %d", nErr);
946948
}
947-
else if (nInbound >= gArgs.GetArg("-maxconnections", 125) - MAX_OUTBOUND_CONNECTIONS)
949+
else if (nInbound >= max_connections - MAX_OUTBOUND_CONNECTIONS)
948950
{
949951
LogPrint(BCLog::LogFlags::NET,
950-
"Surpassed max inbound connections maxconnections:%" PRId64 " minus max_outbound:%i",
951-
gArgs.GetArg("-maxconnections", 125),
952-
MAX_OUTBOUND_CONNECTIONS);
952+
"Surpassed max inbound connections of %i",
953+
std::max<int>(max_connections - MAX_OUTBOUND_CONNECTIONS, 0));
953954

954955
closesocket(hSocket);
955956
}
@@ -1996,15 +1997,17 @@ void StartNode(void* parg)
19961997
util::ThreadSetInternalName("grc-nodestart");
19971998

19981999
fShutdown = false;
1999-
MAX_OUTBOUND_CONNECTIONS = (int)gArgs.GetArg("-maxoutboundconnections", 8);
2000+
MAX_OUTBOUND_CONNECTIONS = (int) gArgs.GetArg("-maxoutboundconnections", 8);
2001+
int max_connections = std::min<int>(gArgs.GetArg("-maxconnections", 125), 950);
20002002
int nMaxOutbound = 0;
20012003
if (semOutbound == nullptr) {
20022004
// initialize semaphore
2003-
nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, (int)gArgs.GetArg("-maxconnections", 125));
2005+
nMaxOutbound = std::min<int>(MAX_OUTBOUND_CONNECTIONS, max_connections);
20042006
semOutbound = new CSemaphore(nMaxOutbound);
20052007
}
20062008

2007-
LogPrintf("Using %i OutboundConnections with a MaxConnections of %" PRId64, MAX_OUTBOUND_CONNECTIONS, gArgs.GetArg("-maxconnections", 125));
2009+
LogPrintf("Using %i OutboundConnections with a MaxConnections of %" PRId64,
2010+
nMaxOutbound, max_connections);
20082011

20092012
if (pnodeLocalHost == nullptr)
20102013
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices));

0 commit comments

Comments
 (0)