Skip to content

Commit a4277a7

Browse files
committed
Merge torrust#1202: Overhaul core Tracker: extract torrents context (part 1)
612f729 refactor: [torrust#1201] remove tracker dependency on TorrentsManager (Jose Celano) 3a2e8f0 refactor: [torrust#1201] reorganize methods in tracker (Jose Celano) b3fcdb4 refactor: [torrust#1201] remove direct dependency on database from tracker (Jose Celano) bdc3f22 refactor: [torrust#1201] add database to app container y environments (Jose Celano) a4a2d67 refactor: [torrust#1201] remove pub fn from tracker (Jose Celano) 6fb632e refactor: [torrust#1201] remove duplicate code (Jose Celano) 4e3dbae refactor: [torrust#1201] inject new extracted sercvies in core tracker (Jose Celano) 6332261 refactor: [torrust#1201] extract TorrentsManager (Jose Celano) f4dcb51 refactor: [torrust#1201] rename tracker field (Jose Celano) 9b5f776 refactor: [torrust#1201] exatrct DatabasePersistentTorrentRepository (Jose Celano) 03ef7f6 refactor: [1201] extract InMemoryTorrentRepository (Jose Celano) Pull request description: Overhaul core Tracker: extract torrents context. This PR is part of the implementation of the issue: torrust#1201. The core Tracker is getting smaller and smaller. This PR reduces the dependencies to: ```rust pub struct Tracker { /// The tracker configuration. config: Core, /// The service to check is a torrent is whitelisted. pub whitelist_authorization: Arc<whitelist::authorization::Authorization>, /// The in-memory torrents repository. in_memory_torrent_repository: Arc<InMemoryTorrentRepository>, /// The persistent torrents repository. db_torrent_repository: Arc<DatabasePersistentTorrentRepository>, } ``` I will open a new PR (part 2) to continue extracting the rest of methods that are not used directly in a `announce` or `scrape` requests. ACKs for top commit: josecelano: ACK 612f729 Tree-SHA512: 3ac17b515e9c521c128cd3694724d9f5294a1ea8a0b123959953d48d20556efec1de8e105c297bf1e75d1164b91fe4805c970a223572edc8ece9de2d020ce7d6
2 parents 444987b + 612f729 commit a4277a7

File tree

25 files changed

+672
-212
lines changed

25 files changed

+672
-212
lines changed

src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
137137

138138
// Start runners to remove torrents without peers, every interval
139139
if config.core.inactive_peer_cleanup_interval > 0 {
140-
jobs.push(torrent_cleanup::start_job(&config.core, &app_container.tracker));
140+
jobs.push(torrent_cleanup::start_job(&config.core, &app_container.torrents_manager));
141141
}
142142

143143
// Start Health Check API

src/app_test.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use crate::core::authentication::key::repository::persisted::DatabaseKeyReposito
99
use crate::core::authentication::service::{self, AuthenticationService};
1010
use crate::core::databases::Database;
1111
use crate::core::services::initialize_database;
12+
use crate::core::torrent::manager::TorrentsManager;
13+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
14+
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
1215
use crate::core::whitelist;
1316
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
1417

@@ -22,6 +25,9 @@ pub fn initialize_tracker_dependencies(
2225
Arc<InMemoryWhitelist>,
2326
Arc<whitelist::authorization::Authorization>,
2427
Arc<AuthenticationService>,
28+
Arc<InMemoryTorrentRepository>,
29+
Arc<DatabasePersistentTorrentRepository>,
30+
Arc<TorrentsManager>,
2531
) {
2632
let database = initialize_database(config);
2733
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
@@ -36,6 +42,21 @@ pub fn initialize_tracker_dependencies(
3642
&db_key_repository.clone(),
3743
&in_memory_key_repository.clone(),
3844
));
45+
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
46+
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
47+
let torrents_manager = Arc::new(TorrentsManager::new(
48+
&config.core,
49+
&in_memory_torrent_repository,
50+
&db_torrent_repository,
51+
));
3952

40-
(database, in_memory_whitelist, whitelist_authorization, authentication_service)
53+
(
54+
database,
55+
in_memory_whitelist,
56+
whitelist_authorization,
57+
authentication_service,
58+
in_memory_torrent_repository,
59+
db_torrent_repository,
60+
torrents_manager,
61+
)
4162
}

src/bootstrap/app.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ use crate::core::authentication::key::repository::in_memory::InMemoryKeyReposito
2727
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
2828
use crate::core::authentication::service;
2929
use crate::core::services::{initialize_database, initialize_tracker, initialize_whitelist_manager, statistics};
30+
use crate::core::torrent::manager::TorrentsManager;
31+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
32+
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
3033
use crate::core::whitelist;
3134
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
3235
use crate::servers::udp::server::banning::BanService;
@@ -103,10 +106,23 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
103106
&db_key_repository.clone(),
104107
&in_memory_key_repository.clone(),
105108
));
109+
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
110+
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
111+
let torrents_manager = Arc::new(TorrentsManager::new(
112+
&configuration.core,
113+
&in_memory_torrent_repository,
114+
&db_torrent_repository,
115+
));
106116

107-
let tracker = Arc::new(initialize_tracker(configuration, &database, &whitelist_authorization));
117+
let tracker = Arc::new(initialize_tracker(
118+
configuration,
119+
&whitelist_authorization,
120+
&in_memory_torrent_repository,
121+
&db_torrent_repository,
122+
));
108123

109124
AppContainer {
125+
database,
110126
tracker,
111127
keys_handler,
112128
authentication_service,
@@ -115,6 +131,9 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
115131
stats_event_sender,
116132
stats_repository,
117133
whitelist_manager,
134+
in_memory_torrent_repository,
135+
db_torrent_repository,
136+
torrents_manager,
118137
}
119138
}
120139

src/bootstrap/jobs/torrent_cleanup.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ use tokio::task::JoinHandle;
1717
use torrust_tracker_configuration::Core;
1818
use tracing::instrument;
1919

20-
use crate::core;
20+
use crate::core::torrent::manager::TorrentsManager;
2121

2222
/// It starts a jobs for cleaning up the torrent data in the tracker.
2323
///
2424
/// The cleaning task is executed on an `inactive_peer_cleanup_interval`.
2525
///
2626
/// Refer to [`torrust-tracker-configuration documentation`](https://docs.rs/torrust-tracker-configuration) for more info about that option.
2727
#[must_use]
28-
#[instrument(skip(config, tracker))]
29-
pub fn start_job(config: &Core, tracker: &Arc<core::Tracker>) -> JoinHandle<()> {
30-
let weak_tracker = std::sync::Arc::downgrade(tracker);
28+
#[instrument(skip(config, torrents_manager))]
29+
pub fn start_job(config: &Core, torrents_manager: &Arc<TorrentsManager>) -> JoinHandle<()> {
30+
let weak_torrents_manager = std::sync::Arc::downgrade(torrents_manager);
3131
let interval = config.inactive_peer_cleanup_interval;
3232

3333
tokio::spawn(async move {
@@ -42,10 +42,10 @@ pub fn start_job(config: &Core, tracker: &Arc<core::Tracker>) -> JoinHandle<()>
4242
break;
4343
}
4444
_ = interval.tick() => {
45-
if let Some(tracker) = weak_tracker.upgrade() {
45+
if let Some(torrents_manager) = weak_torrents_manager.upgrade() {
4646
let start_time = Utc::now().time();
4747
tracing::info!("Cleaning up torrents..");
48-
tracker.cleanup_torrents();
48+
torrents_manager.cleanup_torrents();
4949
tracing::info!("Cleaned up torrents in: {}ms", (Utc::now().time() - start_time).num_milliseconds());
5050
} else {
5151
break;

src/container.rs

+8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ use tokio::sync::RwLock;
44

55
use crate::core::authentication::handler::KeysHandler;
66
use crate::core::authentication::service::AuthenticationService;
7+
use crate::core::databases::Database;
78
use crate::core::statistics::event::sender::Sender;
89
use crate::core::statistics::repository::Repository;
10+
use crate::core::torrent::manager::TorrentsManager;
11+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
12+
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
913
use crate::core::whitelist::manager::WhiteListManager;
1014
use crate::core::{whitelist, Tracker};
1115
use crate::servers::udp::server::banning::BanService;
1216

1317
pub struct AppContainer {
18+
pub database: Arc<Box<dyn Database>>,
1419
pub tracker: Arc<Tracker>,
1520
pub keys_handler: Arc<KeysHandler>,
1621
pub authentication_service: Arc<AuthenticationService>,
@@ -19,4 +24,7 @@ pub struct AppContainer {
1924
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
2025
pub stats_repository: Arc<Repository>,
2126
pub whitelist_manager: Arc<WhiteListManager>,
27+
pub in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
28+
pub db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
29+
pub torrents_manager: Arc<TorrentsManager>,
2230
}

0 commit comments

Comments
 (0)