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

Code before:
                    LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
                    pnode->fDisconnect = true;
                }
                else if (!pnode->fSuccessfullyConnected)
                {
                    LogPrintf("version handshake timeout from %d\n", pnode->id);
                    pnode->fDisconnect = true;
                }
            }
        }
        //
        // Reduce number of connections, if needed
        //
        if (vNodesCopy.size() > (size_t)nMaxConnections)
        {
            DisconnectUnusedNodes();
            DeleteDisconnectedNodes();
            AttemptToEvictConnection();
        }

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

void CConnman::SetMaxConnections(int newMaxConnections)
{
    nMaxConnections = std::min(newMaxConnections, nAvailableFds);
}


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






#ifdef USE_UPNP
void ThreadMapPort()
{

Review comment (bug):
I think it would be good to have a debug message here, so that it's possible to see that this is being triggered. Something like:

```suggestion
        {
            LogPrint("net", "%s: attempting to reduce connections: max=%u current=%u", __func__, vNodesCopy.size(), nMaxConnections);
```

Maybe that should even be LogPrintf and part of the default output rather than only in debug mode.

Code after:
                else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL : 90*60))
                {
                    LogPrintf("socket receive timeout: %is\n", nTime - pnode->nLastRecv);
                    pnode->fDisconnect = true;
                }
                else if (pnode->nPingNonceSent && pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < GetTimeMicros())
                {
                    LogPrintf("ping timeout: %fs\n", 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart));
                    pnode->fDisconnect = true;
                }
                else if (!pnode->fSuccessfullyConnected)
                {
                    LogPrintf("version handshake timeout from %d\n", pnode->id);
                    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);
    }