Skip to content

Commit

Permalink
Avoid redundant requests of announce entries from libtorrent
Browse files Browse the repository at this point in the history
PR #21949.
  • Loading branch information
glassez authored Dec 6, 2024
1 parent 2d1c4fc commit a180162
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
25 changes: 8 additions & 17 deletions src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -6232,7 +6230,10 @@ void SessionImpl::handleTrackerAlert(const lt::tracker_alert *alert)
if (!torrent)
return;

const auto prevSize = m_updatedTrackerStatuses.size();
QMap<int, int> &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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -6329,20 +6319,21 @@ void SessionImpl::loadStatistics()
m_previouslyUploaded = value[u"AlltimeUL"_s].toLongLong();
}

void SessionImpl::updateTrackerEntryStatuses(lt::torrent_handle torrentHandle, QHash<std::string, QHash<lt::tcp::endpoint, QMap<int, int>>> 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<lt::announce_entry> 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<std::string, QHash<lt::tcp::endpoint, QMap<int, int>>> updatedTrackers = m_updatedTrackerStatuses.take(torrentHandle);

QHash<QString, TrackerEntryStatus> trackers;
trackers.reserve(updatedTrackers.size());
for (const lt::announce_entry &announceEntry : nativeTrackers)
Expand Down
3 changes: 1 addition & 2 deletions src/base/bittorrent/sessionimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,6 @@ namespace BitTorrent
void populateAdditionalTrackers();
void enableIPFilter();
void disableIPFilter();
void processTrackerStatuses();
void processTorrentShareLimits(TorrentImpl *torrent);
void populateExcludedFileNamesRegExpList();
void prepareStartup();
Expand Down Expand Up @@ -610,7 +609,7 @@ namespace BitTorrent
void saveStatistics() const;
void loadStatistics();

void updateTrackerEntryStatuses(lt::torrent_handle torrentHandle, QHash<std::string, QHash<lt::tcp::endpoint, QMap<int, int>>> updatedTrackers);
void updateTrackerEntryStatuses(lt::torrent_handle torrentHandle);

void handleRemovedTorrent(const TorrentID &torrentID, const QString &partfileRemoveError = {});

Expand Down

0 comments on commit a180162

Please sign in to comment.