Skip to content

Commit 6332261

Browse files
committed
refactor: [#1201] extract TorrentsManager
1 parent f4dcb51 commit 6332261

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

src/core/mod.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -451,20 +451,18 @@ pub mod peer_tests;
451451

452452
use std::net::IpAddr;
453453
use std::sync::Arc;
454-
use std::time::Duration;
455454

456455
use bittorrent_primitives::info_hash::InfoHash;
456+
use torrent::manager::TorrentsManager;
457457
use torrent::repository::in_memory::InMemoryTorrentRepository;
458458
use torrent::repository::persisted::DatabasePersistentTorrentRepository;
459-
use torrust_tracker_clock::clock::Time;
460459
use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT};
461460
use torrust_tracker_primitives::core::{AnnounceData, ScrapeData};
462461
use torrust_tracker_primitives::peer;
463462
use torrust_tracker_primitives::swarm_metadata::SwarmMetadata;
464463
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
465464

466465
use crate::core::databases::Database;
467-
use crate::CurrentClock;
468466

469467
/// The domain layer tracker service.
470468
///
@@ -491,6 +489,9 @@ pub struct Tracker {
491489

492490
/// The persistent torrents repository.
493491
db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
492+
493+
/// The service to run torrents tasks.
494+
torrents_manager: Arc<TorrentsManager>,
494495
}
495496

496497
/// How many peers the peer announcing wants in the announce response.
@@ -546,12 +547,20 @@ impl Tracker {
546547
database: &Arc<Box<dyn Database>>,
547548
whitelist_authorization: &Arc<whitelist::authorization::Authorization>,
548549
) -> Result<Tracker, databases::error::Error> {
550+
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
551+
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(database));
552+
549553
Ok(Tracker {
550554
config: config.clone(),
551555
database: database.clone(),
552556
whitelist_authorization: whitelist_authorization.clone(),
553-
in_memory_torrent_repository: Arc::new(InMemoryTorrentRepository::default()),
554-
db_torrent_repository: Arc::new(DatabasePersistentTorrentRepository::new(database)),
557+
in_memory_torrent_repository: in_memory_torrent_repository.clone(),
558+
db_torrent_repository: db_torrent_repository.clone(),
559+
torrents_manager: Arc::new(TorrentsManager::new(
560+
config,
561+
&in_memory_torrent_repository,
562+
&db_torrent_repository,
563+
)),
555564
})
556565
}
557566

@@ -670,11 +679,7 @@ impl Tracker {
670679
///
671680
/// Will return a `database::Error` if unable to load the list of `persistent_torrents` from the database.
672681
pub fn load_torrents_from_database(&self) -> Result<(), databases::error::Error> {
673-
let persistent_torrents = self.db_torrent_repository.load_all()?;
674-
675-
self.in_memory_torrent_repository.import_persistent(&persistent_torrents);
676-
677-
Ok(())
682+
self.torrents_manager.load_torrents_from_database()
678683
}
679684

680685
/// # Context: Tracker
@@ -748,15 +753,7 @@ impl Tracker {
748753
///
749754
/// # Context: Tracker
750755
pub fn cleanup_torrents(&self) {
751-
let current_cutoff = CurrentClock::now_sub(&Duration::from_secs(u64::from(self.config.tracker_policy.max_peer_timeout)))
752-
.unwrap_or_default();
753-
754-
self.in_memory_torrent_repository.remove_inactive_peers(current_cutoff);
755-
756-
if self.config.tracker_policy.remove_peerless_torrents {
757-
self.in_memory_torrent_repository
758-
.remove_peerless_torrents(&self.config.tracker_policy);
759-
}
756+
self.torrents_manager.cleanup_torrents();
760757
}
761758

762759
/// It drops the database tables.

src/core/torrent/manager.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::sync::Arc;
2+
use std::time::Duration;
3+
4+
use torrust_tracker_clock::clock::Time;
5+
use torrust_tracker_configuration::Core;
6+
7+
use super::repository::in_memory::InMemoryTorrentRepository;
8+
use super::repository::persisted::DatabasePersistentTorrentRepository;
9+
use crate::core::databases;
10+
use crate::CurrentClock;
11+
12+
pub struct TorrentsManager {
13+
/// The tracker configuration.
14+
config: Core,
15+
16+
/// The in-memory torrents repository.
17+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
18+
19+
/// The persistent torrents repository.
20+
db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
21+
}
22+
23+
impl TorrentsManager {
24+
#[must_use]
25+
pub fn new(
26+
config: &Core,
27+
in_memory_torrent_repository: &Arc<InMemoryTorrentRepository>,
28+
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
29+
) -> Self {
30+
Self {
31+
config: config.clone(),
32+
in_memory_torrent_repository: in_memory_torrent_repository.clone(),
33+
db_torrent_repository: db_torrent_repository.clone(),
34+
}
35+
}
36+
37+
/// It loads the torrents from database into memory. It only loads the
38+
/// torrent entry list with the number of seeders for each torrent. Peers
39+
/// data is not persisted.
40+
///
41+
/// # Errors
42+
///
43+
/// Will return a `database::Error` if unable to load the list of `persistent_torrents` from the database.
44+
pub fn load_torrents_from_database(&self) -> Result<(), databases::error::Error> {
45+
let persistent_torrents = self.db_torrent_repository.load_all()?;
46+
47+
self.in_memory_torrent_repository.import_persistent(&persistent_torrents);
48+
49+
Ok(())
50+
}
51+
52+
/// Remove inactive peers and (optionally) peerless torrents.
53+
pub fn cleanup_torrents(&self) {
54+
let current_cutoff = CurrentClock::now_sub(&Duration::from_secs(u64::from(self.config.tracker_policy.max_peer_timeout)))
55+
.unwrap_or_default();
56+
57+
self.in_memory_torrent_repository.remove_inactive_peers(current_cutoff);
58+
59+
if self.config.tracker_policy.remove_peerless_torrents {
60+
self.in_memory_torrent_repository
61+
.remove_peerless_torrents(&self.config.tracker_policy);
62+
}
63+
}
64+
}

src/core/torrent/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//! - The number of peers that have NOT completed downloading the torrent and are still active, that means they are actively participating in the network.
2626
//! Peer that don not have a full copy of the torrent data are called "leechers".
2727
//!
28+
pub mod manager;
2829
pub mod repository;
2930

3031
use torrust_tracker_torrent_repository::TorrentsSkipMapMutexStd;

0 commit comments

Comments
 (0)