Skip to content

Commit 612f729

Browse files
committed
refactor: [#1201] remove tracker dependency on TorrentsManager
1 parent 3a2e8f0 commit 612f729

File tree

13 files changed

+41
-64
lines changed

13 files changed

+41
-64
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/bootstrap/app.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
119119
&whitelist_authorization,
120120
&in_memory_torrent_repository,
121121
&db_torrent_repository,
122-
&torrents_manager,
123122
));
124123

125124
AppContainer {
@@ -132,6 +131,9 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
132131
stats_event_sender,
133132
stats_repository,
134133
whitelist_manager,
134+
in_memory_torrent_repository,
135+
db_torrent_repository,
136+
torrents_manager,
135137
}
136138
}
137139

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

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use crate::core::authentication::service::AuthenticationService;
77
use crate::core::databases::Database;
88
use crate::core::statistics::event::sender::Sender;
99
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;
1013
use crate::core::whitelist::manager::WhiteListManager;
1114
use crate::core::{whitelist, Tracker};
1215
use crate::servers::udp::server::banning::BanService;
@@ -21,4 +24,7 @@ pub struct AppContainer {
2124
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
2225
pub stats_repository: Arc<Repository>,
2326
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>,
2430
}

src/core/mod.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ use std::net::IpAddr;
453453
use std::sync::Arc;
454454

455455
use bittorrent_primitives::info_hash::InfoHash;
456-
use torrent::manager::TorrentsManager;
457456
use torrent::repository::in_memory::InMemoryTorrentRepository;
458457
use torrent::repository::persisted::DatabasePersistentTorrentRepository;
459458
use torrust_tracker_configuration::{AnnouncePolicy, Core, TORRENT_PEERS_LIMIT};
@@ -483,9 +482,6 @@ pub struct Tracker {
483482

484483
/// The persistent torrents repository.
485484
db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
486-
487-
/// The service to run torrents tasks.
488-
torrents_manager: Arc<TorrentsManager>,
489485
}
490486

491487
/// How many peers the peer announcing wants in the announce response.
@@ -541,14 +537,12 @@ impl Tracker {
541537
whitelist_authorization: &Arc<whitelist::authorization::Authorization>,
542538
in_memory_torrent_repository: &Arc<InMemoryTorrentRepository>,
543539
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
544-
torrents_manager: &Arc<TorrentsManager>,
545540
) -> Result<Tracker, databases::error::Error> {
546541
Ok(Tracker {
547542
config: config.clone(),
548543
whitelist_authorization: whitelist_authorization.clone(),
549544
in_memory_torrent_repository: in_memory_torrent_repository.clone(),
550545
db_torrent_repository: db_torrent_repository.clone(),
551-
torrents_manager: torrents_manager.clone(),
552546
})
553547
}
554548

@@ -724,13 +718,6 @@ impl Tracker {
724718
pub fn get_torrents_metrics(&self) -> TorrentsMetrics {
725719
self.in_memory_torrent_repository.get_torrents_metrics()
726720
}
727-
728-
/// Remove inactive peers and (optionally) peerless torrents.
729-
///
730-
/// # Context: Tracker
731-
pub fn cleanup_torrents(&self) {
732-
self.torrents_manager.cleanup_torrents();
733-
}
734721
}
735722

736723
#[must_use]
@@ -761,6 +748,7 @@ mod tests {
761748
use crate::app_test::initialize_tracker_dependencies;
762749
use crate::core::peer::Peer;
763750
use crate::core::services::{initialize_tracker, initialize_whitelist_manager};
751+
use crate::core::torrent::manager::TorrentsManager;
764752
use crate::core::whitelist::manager::WhiteListManager;
765753
use crate::core::{whitelist, TorrentsMetrics, Tracker};
766754

@@ -774,15 +762,14 @@ mod tests {
774762
_authentication_service,
775763
in_memory_torrent_repository,
776764
db_torrent_repository,
777-
torrents_manager,
765+
_torrents_manager,
778766
) = initialize_tracker_dependencies(&config);
779767

780768
initialize_tracker(
781769
&config,
782770
&whitelist_authorization,
783771
&in_memory_torrent_repository,
784772
&db_torrent_repository,
785-
&torrents_manager,
786773
)
787774
}
788775

@@ -796,7 +783,7 @@ mod tests {
796783
_authentication_service,
797784
in_memory_torrent_repository,
798785
db_torrent_repository,
799-
torrents_manager,
786+
_torrents_manager,
800787
) = initialize_tracker_dependencies(&config);
801788

802789
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
@@ -806,13 +793,12 @@ mod tests {
806793
&whitelist_authorization,
807794
&in_memory_torrent_repository,
808795
&db_torrent_repository,
809-
&torrents_manager,
810796
);
811797

812798
(tracker, whitelist_authorization, whitelist_manager)
813799
}
814800

815-
pub fn tracker_persisting_torrents_in_database() -> Tracker {
801+
pub fn tracker_persisting_torrents_in_database() -> (Tracker, Arc<TorrentsManager>) {
816802
let mut config = configuration::ephemeral_listed();
817803
config.core.tracker_policy.persistent_torrent_completed_stat = true;
818804

@@ -826,13 +812,14 @@ mod tests {
826812
torrents_manager,
827813
) = initialize_tracker_dependencies(&config);
828814

829-
initialize_tracker(
815+
let tracker = initialize_tracker(
830816
&config,
831817
&whitelist_authorization,
832818
&in_memory_torrent_repository,
833819
&db_torrent_repository,
834-
&torrents_manager,
835-
)
820+
);
821+
822+
(tracker, torrents_manager)
836823
}
837824

838825
fn sample_info_hash() -> InfoHash {
@@ -1492,7 +1479,7 @@ mod tests {
14921479

14931480
#[tokio::test]
14941481
async fn it_should_persist_the_number_of_completed_peers_for_all_torrents_into_the_database() {
1495-
let tracker = tracker_persisting_torrents_in_database();
1482+
let (tracker, torrents_manager) = tracker_persisting_torrents_in_database();
14961483

14971484
let info_hash = sample_info_hash();
14981485

@@ -1509,7 +1496,7 @@ mod tests {
15091496
// Remove the newly updated torrent from memory
15101497
let _unused = tracker.in_memory_torrent_repository.remove(&info_hash);
15111498

1512-
tracker.torrents_manager.load_torrents_from_database().unwrap();
1499+
torrents_manager.load_torrents_from_database().unwrap();
15131500

15141501
let torrent_entry = tracker
15151502
.in_memory_torrent_repository

src/core/services/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ 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;
1817
use super::torrent::repository::in_memory::InMemoryTorrentRepository;
1918
use super::torrent::repository::persisted::DatabasePersistentTorrentRepository;
2019
use super::whitelist;
@@ -34,14 +33,12 @@ pub fn initialize_tracker(
3433
whitelist_authorization: &Arc<whitelist::authorization::Authorization>,
3534
in_memory_torrent_repository: &Arc<InMemoryTorrentRepository>,
3635
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
37-
torrents_manager: &Arc<TorrentsManager>,
3836
) -> Tracker {
3937
match Tracker::new(
4038
&Arc::new(config).core,
4139
whitelist_authorization,
4240
in_memory_torrent_repository,
4341
db_torrent_repository,
44-
torrents_manager,
4542
) {
4643
Ok(tracker) => tracker,
4744
Err(error) => {

src/core/services/statistics/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ mod tests {
139139
_authentication_service,
140140
in_memory_torrent_repository,
141141
db_torrent_repository,
142-
torrents_manager,
142+
_torrents_manager,
143143
) = initialize_tracker_dependencies(&config);
144144

145145
let (_stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
@@ -150,7 +150,6 @@ mod tests {
150150
&whitelist_authorization,
151151
&in_memory_torrent_repository,
152152
&db_torrent_repository,
153-
&torrents_manager,
154153
));
155154

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

src/core/services/torrent.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,14 @@ mod tests {
125125
_authentication_service,
126126
in_memory_torrent_repository,
127127
db_torrent_repository,
128-
torrents_manager,
128+
_torrents_manager,
129129
) = initialize_tracker_dependencies(config);
130130

131131
Arc::new(initialize_tracker(
132132
config,
133133
&whitelist_authorization,
134134
&in_memory_torrent_repository,
135135
&db_torrent_repository,
136-
&torrents_manager,
137136
))
138137
}
139138

@@ -178,15 +177,14 @@ mod tests {
178177
_authentication_service,
179178
in_memory_torrent_repository,
180179
db_torrent_repository,
181-
torrents_manager,
180+
_torrents_manager,
182181
) = initialize_tracker_dependencies(&config);
183182

184183
let tracker = initialize_tracker(
185184
&config,
186185
&whitelist_authorization,
187186
&in_memory_torrent_repository,
188187
&db_torrent_repository,
189-
&torrents_manager,
190188
);
191189

192190
let tracker = Arc::new(tracker);

src/servers/http/v1/handlers/announce.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ mod tests {
281281
authentication_service,
282282
in_memory_torrent_repository,
283283
db_torrent_repository,
284-
torrents_manager,
284+
_torrents_manager,
285285
) = initialize_tracker_dependencies(config);
286286

287287
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
@@ -292,7 +292,6 @@ mod tests {
292292
&whitelist_authorization,
293293
&in_memory_torrent_repository,
294294
&db_torrent_repository,
295-
&torrents_manager,
296295
));
297296

298297
(tracker, stats_event_sender, whitelist_authorization, authentication_service)

src/servers/http/v1/handlers/scrape.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ mod tests {
158158
authentication_service,
159159
in_memory_torrent_repository,
160160
db_torrent_repository,
161-
torrents_manager,
161+
_torrents_manager,
162162
) = initialize_tracker_dependencies(&config);
163163

164164
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
@@ -169,7 +169,6 @@ mod tests {
169169
&whitelist_authorization,
170170
&in_memory_torrent_repository,
171171
&db_torrent_repository,
172-
&torrents_manager,
173172
),
174173
stats_event_sender,
175174
authentication_service,
@@ -190,7 +189,7 @@ mod tests {
190189
authentication_service,
191190
in_memory_torrent_repository,
192191
db_torrent_repository,
193-
torrents_manager,
192+
_torrents_manager,
194193
) = initialize_tracker_dependencies(&config);
195194

196195
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
@@ -201,7 +200,6 @@ mod tests {
201200
&whitelist_authorization,
202201
&in_memory_torrent_repository,
203202
&db_torrent_repository,
204-
&torrents_manager,
205203
),
206204
stats_event_sender,
207205
authentication_service,
@@ -222,7 +220,7 @@ mod tests {
222220
authentication_service,
223221
in_memory_torrent_repository,
224222
db_torrent_repository,
225-
torrents_manager,
223+
_torrents_manager,
226224
) = initialize_tracker_dependencies(&config);
227225

228226
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
@@ -233,7 +231,6 @@ mod tests {
233231
&whitelist_authorization,
234232
&in_memory_torrent_repository,
235233
&db_torrent_repository,
236-
&torrents_manager,
237234
),
238235
stats_event_sender,
239236
authentication_service,
@@ -254,7 +251,7 @@ mod tests {
254251
authentication_service,
255252
in_memory_torrent_repository,
256253
db_torrent_repository,
257-
torrents_manager,
254+
_torrents_manager,
258255
) = initialize_tracker_dependencies(&config);
259256

260257
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
@@ -265,7 +262,6 @@ mod tests {
265262
&whitelist_authorization,
266263
&in_memory_torrent_repository,
267264
&db_torrent_repository,
268-
&torrents_manager,
269265
),
270266
stats_event_sender,
271267
authentication_service,

0 commit comments

Comments
 (0)