Skip to content

Commit 6ceb3fb

Browse files
committed
refactor: [#1203] use directly the InMemoryTorrentRepository
1 parent 0f1b2fb commit 6ceb3fb

File tree

12 files changed

+105
-97
lines changed

12 files changed

+105
-97
lines changed

src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
118118
if let Some(http_api_config) = &config.http_api {
119119
if let Some(job) = tracker_apis::start_job(
120120
http_api_config,
121-
app_container.tracker.clone(),
121+
app_container.in_memory_torrent_repository.clone(),
122122
app_container.keys_handler.clone(),
123123
app_container.whitelist_manager.clone(),
124124
app_container.ban_service.clone(),

src/bootstrap/jobs/tracker_apis.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use super::make_rust_tls;
3333
use crate::core::authentication::handler::KeysHandler;
3434
use crate::core::statistics::event::sender::Sender;
3535
use crate::core::statistics::repository::Repository;
36+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
3637
use crate::core::whitelist::manager::WhiteListManager;
37-
use crate::core::{self};
3838
use crate::servers::apis::server::{ApiServer, Launcher};
3939
use crate::servers::apis::Version;
4040
use crate::servers::registar::ServiceRegistrationForm;
@@ -63,7 +63,6 @@ pub struct ApiServerJobStarted();
6363
#[allow(clippy::too_many_arguments)]
6464
#[instrument(skip(
6565
config,
66-
tracker,
6766
keys_handler,
6867
whitelist_manager,
6968
ban_service,
@@ -73,7 +72,7 @@ pub struct ApiServerJobStarted();
7372
))]
7473
pub async fn start_job(
7574
config: &HttpApi,
76-
tracker: Arc<core::Tracker>,
75+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
7776
keys_handler: Arc<KeysHandler>,
7877
whitelist_manager: Arc<WhiteListManager>,
7978
ban_service: Arc<RwLock<BanService>>,
@@ -95,7 +94,7 @@ pub async fn start_job(
9594
start_v1(
9695
bind_to,
9796
tls,
98-
tracker.clone(),
97+
in_memory_torrent_repository.clone(),
9998
keys_handler.clone(),
10099
whitelist_manager.clone(),
101100
ban_service.clone(),
@@ -114,7 +113,6 @@ pub async fn start_job(
114113
#[instrument(skip(
115114
socket,
116115
tls,
117-
tracker,
118116
keys_handler,
119117
whitelist_manager,
120118
ban_service,
@@ -126,7 +124,7 @@ pub async fn start_job(
126124
async fn start_v1(
127125
socket: SocketAddr,
128126
tls: Option<RustlsConfig>,
129-
tracker: Arc<core::Tracker>,
127+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
130128
keys_handler: Arc<KeysHandler>,
131129
whitelist_manager: Arc<WhiteListManager>,
132130
ban_service: Arc<RwLock<BanService>>,
@@ -137,7 +135,7 @@ async fn start_v1(
137135
) -> JoinHandle<()> {
138136
let server = ApiServer::new(Launcher::new(socket, tls))
139137
.start(
140-
tracker,
138+
in_memory_torrent_repository,
141139
keys_handler,
142140
whitelist_manager,
143141
stats_event_sender,
@@ -179,7 +177,7 @@ mod tests {
179177

180178
start_job(
181179
config,
182-
app_container.tracker,
180+
app_container.in_memory_torrent_repository,
183181
app_container.keys_handler,
184182
app_container.whitelist_manager,
185183
app_container.ban_service,

src/core/services/statistics/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
4545

4646
use crate::core::statistics::metrics::Metrics;
4747
use crate::core::statistics::repository::Repository;
48-
use crate::core::Tracker;
48+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
4949
use crate::servers::udp::server::banning::BanService;
5050

5151
/// All the metrics collected by the tracker.
@@ -64,11 +64,11 @@ pub struct TrackerMetrics {
6464

6565
/// It returns all the [`TrackerMetrics`]
6666
pub async fn get_metrics(
67-
tracker: Arc<Tracker>,
67+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
6868
ban_service: Arc<RwLock<BanService>>,
6969
stats_repository: Arc<Repository>,
7070
) -> TrackerMetrics {
71-
let torrents_metrics = tracker.in_memory_torrent_repository.get_torrents_metrics();
71+
let torrents_metrics = in_memory_torrent_repository.get_torrents_metrics();
7272
let stats = stats_repository.get_stats().await;
7373
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
7474

@@ -145,7 +145,7 @@ mod tests {
145145
let (_stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
146146
let stats_repository = Arc::new(stats_repository);
147147

148-
let tracker = Arc::new(initialize_tracker(
148+
let _tracker = Arc::new(initialize_tracker(
149149
&config,
150150
&whitelist_authorization,
151151
&in_memory_torrent_repository,
@@ -154,7 +154,12 @@ mod tests {
154154

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

157-
let tracker_metrics = get_metrics(tracker.clone(), ban_service.clone(), stats_repository.clone()).await;
157+
let tracker_metrics = get_metrics(
158+
in_memory_torrent_repository.clone(),
159+
ban_service.clone(),
160+
stats_repository.clone(),
161+
)
162+
.await;
158163

159164
assert_eq!(
160165
tracker_metrics,

src/core/services/torrent.rs

+41-51
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use torrust_tracker_primitives::pagination::Pagination;
1111
use torrust_tracker_primitives::peer;
1212
use torrust_tracker_torrent_repository::entry::EntrySync;
1313

14-
use crate::core::Tracker;
14+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
1515

1616
/// It contains all the information the tracker has about a torrent
1717
#[derive(Debug, PartialEq)]
@@ -44,8 +44,11 @@ pub struct BasicInfo {
4444
}
4545

4646
/// It returns all the information the tracker has about one torrent in a [Info] struct.
47-
pub async fn get_torrent_info(tracker: Arc<Tracker>, info_hash: &InfoHash) -> Option<Info> {
48-
let torrent_entry_option = tracker.in_memory_torrent_repository.get(info_hash);
47+
pub async fn get_torrent_info(
48+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
49+
info_hash: &InfoHash,
50+
) -> Option<Info> {
51+
let torrent_entry_option = in_memory_torrent_repository.get(info_hash);
4952

5053
let torrent_entry = torrent_entry_option?;
5154

@@ -65,10 +68,13 @@ pub async fn get_torrent_info(tracker: Arc<Tracker>, info_hash: &InfoHash) -> Op
6568
}
6669

6770
/// It returns all the information the tracker has about multiple torrents in a [`BasicInfo`] struct, excluding the peer list.
68-
pub async fn get_torrents_page(tracker: Arc<Tracker>, pagination: Option<&Pagination>) -> Vec<BasicInfo> {
71+
pub async fn get_torrents_page(
72+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
73+
pagination: Option<&Pagination>,
74+
) -> Vec<BasicInfo> {
6975
let mut basic_infos: Vec<BasicInfo> = vec![];
7076

71-
for (info_hash, torrent_entry) in tracker.in_memory_torrent_repository.get_paginated(pagination) {
77+
for (info_hash, torrent_entry) in in_memory_torrent_repository.get_paginated(pagination) {
7278
let stats = torrent_entry.get_swarm_metadata();
7379

7480
basic_infos.push(BasicInfo {
@@ -83,15 +89,14 @@ pub async fn get_torrents_page(tracker: Arc<Tracker>, pagination: Option<&Pagina
8389
}
8490

8591
/// It returns all the information the tracker has about multiple torrents in a [`BasicInfo`] struct, excluding the peer list.
86-
pub async fn get_torrents(tracker: Arc<Tracker>, info_hashes: &[InfoHash]) -> Vec<BasicInfo> {
92+
pub async fn get_torrents(
93+
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
94+
info_hashes: &[InfoHash],
95+
) -> Vec<BasicInfo> {
8796
let mut basic_infos: Vec<BasicInfo> = vec![];
8897

8998
for info_hash in info_hashes {
90-
if let Some(stats) = tracker
91-
.in_memory_torrent_repository
92-
.get(info_hash)
93-
.map(|t| t.get_swarm_metadata())
94-
{
99+
if let Some(stats) = in_memory_torrent_repository.get(info_hash).map(|t| t.get_swarm_metadata()) {
95100
basic_infos.push(BasicInfo {
96101
info_hash: *info_hash,
97102
seeders: u64::from(stats.complete),
@@ -115,9 +120,10 @@ mod tests {
115120

116121
use crate::app_test::initialize_tracker_dependencies;
117122
use crate::core::services::initialize_tracker;
123+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
118124
use crate::core::Tracker;
119125

120-
fn initialize_tracker_and_deps(config: &Configuration) -> Arc<Tracker> {
126+
fn initialize_tracker_and_deps(config: &Configuration) -> (Arc<Tracker>, Arc<InMemoryTorrentRepository>) {
121127
let (
122128
_database,
123129
_in_memory_whitelist,
@@ -128,12 +134,14 @@ mod tests {
128134
_torrents_manager,
129135
) = initialize_tracker_dependencies(config);
130136

131-
Arc::new(initialize_tracker(
137+
let tracker = Arc::new(initialize_tracker(
132138
config,
133139
&whitelist_authorization,
134140
&in_memory_torrent_repository,
135141
&db_torrent_repository,
136-
))
142+
));
143+
144+
(tracker, in_memory_torrent_repository)
137145
}
138146

139147
fn sample_peer() -> peer::Peer {
@@ -157,40 +165,20 @@ mod tests {
157165
use torrust_tracker_configuration::Configuration;
158166
use torrust_tracker_test_helpers::configuration;
159167

160-
use crate::app_test::initialize_tracker_dependencies;
161-
use crate::core::services::initialize_tracker;
162168
use crate::core::services::torrent::tests::{initialize_tracker_and_deps, sample_peer};
163169
use crate::core::services::torrent::{get_torrent_info, Info};
170+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
164171

165172
pub fn tracker_configuration() -> Configuration {
166173
configuration::ephemeral()
167174
}
168175

169176
#[tokio::test]
170177
async fn should_return_none_if_the_tracker_does_not_have_the_torrent() {
171-
let config = tracker_configuration();
172-
173-
let (
174-
_database,
175-
_in_memory_whitelist,
176-
whitelist_authorization,
177-
_authentication_service,
178-
in_memory_torrent_repository,
179-
db_torrent_repository,
180-
_torrents_manager,
181-
) = initialize_tracker_dependencies(&config);
182-
183-
let tracker = initialize_tracker(
184-
&config,
185-
&whitelist_authorization,
186-
&in_memory_torrent_repository,
187-
&db_torrent_repository,
188-
);
189-
190-
let tracker = Arc::new(tracker);
178+
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
191179

192180
let torrent_info = get_torrent_info(
193-
tracker.clone(),
181+
in_memory_torrent_repository.clone(),
194182
&InfoHash::from_str("0b3aea4adc213ce32295be85d3883a63bca25446").unwrap(),
195183
)
196184
.await;
@@ -202,13 +190,15 @@ mod tests {
202190
async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() {
203191
let config = tracker_configuration();
204192

205-
let tracker = initialize_tracker_and_deps(&config);
193+
let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
206194

207195
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
208196
let info_hash = InfoHash::from_str(&hash).unwrap();
209197
let _ = tracker.upsert_peer_and_get_stats(&info_hash, &sample_peer());
210198

211-
let torrent_info = get_torrent_info(tracker.clone(), &info_hash).await.unwrap();
199+
let torrent_info = get_torrent_info(in_memory_torrent_repository.clone(), &info_hash)
200+
.await
201+
.unwrap();
212202

213203
assert_eq!(
214204
torrent_info,
@@ -226,25 +216,25 @@ mod tests {
226216
mod searching_for_torrents {
227217

228218
use std::str::FromStr;
219+
use std::sync::Arc;
229220

230221
use bittorrent_primitives::info_hash::InfoHash;
231222
use torrust_tracker_configuration::Configuration;
232223
use torrust_tracker_test_helpers::configuration;
233224

234225
use crate::core::services::torrent::tests::{initialize_tracker_and_deps, sample_peer};
235226
use crate::core::services::torrent::{get_torrents_page, BasicInfo, Pagination};
227+
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
236228

237229
pub fn tracker_configuration() -> Configuration {
238230
configuration::ephemeral()
239231
}
240232

241233
#[tokio::test]
242234
async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() {
243-
let config = tracker_configuration();
244-
245-
let tracker = initialize_tracker_and_deps(&config);
235+
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
246236

247-
let torrents = get_torrents_page(tracker.clone(), Some(&Pagination::default())).await;
237+
let torrents = get_torrents_page(in_memory_torrent_repository.clone(), Some(&Pagination::default())).await;
248238

249239
assert_eq!(torrents, vec![]);
250240
}
@@ -253,14 +243,14 @@ mod tests {
253243
async fn should_return_a_summarized_info_for_all_torrents() {
254244
let config = tracker_configuration();
255245

256-
let tracker = initialize_tracker_and_deps(&config);
246+
let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
257247

258248
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
259249
let info_hash = InfoHash::from_str(&hash).unwrap();
260250

261251
let _ = tracker.upsert_peer_and_get_stats(&info_hash, &sample_peer());
262252

263-
let torrents = get_torrents_page(tracker.clone(), Some(&Pagination::default())).await;
253+
let torrents = get_torrents_page(in_memory_torrent_repository.clone(), Some(&Pagination::default())).await;
264254

265255
assert_eq!(
266256
torrents,
@@ -277,7 +267,7 @@ mod tests {
277267
async fn should_allow_limiting_the_number_of_torrents_in_the_result() {
278268
let config = tracker_configuration();
279269

280-
let tracker = initialize_tracker_and_deps(&config);
270+
let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
281271

282272
let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
283273
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
@@ -290,7 +280,7 @@ mod tests {
290280
let offset = 0;
291281
let limit = 1;
292282

293-
let torrents = get_torrents_page(tracker.clone(), Some(&Pagination::new(offset, limit))).await;
283+
let torrents = get_torrents_page(in_memory_torrent_repository.clone(), Some(&Pagination::new(offset, limit))).await;
294284

295285
assert_eq!(torrents.len(), 1);
296286
}
@@ -299,7 +289,7 @@ mod tests {
299289
async fn should_allow_using_pagination_in_the_result() {
300290
let config = tracker_configuration();
301291

302-
let tracker = initialize_tracker_and_deps(&config);
292+
let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
303293

304294
let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
305295
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
@@ -312,7 +302,7 @@ mod tests {
312302
let offset = 1;
313303
let limit = 4000;
314304

315-
let torrents = get_torrents_page(tracker.clone(), Some(&Pagination::new(offset, limit))).await;
305+
let torrents = get_torrents_page(in_memory_torrent_repository.clone(), Some(&Pagination::new(offset, limit))).await;
316306

317307
assert_eq!(torrents.len(), 1);
318308
assert_eq!(
@@ -330,7 +320,7 @@ mod tests {
330320
async fn should_return_torrents_ordered_by_info_hash() {
331321
let config = tracker_configuration();
332322

333-
let tracker = initialize_tracker_and_deps(&config);
323+
let (tracker, in_memory_torrent_repository) = initialize_tracker_and_deps(&config);
334324

335325
let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
336326
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
@@ -340,7 +330,7 @@ mod tests {
340330
let info_hash2 = InfoHash::from_str(&hash2).unwrap();
341331
let _ = tracker.upsert_peer_and_get_stats(&info_hash2, &sample_peer());
342332

343-
let torrents = get_torrents_page(tracker.clone(), Some(&Pagination::default())).await;
333+
let torrents = get_torrents_page(in_memory_torrent_repository.clone(), Some(&Pagination::default())).await;
344334

345335
assert_eq!(
346336
torrents,

0 commit comments

Comments
 (0)