From a18016240521dd7089f356231a837c843caf169b Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Fri, 6 Dec 2024 19:59:45 +0300 Subject: [PATCH] Avoid redundant requests of announce entries from libtorrent PR #21949. --- src/base/bittorrent/sessionimpl.cpp | 25 ++++++++----------------- src/base/bittorrent/sessionimpl.h | 3 +-- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 4deb2b3ed..8b7254690 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -5541,8 +5541,6 @@ void SessionImpl::readAlerts() // Some torrents may become "finished" after different alerts handling. processPendingFinishedTorrents(); - - processTrackerStatuses(); } void SessionImpl::handleAddTorrentAlert(const lt::add_torrent_alert *alert) @@ -6232,7 +6230,10 @@ void SessionImpl::handleTrackerAlert(const lt::tracker_alert *alert) if (!torrent) return; + const auto prevSize = m_updatedTrackerStatuses.size(); QMap &updateInfo = m_updatedTrackerStatuses[torrent->nativeHandle()][std::string(alert->tracker_url())][alert->local_endpoint]; + if (prevSize < m_updatedTrackerStatuses.size()) + updateTrackerEntryStatuses(torrent->nativeHandle()); if (alert->type() == lt::tracker_reply_alert::alert_type) { @@ -6294,17 +6295,6 @@ void SessionImpl::handleTorrentConflictAlert(const lt::torrent_conflict_alert *a } #endif -void SessionImpl::processTrackerStatuses() -{ - if (m_updatedTrackerStatuses.isEmpty()) - return; - - for (auto it = m_updatedTrackerStatuses.cbegin(); it != m_updatedTrackerStatuses.cend(); ++it) - updateTrackerEntryStatuses(it.key(), it.value()); - - m_updatedTrackerStatuses.clear(); -} - void SessionImpl::saveStatistics() const { if (!m_isStatisticsDirty) @@ -6329,20 +6319,21 @@ void SessionImpl::loadStatistics() m_previouslyUploaded = value[u"AlltimeUL"_s].toLongLong(); } -void SessionImpl::updateTrackerEntryStatuses(lt::torrent_handle torrentHandle, QHash>> updatedTrackers) +void SessionImpl::updateTrackerEntryStatuses(lt::torrent_handle torrentHandle) { - invokeAsync([this, torrentHandle = std::move(torrentHandle), updatedTrackers = std::move(updatedTrackers)]() mutable + invokeAsync([this, torrentHandle = std::move(torrentHandle)]() mutable { try { std::vector nativeTrackers = torrentHandle.trackers(); - invoke([this, torrentHandle, nativeTrackers = std::move(nativeTrackers) - , updatedTrackers = std::move(updatedTrackers)] + invoke([this, torrentHandle, nativeTrackers = std::move(nativeTrackers)] { TorrentImpl *torrent = m_torrents.value(torrentHandle.info_hash()); if (!torrent || torrent->isStopped()) return; + QHash>> updatedTrackers = m_updatedTrackerStatuses.take(torrentHandle); + QHash trackers; trackers.reserve(updatedTrackers.size()); for (const lt::announce_entry &announceEntry : nativeTrackers) diff --git a/src/base/bittorrent/sessionimpl.h b/src/base/bittorrent/sessionimpl.h index 8136a4749..21971f266 100644 --- a/src/base/bittorrent/sessionimpl.h +++ b/src/base/bittorrent/sessionimpl.h @@ -546,7 +546,6 @@ namespace BitTorrent void populateAdditionalTrackers(); void enableIPFilter(); void disableIPFilter(); - void processTrackerStatuses(); void processTorrentShareLimits(TorrentImpl *torrent); void populateExcludedFileNamesRegExpList(); void prepareStartup(); @@ -610,7 +609,7 @@ namespace BitTorrent void saveStatistics() const; void loadStatistics(); - void updateTrackerEntryStatuses(lt::torrent_handle torrentHandle, QHash>> updatedTrackers); + void updateTrackerEntryStatuses(lt::torrent_handle torrentHandle); void handleRemovedTorrent(const TorrentID &torrentID, const QString &partfileRemoveError = {});