Skip to content

Commit e90585a

Browse files
committed
refactor: [torrust#1524] move total downloads udpate from announce command to event handler
1 parent 8968757 commit e90585a

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

packages/tracker-core/src/announce_handler.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ impl AnnounceHandler {
163163
) -> Result<AnnounceData, AnnounceError> {
164164
self.whitelist_authorization.authorize(info_hash).await?;
165165

166+
// This will be removed in the future.
167+
// See https://github.com/torrust/torrust-tracker/issues/1502
168+
// There will be a persisted metric for counting the total number of
169+
// downloads across all torrents. The in-memory metric will count only
170+
// the number of downloads during the current tracker uptime.
166171
let opt_persistent_torrent = if self.config.tracker_policy.persistent_torrent_completed_stat {
167172
self.db_torrent_repository.load(info_hash)?
168173
} else {
@@ -171,15 +176,11 @@ impl AnnounceHandler {
171176

172177
peer.change_ip(&assign_ip_address_to_peer(remote_client_ip, self.config.net.external_ip));
173178

174-
let number_of_downloads_increased = self
179+
let _number_of_downloads_increased = self
175180
.in_memory_torrent_repository
176181
.upsert_peer(info_hash, peer, opt_persistent_torrent)
177182
.await;
178183

179-
if self.config.tracker_policy.persistent_torrent_completed_stat && number_of_downloads_increased {
180-
self.db_torrent_repository.increase_number_of_downloads(info_hash)?;
181-
}
182-
183184
Ok(self.build_announce_data(info_hash, peer, peers_wanted).await)
184185
}
185186

packages/tracker-core/src/statistics/event/handler.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
use std::sync::Arc;
2+
13
use torrust_tracker_primitives::DurationSinceUnixEpoch;
24
use torrust_tracker_torrent_repository::event::Event;
35

4-
pub async fn handle_event(event: Event, _now: DurationSinceUnixEpoch) {
6+
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;
7+
8+
pub async fn handle_event(
9+
event: Event,
10+
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
11+
_now: DurationSinceUnixEpoch,
12+
) {
513
match event {
614
// Torrent events
715
Event::TorrentAdded { info_hash, .. } => {
@@ -27,6 +35,15 @@ pub async fn handle_event(event: Event, _now: DurationSinceUnixEpoch) {
2735
}
2836
Event::PeerDownloadCompleted { info_hash, peer } => {
2937
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer download completed", );
38+
39+
match db_torrent_repository.increase_number_of_downloads(&info_hash) {
40+
Ok(()) => {
41+
tracing::debug!(info_hash = ?info_hash, "Number of downloads increased");
42+
}
43+
Err(err) => {
44+
tracing::error!(info_hash = ?info_hash, error = ?err, "Failed to increase number of downloads");
45+
}
46+
}
3047
}
3148
}
3249
}

packages/tracker-core/src/statistics/event/listener.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1+
use std::sync::Arc;
2+
13
use tokio::task::JoinHandle;
24
use torrust_tracker_clock::clock::Time;
35
use torrust_tracker_events::receiver::RecvError;
46
use torrust_tracker_torrent_repository::event::receiver::Receiver;
57

68
use super::handler::handle_event;
9+
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;
710
use crate::{CurrentClock, TRACKER_CORE_LOG_TARGET};
811

912
#[must_use]
10-
pub fn run_event_listener(receiver: Receiver) -> JoinHandle<()> {
13+
pub fn run_event_listener(
14+
receiver: Receiver,
15+
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
16+
) -> JoinHandle<()> {
17+
let db_torrent_repository: Arc<DatabasePersistentTorrentRepository> = db_torrent_repository.clone();
18+
1119
tracing::info!(target: TRACKER_CORE_LOG_TARGET, "Starting torrent repository event listener");
1220

1321
tokio::spawn(async move {
14-
dispatch_events(receiver).await;
22+
dispatch_events(receiver, db_torrent_repository).await;
1523

1624
tracing::info!(target: TRACKER_CORE_LOG_TARGET, "Torrent repository listener finished");
1725
})
1826
}
1927

20-
async fn dispatch_events(mut receiver: Receiver) {
28+
async fn dispatch_events(mut receiver: Receiver, db_torrent_repository: Arc<DatabasePersistentTorrentRepository>) {
2129
let shutdown_signal = tokio::signal::ctrl_c();
2230

2331
tokio::pin!(shutdown_signal);
@@ -33,7 +41,7 @@ async fn dispatch_events(mut receiver: Receiver) {
3341

3442
result = receiver.recv() => {
3543
match result {
36-
Ok(event) => handle_event(event, CurrentClock::now()).await,
44+
Ok(event) => handle_event(event, &db_torrent_repository, CurrentClock::now()).await,
3745
Err(e) => {
3846
match e {
3947
RecvError::Closed => {

src/bootstrap/jobs/tracker_core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn start_event_listener(config: &Configuration, app_container: &Arc<AppConta
1111
if config.core.tracker_policy.persistent_torrent_completed_stat {
1212
let job = bittorrent_tracker_core::statistics::event::listener::run_event_listener(
1313
app_container.torrent_repository_container.event_bus.receiver(),
14+
&app_container.tracker_core_container.db_torrent_repository,
1415
);
1516

1617
Some(job)

0 commit comments

Comments
 (0)