From 88fe76781c1dc9e723c1b142aaf8b84dc556c9ef Mon Sep 17 00:00:00 2001 From: Yat Ho Date: Fri, 20 Oct 2023 02:05:19 +0800 Subject: [PATCH] perf: connectable pool housekeeping (#6111) --- libtransmission/peer-mgr.cc | 18 ++++++++++++++++++ libtransmission/peer-mgr.h | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index e8e8e5d5d2c..b7e59c9f63e 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -339,6 +339,23 @@ class tr_swarm } } + void remove_inactive_peer_info() noexcept + { + auto const now = tr_time(); + for (auto iter = std::begin(connectable_pool), end = std::end(connectable_pool); iter != end;) + { + auto& [socket_address, peer_info] = *iter; + if (peer_info.is_inactive(now)) + { + iter = connectable_pool.erase(iter); + } + else + { + ++iter; + } + } + } + [[nodiscard]] uint16_t countActiveWebseeds(uint64_t now) const noexcept { if (!tor->is_running() || tor->is_done()) @@ -1145,6 +1162,7 @@ void tr_peerMgr::refillUpkeep() const for (auto* const tor : session->torrents()) { tor->swarm->cancelOldRequests(); + tor->swarm->remove_inactive_peer_info(); } } diff --git a/libtransmission/peer-mgr.h b/libtransmission/peer-mgr.h index 946ca519403..1f8a6341c67 100644 --- a/libtransmission/peer-mgr.h +++ b/libtransmission/peer-mgr.h @@ -300,6 +300,11 @@ class tr_peer_info return now - std::max(piece_data_at_, connection_changed_at_); } + [[nodiscard]] auto is_inactive(time_t const now) const noexcept + { + return !is_in_use() && now - connection_changed_at_ >= UselessThresSecs; + } + // --- constexpr void on_connection_failed() noexcept @@ -417,6 +422,7 @@ class tr_peer_info // the minimum we'll wait before attempting to reconnect to a peer static auto constexpr MinimumReconnectIntervalSecs = time_t{ 5U }; + static auto constexpr UselessThresSecs = time_t{ 24 * 60 * 60 }; static auto inline n_known_connectable = size_t{};