Skip to content

Commit 2d4327d

Browse files
committed
net: Allow connecting to extra outbound peers
1 parent ba216b5 commit 2d4327d

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/net.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,36 @@ void CConnman::ProcessOneShot()
16931693
}
16941694
}
16951695

1696+
bool CConnman::GetTryNewOutboundPeer()
1697+
{
1698+
return m_try_another_outbound_peer;
1699+
}
1700+
1701+
void CConnman::SetTryNewOutboundPeer(bool flag)
1702+
{
1703+
m_try_another_outbound_peer = flag;
1704+
}
1705+
1706+
// Return the number of peers we have over our outbound connection limit
1707+
// Exclude peers that are marked for disconnect, or are going to be
1708+
// disconnected soon (eg one-shots and feelers)
1709+
// Also exclude peers that haven't finished initial connection handshake yet
1710+
// (so that we don't decide we're over our desired connection limit, and then
1711+
// evict some peer that has finished the handshake)
1712+
int CConnman::GetExtraOutboundCount()
1713+
{
1714+
int nOutbound = 0;
1715+
{
1716+
LOCK(cs_vNodes);
1717+
for (CNode* pnode : vNodes) {
1718+
if (!pnode->fInbound && !pnode->m_manual_connection && !pnode->fFeeler && !pnode->fDisconnect && !pnode->fOneShot && pnode->fSuccessfullyConnected) {
1719+
++nOutbound;
1720+
}
1721+
}
1722+
}
1723+
return std::max(nOutbound - nMaxOutbound, 0);
1724+
}
1725+
16961726
void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
16971727
{
16981728
// Connect to specific addresses
@@ -1781,7 +1811,8 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17811811
// * Only make a feeler connection once every few minutes.
17821812
//
17831813
bool fFeeler = false;
1784-
if (nOutbound >= nMaxOutbound) {
1814+
1815+
if (nOutbound >= nMaxOutbound && !GetTryNewOutboundPeer()) {
17851816
int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
17861817
if (nTime > nNextFeeler) {
17871818
nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
@@ -2204,6 +2235,7 @@ CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSe
22042235
semOutbound = nullptr;
22052236
semAddnode = nullptr;
22062237
flagInterruptMsgProc = false;
2238+
SetTryNewOutboundPeer(false);
22072239

22082240
Options connOptions;
22092241
Init(connOptions);

src/net.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,19 @@ class CConnman
251251
void GetBanned(banmap_t &banmap);
252252
void SetBanned(const banmap_t &banmap);
253253

254+
// This allows temporarily exceeding nMaxOutbound, with the goal of finding
255+
// a peer that is better than all our current peers.
256+
void SetTryNewOutboundPeer(bool flag);
257+
bool GetTryNewOutboundPeer();
258+
259+
// Return the number of outbound peers we have in excess of our target (eg,
260+
// if we previously called SetTryNewOutboundPeer(true), and have since set
261+
// to false, we may have extra peers that we wish to disconnect). This may
262+
// return a value less than (num_outbound_connections - num_outbound_slots)
263+
// in cases where some outbound connections are not yet fully connected, or
264+
// not yet fully disconnected.
265+
int GetExtraOutboundCount();
266+
254267
bool AddNode(const std::string& node);
255268
bool RemoveAddedNode(const std::string& node);
256269
std::vector<AddedNodeInfo> GetAddedNodeInfo();
@@ -413,6 +426,11 @@ class CConnman
413426
std::thread threadOpenAddedConnections;
414427
std::thread threadOpenConnections;
415428
std::thread threadMessageHandler;
429+
430+
/** flag for deciding to connect to an extra outbound peer,
431+
* in excess of nMaxOutbound
432+
* This takes the place of a feeler connection */
433+
std::atomic_bool m_try_another_outbound_peer;
416434
};
417435
extern std::unique_ptr<CConnman> g_connman;
418436
void Discover(boost::thread_group& threadGroup);

0 commit comments

Comments
 (0)