mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 14:38:29 +01:00
Merge bitcoin/bitcoin#28464: net: improve max-connection limits code
df69b22f2edoc: improve documentation around connection limit maximums (Amiti Uttarwar)adc171edf4scripted-diff: Rename connection limit variables (Amiti Uttarwar)e9fd9c0225net: add m_max_inbound to connman (Amiti Uttarwar)c25e0e0555net, refactor: move calculations for connection type limits into connman (Amiti Uttarwar) Pull request description: This is joint work with amitiuttarwar. This has the first few commits of #28463. It is not strictly a prerequisite for that, but has changes that in our opinion make sense on their own. It improves the handling of maximum numbers for different connection types (that are set during init and don’t change after) by: * moving all calculations into one place, `CConnMan::Init()`. Before, they were dispersed between `Init`, `CConnman::Init` and other parts of `CConnman`, resulting in some duplicated test code. * removing the possibility of having a negative maximum of inbound connections, which is hard to argue about * renaming of variables and doc improvements ACKs for top commit: amitiuttarwar: co-author review ACKdf69b22f2enaumenkogs: ACKdf69b22f2eachow101: ACKdf69b22f2eTree-SHA512: 913d56136bc1df739978de50db67302f88bac2a9d34748ae96763288d97093e998fc0f94f9b6eff12867712d7e86225af6128f4170bf2b5b8ab76f024870a22c
This commit is contained in:
15
src/net.cpp
15
src/net.cpp
@@ -1730,7 +1730,6 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
|
||||
const CAddress& addr)
|
||||
{
|
||||
int nInbound = 0;
|
||||
int nMaxInbound = nMaxConnections - m_max_outbound;
|
||||
|
||||
AddWhitelistPermissionFlags(permission_flags, addr);
|
||||
if (NetPermissions::HasFlag(permission_flags, NetPermissionFlags::Implicit)) {
|
||||
@@ -1776,13 +1775,13 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
|
||||
|
||||
// Only accept connections from discouraged peers if our inbound slots aren't (almost) full.
|
||||
bool discouraged = m_banman && m_banman->IsDiscouraged(addr);
|
||||
if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && nInbound + 1 >= nMaxInbound && discouraged)
|
||||
if (!NetPermissions::HasFlag(permission_flags, NetPermissionFlags::NoBan) && nInbound + 1 >= m_max_inbound && discouraged)
|
||||
{
|
||||
LogPrint(BCLog::NET, "connection from %s dropped (discouraged)\n", addr.ToStringAddrPort());
|
||||
return;
|
||||
}
|
||||
|
||||
if (nInbound >= nMaxInbound)
|
||||
if (nInbound >= m_max_inbound)
|
||||
{
|
||||
if (!AttemptToEvictConnection()) {
|
||||
// No connection to evict, disconnect the new connection
|
||||
@@ -2733,7 +2732,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
||||
// different netgroups in ipv4/ipv6 networks + all peers in Tor/I2P/CJDNS networks.
|
||||
// Don't record addrman failure attempts when node is offline. This can be identified since all local
|
||||
// network connections (if any) belong in the same netgroup, and the size of `outbound_ipv46_peer_netgroups` would only be 1.
|
||||
const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(nMaxConnections - 1, 2)};
|
||||
const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(m_max_automatic_connections - 1, 2)};
|
||||
// Use BIP324 transport when both us and them have NODE_V2_P2P set.
|
||||
const bool use_v2transport(addrConnect.nServices & GetLocalServices() & NODE_P2P_V2);
|
||||
OpenNetworkConnection(addrConnect, count_failures, std::move(grant), /*strDest=*/nullptr, conn_type, use_v2transport);
|
||||
@@ -3208,11 +3207,11 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
|
||||
|
||||
if (semOutbound == nullptr) {
|
||||
// initialize semaphore
|
||||
semOutbound = std::make_unique<CSemaphore>(std::min(m_max_outbound, nMaxConnections));
|
||||
semOutbound = std::make_unique<CSemaphore>(std::min(m_max_automatic_outbound, m_max_automatic_connections));
|
||||
}
|
||||
if (semAddnode == nullptr) {
|
||||
// initialize semaphore
|
||||
semAddnode = std::make_unique<CSemaphore>(nMaxAddnode);
|
||||
semAddnode = std::make_unique<CSemaphore>(m_max_addnode);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -3293,13 +3292,13 @@ void CConnman::Interrupt()
|
||||
g_socks5_interrupt();
|
||||
|
||||
if (semOutbound) {
|
||||
for (int i=0; i<m_max_outbound; i++) {
|
||||
for (int i=0; i<m_max_automatic_outbound; i++) {
|
||||
semOutbound->post();
|
||||
}
|
||||
}
|
||||
|
||||
if (semAddnode) {
|
||||
for (int i=0; i<nMaxAddnode; i++) {
|
||||
for (int i=0; i<m_max_addnode; i++) {
|
||||
semAddnode->post();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user