Skip to content

Commit

Permalink
net: move max/max-outbound to CConnman
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni authored and Fuzzbawls committed Aug 25, 2020
1 parent 2bf0921 commit 15eed91
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,10 @@ bool AppInit2()
// ********************************************************* Step 2: parameter interactions
// Make sure enough file descriptors are available
int nBind = std::max((int)mapArgs.count("-bind") + (int)mapArgs.count("-whitebind"), 1);
nMaxConnections = GetArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
int nUserMaxConnections = GetArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
int nMaxConnections = std::max(nUserMaxConnections, 0);

// Trim requested connection counts, to fit into system limitations
nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0);
int nFD = RaiseFileDescriptorLimit(nMaxConnections + MIN_CORE_FILEDESCRIPTORS);
if (nFD < MIN_CORE_FILEDESCRIPTORS)
Expand Down Expand Up @@ -1878,7 +1881,8 @@ bool AppInit2()
StartTorControl(threadGroup);

std::string strNodeError;
if(!StartNode(connman, threadGroup, scheduler, nLocalServices, nRelevantServices, strNodeError))
int nMaxOutbound = std::min(MAX_OUTBOUND_CONNECTIONS, nMaxConnections);
if(!StartNode(connman, threadGroup, scheduler, nLocalServices, nRelevantServices, nMaxConnections, nMaxOutbound, strNodeError))
return UIError(strNodeError);

#ifdef ENABLE_WALLET
Expand Down
22 changes: 12 additions & 10 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@

namespace
{
const int MAX_OUTBOUND_CONNECTIONS = 16;
const int MAX_FEELER_CONNECTIONS = 1;
}

Expand All @@ -78,7 +77,6 @@ RecursiveMutex cs_mapLocalHost;
std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
static bool vfLimited[NET_MAX] = {};
static CNode* pnodeLocalHost = NULL;
int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
std::string strSubVersion;

std::map<CInv, CDataStream> mapRelay;
Expand Down Expand Up @@ -999,7 +997,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
CAddress addr;
int nInbound = 0;
int nMaxInbound = nMaxConnections - (MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS);
int nMaxInbound = nMaxConnections - (nMaxOutbound + MAX_FEELER_CONNECTIONS);
assert(nMaxInbound > 0);

if (hSocket != INVALID_SOCKET)
Expand Down Expand Up @@ -1598,7 +1596,7 @@ void CConnman::ThreadOpenConnections()
// * Only make a feeler connection once every few minutes.
//
bool fFeeler = false;
if (nOutbound >= MAX_OUTBOUND_CONNECTIONS) {
if (nOutbound >= nMaxOutbound) {
int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
if (nTime > nNextFeeler) {
nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
Expand Down Expand Up @@ -1961,13 +1959,15 @@ CConnman::CConnman()
nSendBufferMaxSize = 0;
nReceiveFloodSize = 0;
semOutbound = NULL;
nMaxConnections = 0;
nMaxOutbound = 0;
}

bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServices, ServiceFlags nRelevantServices, std::string& strNodeError)
bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServices, ServiceFlags nRelevantServices, int nMaxConnectionsIn, int nMaxOutboundIn, std::string& strNodeError)
{
Discover(threadGroup);

bool ret = connman.Start(threadGroup, scheduler, nLocalServices, nRelevantServices, strNodeError);
bool ret = connman.Start(threadGroup, scheduler, nLocalServices, nRelevantServices, nMaxConnectionsIn, nMaxOutboundIn, strNodeError);

return ret;
}
Expand All @@ -1977,13 +1977,16 @@ NodeId CConnman::GetNewNodeId()
return nLastNodeId.fetch_add(1, std::memory_order_relaxed);
}

bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServicesIn, ServiceFlags nRelevantServicesIn, std::string& strNodeError)
bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServicesIn, ServiceFlags nRelevantServicesIn, int nMaxConnectionsIn, int nMaxOutboundIn, std::string& strNodeError)
{
nTotalBytesRecv = 0;
nTotalBytesSent = 0;
nLocalServices = nLocalServicesIn;
nRelevantServices = nRelevantServicesIn;

nMaxConnections = nMaxConnectionsIn;
nMaxOutbound = std::min((nMaxOutboundIn), nMaxConnections);

nSendBufferMaxSize = 1000*GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
nReceiveFloodSize = 1000*GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);

Expand Down Expand Up @@ -2027,8 +2030,7 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, Se

if (semOutbound == NULL) {
// initialize semaphore
int nMaxOutbound = std::min((MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS), nMaxConnections);
semOutbound = new CSemaphore(nMaxOutbound);
semOutbound = new CSemaphore(std::min((nMaxOutbound + MAX_FEELER_CONNECTIONS), nMaxConnections));
}

if (pnodeLocalHost == nullptr) {
Expand Down Expand Up @@ -2104,7 +2106,7 @@ void CExplicitNetCleanup::callCleanup()
void CConnman::Stop()
{
if (semOutbound)
for (int i=0; i<(MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS); i++)
for (int i=0; i<(nMaxOutbound + MAX_FEELER_CONNECTIONS); i++)
semOutbound->post();

if (fAddressesInitialized)
Expand Down
12 changes: 7 additions & 5 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ static const unsigned int MAX_ADDR_TO_SEND = 1000;
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 2 * 1024 * 1024;
/** Maximum length of strSubVer in `version` message */
static const unsigned int MAX_SUBVERSION_LENGTH = 256;
/** Maximum number of outgoing nodes */
static const int MAX_OUTBOUND_CONNECTIONS = 16;
/** -listen default */
static const bool DEFAULT_LISTEN = true;
/** -upnp default */
Expand Down Expand Up @@ -111,7 +113,7 @@ class CConnman

CConnman();
~CConnman();
bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServicesIn, ServiceFlags nRelevantServicesIn, std::string& strNodeError);
bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServicesIn, ServiceFlags nRelevantServicesIn, int nMaxConnectionsIn, int nMaxOutboundIn, std::string& strNodeError);
void Stop();
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant* grantOutbound = NULL, const char* strDest = NULL, bool fOneShot = false, bool fFeeler = false);
Expand Down Expand Up @@ -267,12 +269,14 @@ class CConnman
ServiceFlags nRelevantServices;

CSemaphore *semOutbound;
int nMaxConnections;
int nMaxOutbound;
};
extern std::unique_ptr<CConnman> g_connman;
void MapPort(bool fUseUPnP);
unsigned short GetListenPort();
bool BindListenPort(const CService& bindAddr, std::string& strError, bool fWhitelisted = false);
bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServices, ServiceFlags nRelevantServices, std::string& strNodeError);
bool StartNode(CConnman& connman, boost::thread_group& threadGroup, CScheduler& scheduler, ServiceFlags nLocalServices, ServiceFlags nRelevantServices, int nMaxConnections, int nMaxOutbound, std::string& strNodeError);
bool StopNode(CConnman& connman);
size_t SocketSendData(CNode* pnode);
void CheckOffsetDisconnectedPeers(const CNetAddr& ip);
Expand Down Expand Up @@ -336,12 +340,10 @@ bool validateMasternodeIP(const std::string& addrStr); // valid, reacha
extern bool fDiscover;
extern bool fListen;

/** Maximum number of connections to simultaneously allow (aka connection slots) */
extern int nMaxConnections;

extern std::map<CInv, CDataStream> mapRelay;
extern std::deque<std::pair<int64_t, CInv> > vRelayExpiration;
extern RecursiveMutex cs_mapRelay;

extern limitedmap<CInv, int64_t> mapAlreadyAskedFor;

/** Subversion as sent to the P2P network in `version` messages */
Expand Down

0 comments on commit 15eed91

Please sign in to comment.