net: move max/max-outbound to CConnman

This commit is contained in:
Cory Fields 2016-05-22 09:52:03 +02:00
parent 8a593694b1
commit fdf69ff21a
3 changed files with 22 additions and 18 deletions

View File

@ -861,7 +861,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
// Make sure enough file descriptors are available // Make sure enough file descriptors are available
int nBind = std::max((int)mapArgs.count("-bind") + (int)mapArgs.count("-whitebind"), 1); int nBind = std::max((int)mapArgs.count("-bind") + (int)mapArgs.count("-whitebind"), 1);
int nUserMaxConnections = GetArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS); int nUserMaxConnections = GetArg("-maxconnections", DEFAULT_MAX_PEER_CONNECTIONS);
nMaxConnections = std::max(nUserMaxConnections, 0); int nMaxConnections = std::max(nUserMaxConnections, 0);
// Trim requested connection counts, to fit into system limitations // Trim requested connection counts, to fit into system limitations
nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0); nMaxConnections = std::max(std::min(nMaxConnections, (int)(FD_SETSIZE - nBind - MIN_CORE_FILEDESCRIPTORS)), 0);
@ -1509,7 +1509,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
StartTorControl(threadGroup, scheduler); StartTorControl(threadGroup, scheduler);
std::string strNodeError; 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 InitError(strNodeError); return InitError(strNodeError);
// ********************************************************* Step 12: finished // ********************************************************* Step 12: finished

View File

@ -63,7 +63,6 @@
namespace { namespace {
const int MAX_OUTBOUND_CONNECTIONS = 8;
const int MAX_FEELER_CONNECTIONS = 1; const int MAX_FEELER_CONNECTIONS = 1;
} }
@ -79,7 +78,6 @@ CCriticalSection cs_mapLocalHost;
std::map<CNetAddr, LocalServiceInfo> mapLocalHost; std::map<CNetAddr, LocalServiceInfo> mapLocalHost;
static bool vfLimited[NET_MAX] = {}; static bool vfLimited[NET_MAX] = {};
static CNode* pnodeLocalHost = NULL; static CNode* pnodeLocalHost = NULL;
int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
std::string strSubVersion; std::string strSubVersion;
limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ); limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
@ -974,7 +972,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len); SOCKET hSocket = accept(hListenSocket.socket, (struct sockaddr*)&sockaddr, &len);
CAddress addr; CAddress addr;
int nInbound = 0; int nInbound = 0;
int nMaxInbound = nMaxConnections - (MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS); int nMaxInbound = nMaxConnections - (nMaxOutbound + MAX_FEELER_CONNECTIONS);
assert(nMaxInbound > 0); assert(nMaxInbound > 0);
if (hSocket != INVALID_SOCKET) if (hSocket != INVALID_SOCKET)
@ -1626,7 +1624,7 @@ void CConnman::ThreadOpenConnections()
} }
} }
} }
assert(nOutbound <= (MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS)); assert(nOutbound <= (nMaxOutbound + MAX_FEELER_CONNECTIONS));
// Feeler Connections // Feeler Connections
// //
@ -1641,7 +1639,7 @@ void CConnman::ThreadOpenConnections()
// * Only make a feeler connection once every few minutes. // * Only make a feeler connection once every few minutes.
// //
bool fFeeler = false; bool fFeeler = false;
if (nOutbound >= MAX_OUTBOUND_CONNECTIONS) { if (nOutbound >= nMaxOutbound) {
int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds). int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
if (nTime > nNextFeeler) { if (nTime > nNextFeeler) {
nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL); nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
@ -2038,13 +2036,15 @@ CConnman::CConnman()
nSendBufferMaxSize = 0; nSendBufferMaxSize = 0;
nReceiveFloodSize = 0; nReceiveFloodSize = 0;
semOutbound = NULL; 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); Discover(threadGroup);
bool ret = connman.Start(threadGroup, scheduler, nLocalServices, nRelevantServices, strNodeError); bool ret = connman.Start(threadGroup, scheduler, nLocalServices, nRelevantServices, nMaxConnectionsIn, nMaxOutboundIn, strNodeError);
return ret; return ret;
} }
@ -2054,7 +2054,7 @@ NodeId CConnman::GetNewNodeId()
return nLastNodeId.fetch_add(1, std::memory_order_relaxed); 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; nTotalBytesRecv = 0;
nTotalBytesSent = 0; nTotalBytesSent = 0;
@ -2065,6 +2065,9 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, Se
nRelevantServices = nRelevantServicesIn; nRelevantServices = nRelevantServicesIn;
nMaxOutboundCycleStartTime = 0; nMaxOutboundCycleStartTime = 0;
nMaxConnections = nMaxConnectionsIn;
nMaxOutbound = std::min((nMaxOutboundIn), nMaxConnections);
nSendBufferMaxSize = 1000*GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER); nSendBufferMaxSize = 1000*GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
nReceiveFloodSize = 1000*GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER); nReceiveFloodSize = 1000*GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
@ -2106,8 +2109,7 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, Se
if (semOutbound == NULL) { if (semOutbound == NULL) {
// initialize semaphore // initialize semaphore
int nMaxOutbound = std::min((MAX_OUTBOUND_CONNECTIONS + MAX_FEELER_CONNECTIONS), nMaxConnections); semOutbound = new CSemaphore(std::min((nMaxOutbound + MAX_FEELER_CONNECTIONS), nMaxConnections));
semOutbound = new CSemaphore(nMaxOutbound);
} }
if (pnodeLocalHost == NULL) { if (pnodeLocalHost == NULL) {
@ -2174,7 +2176,7 @@ instance_of_cnetcleanup;
void CConnman::Stop() void CConnman::Stop()
{ {
if (semOutbound) 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(); semOutbound->post();
if (fAddressesInitialized) if (fAddressesInitialized)

View File

@ -54,6 +54,8 @@ static const unsigned int MAX_ADDR_TO_SEND = 1000;
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 4 * 1000 * 1000; static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 4 * 1000 * 1000;
/** Maximum length of strSubVer in `version` message */ /** Maximum length of strSubVer in `version` message */
static const unsigned int MAX_SUBVERSION_LENGTH = 256; static const unsigned int MAX_SUBVERSION_LENGTH = 256;
/** Maximum number of outgoing nodes */
static const int MAX_OUTBOUND_CONNECTIONS = 8;
/** -listen default */ /** -listen default */
static const bool DEFAULT_LISTEN = true; static const bool DEFAULT_LISTEN = true;
/** -upnp default */ /** -upnp default */
@ -107,7 +109,7 @@ public:
CConnman(); 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(); void Stop();
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false); 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); bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false, bool fFeeler = false);
@ -284,12 +286,14 @@ private:
ServiceFlags nRelevantServices; ServiceFlags nRelevantServices;
CSemaphore *semOutbound; CSemaphore *semOutbound;
int nMaxConnections;
int nMaxOutbound;
}; };
extern std::unique_ptr<CConnman> g_connman; extern std::unique_ptr<CConnman> g_connman;
void MapPort(bool fUseUPnP); void MapPort(bool fUseUPnP);
unsigned short GetListenPort(); unsigned short GetListenPort();
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false); 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); bool StopNode(CConnman& connman);
size_t SocketSendData(CNode *pnode); size_t SocketSendData(CNode *pnode);
@ -353,9 +357,6 @@ extern bool fDiscover;
extern bool fListen; extern bool fListen;
extern bool fRelayTxes; extern bool fRelayTxes;
/** Maximum number of connections to simultaneously allow (aka connection slots) */
extern int nMaxConnections;
extern limitedmap<uint256, int64_t> mapAlreadyAskedFor; extern limitedmap<uint256, int64_t> mapAlreadyAskedFor;
/** Subversion as sent to the P2P network in `version` messages */ /** Subversion as sent to the P2P network in `version` messages */