Repository: dogecoin/dogecoin
File: src/net.cpp (C++)

Code before:
                    pnode->fDisconnect = true;
                }
            }
        }
        //
        // Reduce number of connections, if needed
        //
        if (vNodesCopy.size() - (nUnevictableConnections + nWhitelistedConnections) > (long unsigned int)nMaxConnections)
        {
            LogPrintf("%s: attempting to reduce connections: max=%u current=%u\n", __func__, nMaxConnections, vNodesCopy.size());
            DisconnectUnusedNodes();
            DeleteDisconnectedNodes();
            AttemptToEvictConnection();
        }

        {
            LOCK(cs_vNodes);
            BOOST_FOREACH(CNode* pnode, vNodesCopy)
                pnode->Release();
        }
    }
}

void CConnman::SetMaxConnections(int newMaxConnections)
{
    newMaxConnections = std::max(newMaxConnections, MAX_ADDNODE_CONNECTIONS);
    nMaxConnections = std::min(newMaxConnections, nAvailableFds);
    if (nMaxConnections != newMaxConnections) {
        LogPrintf("%s: capped new maxconnections request of %d to %d\n", __func__, newMaxConnections, nMaxConnections);
    }
}


void CConnman::WakeMessageHandler()
{
    {
        std::lock_guard<std::mutex> lock(mutexMsgProc);
        fMsgProcWake = true;
    }
    condMsgProc.notify_one();
}






#ifdef USE_UPNP
void ThreadMapPort()
{
    std::string port = strprintf("%u", GetListenPort());

Review comment (suggestion):
I think we need to do the `std::max` against `MAX_ADDNODE_CONNECTIONS + PROTECTED_INBOUND_PEERS` here: when I `setmaxconnections` below `28` but above `MAX_ADDNODE_CONNECTIONS ` (eg: `10`), I get into a loop trying to get under 28 but it of course never gets there and just keeps trying...

```
2022-01-22 21:28:45 ThreadSocketHandler: attempting to reduce connections: max=10 current=28
2022-01-22 21:28:45 ThreadSocketHandler: attempting to reduce connections: max=10 current=28
2022-01-22 21:28:45 ThreadSocketHandler: attempting to reduce connections: max=10 current=28
```

Code after:
                    pnode->fDisconnect = true;
                }
            }
        }
        //
        // Reduce number of connections, if needed
        //

        long unsigned int nKeepConnections = nUnevictableConnections + nWhitelistedConnections;
        long unsigned int nNodesCopy       = vNodesCopy.size();

        if (nNodesCopy > nKeepConnections && (nNodesCopy > (long unsigned int)nMaxConnections))
        {
            LogPrintf("%s: attempting to reduce connections: max=%u current=%u keep=%u\n", __func__, nMaxConnections, nNodesCopy, nKeepConnections);
            DisconnectUnusedNodes();
            DeleteDisconnectedNodes();

            if (!AttemptToEvictConnection()) {
                LogPrint("net", "Failed to evict connections\n");
            }
        }

        {
            LOCK(cs_vNodes);
            BOOST_FOREACH(CNode* pnode, vNodesCopy)
                pnode->Release();
        }
    }
}

void CConnman::SetMaxConnections(int newMaxConnections)
{
    newMaxConnections = std::max(newMaxConnections, MAX_ADDNODE_CONNECTIONS + PROTECTED_INBOUND_PEERS);
    nMaxConnections = std::min(newMaxConnections, nAvailableFds);

    if (nMaxConnections != newMaxConnections) {
        LogPrintf("%s: capped new maxconnections request of %d to %d\n", __func__, newMaxConnections, nMaxConnections);
    }
}


void CConnman::WakeMessageHandler()
{
    {
        std::lock_guard<std::mutex> lock(mutexMsgProc);
        fMsgProcWake = true;
    }
    condMsgProc.notify_one();
}