Skip to content

Commit 762bf69

Browse files
committed
refactor: [torrust#1543] Optimization: Don't load number of downloads from DB if not needed
1 parent e107614 commit 762bf69

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

packages/torrent-repository/src/swarms.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ impl Swarms {
467467
pub fn is_empty(&self) -> bool {
468468
self.swarms.is_empty()
469469
}
470+
471+
pub fn contains(&self, key: &InfoHash) -> bool {
472+
self.swarms.contains_key(key)
473+
}
470474
}
471475

472476
#[derive(thiserror::Error, Debug, Clone)]

packages/tracker-core/src/announce_handler.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ use std::sync::Arc;
9696
use bittorrent_primitives::info_hash::InfoHash;
9797
use torrust_tracker_configuration::{Core, TORRENT_PEERS_LIMIT};
9898
use torrust_tracker_primitives::core::AnnounceData;
99-
use torrust_tracker_primitives::peer;
99+
use torrust_tracker_primitives::{peer, NumberOfDownloads};
100100

101101
use super::torrent::repository::in_memory::InMemoryTorrentRepository;
102+
use crate::databases;
102103
use crate::error::AnnounceError;
103104
use crate::statistics::persisted::downloads::DatabaseDownloadsMetricRepository;
104105
use crate::whitelist::authorization::WhitelistAuthorization;
@@ -163,21 +164,28 @@ impl AnnounceHandler {
163164
) -> Result<AnnounceData, AnnounceError> {
164165
self.whitelist_authorization.authorize(info_hash).await?;
165166

166-
let opt_persistent_torrent = if self.config.tracker_policy.persistent_torrent_completed_stat {
167-
self.db_downloads_metric_repository.load_torrent_downloads(info_hash)?
168-
} else {
169-
None
170-
};
171-
172167
peer.change_ip(&assign_ip_address_to_peer(remote_client_ip, self.config.net.external_ip));
173168

174169
self.in_memory_torrent_repository
175-
.handle_announcement(info_hash, peer, opt_persistent_torrent)
170+
.handle_announcement(info_hash, peer, self.load_downloads_metric_if_needed(info_hash)?)
176171
.await;
177172

178173
Ok(self.build_announce_data(info_hash, peer, peers_wanted).await)
179174
}
180175

176+
/// Loads the number of downloads for a torrent if needed.
177+
fn load_downloads_metric_if_needed(
178+
&self,
179+
info_hash: &InfoHash,
180+
) -> Result<Option<NumberOfDownloads>, databases::error::Error> {
181+
if self.config.tracker_policy.persistent_torrent_completed_stat && !self.in_memory_torrent_repository.contains(info_hash)
182+
{
183+
Ok(self.db_downloads_metric_repository.load_torrent_downloads(info_hash)?)
184+
} else {
185+
Ok(None)
186+
}
187+
}
188+
181189
/// Builds the announce data for the peer making the request.
182190
async fn build_announce_data(&self, info_hash: &InfoHash, peer: &peer::Peer, peers_wanted: &PeersWanted) -> AnnounceData {
183191
let peers = self

packages/tracker-core/src/torrent/repository/in_memory.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,10 @@ impl InMemoryTorrentRepository {
267267
pub fn import_persistent(&self, persistent_torrents: &NumberOfDownloadsBTreeMap) {
268268
self.swarms.import_persistent(persistent_torrents);
269269
}
270+
271+
/// Checks if the repository contains a torrent entry for the given infohash.
272+
#[must_use]
273+
pub fn contains(&self, info_hash: &InfoHash) -> bool {
274+
self.swarms.contains(info_hash)
275+
}
270276
}

0 commit comments

Comments
 (0)