Browse Source

net: Allow connecting to extra outbound peers

0.16
Suhas Daftuar 7 years ago
parent
commit
2d4327db19
  1. 34
      src/net.cpp
  2. 18
      src/net.h

34
src/net.cpp

@ -1693,6 +1693,36 @@ void CConnman::ProcessOneShot()
} }
} }
bool CConnman::GetTryNewOutboundPeer()
{
return m_try_another_outbound_peer;
}
void CConnman::SetTryNewOutboundPeer(bool flag)
{
m_try_another_outbound_peer = flag;
}
// Return the number of peers we have over our outbound connection limit
// Exclude peers that are marked for disconnect, or are going to be
// disconnected soon (eg one-shots and feelers)
// Also exclude peers that haven't finished initial connection handshake yet
// (so that we don't decide we're over our desired connection limit, and then
// evict some peer that has finished the handshake)
int CConnman::GetExtraOutboundCount()
{
int nOutbound = 0;
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes) {
if (!pnode->fInbound && !pnode->m_manual_connection && !pnode->fFeeler && !pnode->fDisconnect && !pnode->fOneShot && pnode->fSuccessfullyConnected) {
++nOutbound;
}
}
}
return std::max(nOutbound - nMaxOutbound, 0);
}
void CConnman::ThreadOpenConnections(const std::vector<std::string> connect) void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
{ {
// Connect to specific addresses // Connect to specific addresses
@ -1781,7 +1811,8 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
// * 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 >= nMaxOutbound) {
if (nOutbound >= nMaxOutbound && !GetTryNewOutboundPeer()) {
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);
@ -2204,6 +2235,7 @@ CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSe
semOutbound = nullptr; semOutbound = nullptr;
semAddnode = nullptr; semAddnode = nullptr;
flagInterruptMsgProc = false; flagInterruptMsgProc = false;
SetTryNewOutboundPeer(false);
Options connOptions; Options connOptions;
Init(connOptions); Init(connOptions);

18
src/net.h

@ -251,6 +251,19 @@ public:
void GetBanned(banmap_t &banmap); void GetBanned(banmap_t &banmap);
void SetBanned(const banmap_t &banmap); void SetBanned(const banmap_t &banmap);
// This allows temporarily exceeding nMaxOutbound, with the goal of finding
// a peer that is better than all our current peers.
void SetTryNewOutboundPeer(bool flag);
bool GetTryNewOutboundPeer();
// Return the number of outbound peers we have in excess of our target (eg,
// if we previously called SetTryNewOutboundPeer(true), and have since set
// to false, we may have extra peers that we wish to disconnect). This may
// return a value less than (num_outbound_connections - num_outbound_slots)
// in cases where some outbound connections are not yet fully connected, or
// not yet fully disconnected.
int GetExtraOutboundCount();
bool AddNode(const std::string& node); bool AddNode(const std::string& node);
bool RemoveAddedNode(const std::string& node); bool RemoveAddedNode(const std::string& node);
std::vector<AddedNodeInfo> GetAddedNodeInfo(); std::vector<AddedNodeInfo> GetAddedNodeInfo();
@ -413,6 +426,11 @@ private:
std::thread threadOpenAddedConnections; std::thread threadOpenAddedConnections;
std::thread threadOpenConnections; std::thread threadOpenConnections;
std::thread threadMessageHandler; std::thread threadMessageHandler;
/** flag for deciding to connect to an extra outbound peer,
* in excess of nMaxOutbound
* This takes the place of a feeler connection */
std::atomic_bool m_try_another_outbound_peer;
}; };
extern std::unique_ptr<CConnman> g_connman; extern std::unique_ptr<CConnman> g_connman;
void Discover(boost::thread_group& threadGroup); void Discover(boost::thread_group& threadGroup);

Loading…
Cancel
Save