diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index a7819c976..29b0cde53 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -2054,7 +2054,12 @@ constexpr struct { return compare(a, b) < 0; } -} ComparePeerByActivity{}; +} ComparePeerByMostActive{}; + +constexpr auto ComparePeerByLeastActive = [](tr_peer const* a, tr_peer const* b) +{ + return ComparePeerByMostActive(b, a); +}; [[nodiscard]] auto getPeersToClose(tr_swarm const* const swarm, time_t const now_sec) { @@ -2085,15 +2090,21 @@ void closeBadPeers(tr_swarm* s, time_t const now_sec) void enforceSwarmPeerLimit(tr_swarm* swarm, size_t max) { // do we have too many peers? - if (auto const n = swarm->peerCount(); n <= max) + auto const n = swarm->peerCount(); + if (n <= max) { return; } // close all but the `max` most active - auto peers = swarm->peers; - std::partial_sort(std::begin(peers), std::begin(peers) + max, std::end(peers), ComparePeerByActivity); - std::for_each(std::begin(peers) + max, std::end(peers), closePeer); + auto peers = std::vector{ n - max }; + std::partial_sort_copy( + std::begin(swarm->peers), + std::end(swarm->peers), + std::begin(peers), + std::end(peers), + ComparePeerByLeastActive); + std::for_each(std::begin(peers), std::end(peers), closePeer); } void enforceSessionPeerLimit(tr_session* session) @@ -2116,7 +2127,7 @@ void enforceSessionPeerLimit(tr_session* session) TR_ASSERT(tr_peerMsgs::size() == std::size(peers)); if (std::size(peers) > max) { - std::partial_sort(std::begin(peers), std::begin(peers) + max, std::end(peers), ComparePeerByActivity); + std::partial_sort(std::begin(peers), std::begin(peers) + max, std::end(peers), ComparePeerByMostActive); std::for_each(std::begin(peers) + max, std::end(peers), closePeer); } }