Skip to content

Commit 4e3dbae

Browse files
committed
refactor: [torrust#1201] inject new extracted sercvies in core tracker
1 parent 6332261 commit 4e3dbae

File tree

11 files changed

+471
-93
lines changed

11 files changed

+471
-93
lines changed

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

+18-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,8 +106,22 @@ 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+
&database,
120+
&whitelist_authorization,
121+
&in_memory_torrent_repository,
122+
&db_torrent_repository,
123+
&torrents_manager,
124+
));
108125

109126
AppContainer {
110127
tracker,

src/core/mod.rs

+57-19
Original file line numberDiff line numberDiff line change
@@ -546,21 +546,17 @@ impl Tracker {
546546
config: &Core,
547547
database: &Arc<Box<dyn Database>>,
548548
whitelist_authorization: &Arc<whitelist::authorization::Authorization>,
549+
in_memory_torrent_repository: &Arc<InMemoryTorrentRepository>,
550+
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
551+
torrents_manager: &Arc<TorrentsManager>,
549552
) -> 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-
553553
Ok(Tracker {
554554
config: config.clone(),
555555
database: database.clone(),
556556
whitelist_authorization: whitelist_authorization.clone(),
557557
in_memory_torrent_repository: in_memory_torrent_repository.clone(),
558558
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-
)),
559+
torrents_manager: torrents_manager.clone(),
564560
})
565561
}
566562

@@ -802,21 +798,49 @@ mod tests {
802798
fn public_tracker() -> Tracker {
803799
let config = configuration::ephemeral_public();
804800

805-
let (database, _in_memory_whitelist, whitelist_authorization, _authentication_service) =
806-
initialize_tracker_dependencies(&config);
807-
808-
initialize_tracker(&config, &database, &whitelist_authorization)
801+
let (
802+
database,
803+
_in_memory_whitelist,
804+
whitelist_authorization,
805+
_authentication_service,
806+
in_memory_torrent_repository,
807+
db_torrent_repository,
808+
torrents_manager,
809+
) = initialize_tracker_dependencies(&config);
810+
811+
initialize_tracker(
812+
&config,
813+
&database,
814+
&whitelist_authorization,
815+
&in_memory_torrent_repository,
816+
&db_torrent_repository,
817+
&torrents_manager,
818+
)
809819
}
810820

811821
fn whitelisted_tracker() -> (Tracker, Arc<whitelist::authorization::Authorization>, Arc<WhiteListManager>) {
812822
let config = configuration::ephemeral_listed();
813823

814-
let (database, in_memory_whitelist, whitelist_authorization, _authentication_service) =
815-
initialize_tracker_dependencies(&config);
824+
let (
825+
database,
826+
in_memory_whitelist,
827+
whitelist_authorization,
828+
_authentication_service,
829+
in_memory_torrent_repository,
830+
db_torrent_repository,
831+
torrents_manager,
832+
) = initialize_tracker_dependencies(&config);
816833

817834
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
818835

819-
let tracker = initialize_tracker(&config, &database, &whitelist_authorization);
836+
let tracker = initialize_tracker(
837+
&config,
838+
&database,
839+
&whitelist_authorization,
840+
&in_memory_torrent_repository,
841+
&db_torrent_repository,
842+
&torrents_manager,
843+
);
820844

821845
(tracker, whitelist_authorization, whitelist_manager)
822846
}
@@ -825,10 +849,24 @@ mod tests {
825849
let mut config = configuration::ephemeral_listed();
826850
config.core.tracker_policy.persistent_torrent_completed_stat = true;
827851

828-
let (database, _in_memory_whitelist, whitelist_authorization, _authentication_service) =
829-
initialize_tracker_dependencies(&config);
830-
831-
initialize_tracker(&config, &database, &whitelist_authorization)
852+
let (
853+
database,
854+
_in_memory_whitelist,
855+
whitelist_authorization,
856+
_authentication_service,
857+
in_memory_torrent_repository,
858+
db_torrent_repository,
859+
torrents_manager,
860+
) = initialize_tracker_dependencies(&config);
861+
862+
initialize_tracker(
863+
&config,
864+
&database,
865+
&whitelist_authorization,
866+
&in_memory_torrent_repository,
867+
&db_torrent_repository,
868+
&torrents_manager,
869+
)
832870
}
833871

834872
fn sample_info_hash() -> InfoHash {

src/core/services/mod.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use torrust_tracker_configuration::v2_0_0::database;
1414
use torrust_tracker_configuration::Configuration;
1515

1616
use super::databases::{self, Database};
17+
use super::torrent::manager::TorrentsManager;
18+
use super::torrent::repository::in_memory::InMemoryTorrentRepository;
19+
use super::torrent::repository::persisted::DatabasePersistentTorrentRepository;
1720
use super::whitelist;
1821
use super::whitelist::manager::WhiteListManager;
1922
use super::whitelist::repository::in_memory::InMemoryWhitelist;
@@ -30,8 +33,18 @@ pub fn initialize_tracker(
3033
config: &Configuration,
3134
database: &Arc<Box<dyn Database>>,
3235
whitelist_authorization: &Arc<whitelist::authorization::Authorization>,
36+
in_memory_torrent_repository: &Arc<InMemoryTorrentRepository>,
37+
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
38+
torrents_manager: &Arc<TorrentsManager>,
3339
) -> Tracker {
34-
match Tracker::new(&Arc::new(config).core, database, whitelist_authorization) {
40+
match Tracker::new(
41+
&Arc::new(config).core,
42+
database,
43+
whitelist_authorization,
44+
in_memory_torrent_repository,
45+
db_torrent_repository,
46+
torrents_manager,
47+
) {
3548
Ok(tracker) => tracker,
3649
Err(error) => {
3750
panic!("{}", error)

src/core/services/statistics/mod.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,27 @@ mod tests {
132132
async fn the_statistics_service_should_return_the_tracker_metrics() {
133133
let config = tracker_configuration();
134134

135-
let (database, _in_memory_whitelist, whitelist_authorization, _authentication_service) =
136-
initialize_tracker_dependencies(&config);
135+
let (
136+
database,
137+
_in_memory_whitelist,
138+
whitelist_authorization,
139+
_authentication_service,
140+
in_memory_torrent_repository,
141+
db_torrent_repository,
142+
torrents_manager,
143+
) = initialize_tracker_dependencies(&config);
144+
137145
let (_stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
138146
let stats_repository = Arc::new(stats_repository);
139147

140-
let tracker = Arc::new(initialize_tracker(&config, &database, &whitelist_authorization));
148+
let tracker = Arc::new(initialize_tracker(
149+
&config,
150+
&database,
151+
&whitelist_authorization,
152+
&in_memory_torrent_repository,
153+
&db_torrent_repository,
154+
&torrents_manager,
155+
));
141156

142157
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));
143158

0 commit comments

Comments
 (0)