Skip to content

Commit e9713a8

Browse files
committed
dev: refactor torrent/repository templates
1 parent f5141be commit e9713a8

File tree

7 files changed

+188
-164
lines changed

7 files changed

+188
-164
lines changed

packages/torrent-repository-benchmarks/src/benches/asyn.rs

+25-16
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ use std::time::Duration;
33

44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
6-
use torrust_tracker::core::torrent::repository_asyn::RepositoryAsync;
6+
use torrust_tracker::core::torrent::repository_asyn::{Repository, RepositoryAsync};
77
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
88

99
use crate::args::Args;
1010
use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjusted_average_from_results, DEFAULT_PEER};
1111

12-
pub async fn async_add_one_torrent<T: RepositoryAsync + Send + Sync + 'static>(samples: usize) -> (Duration, Duration) {
12+
pub async fn async_add_one_torrent<T>(samples: usize) -> (Duration, Duration)
13+
where
14+
Repository<T>: RepositoryAsync<T>,
15+
{
1316
let mut results: Vec<Duration> = Vec::with_capacity(samples);
1417

1518
for _ in 0..samples {
16-
let torrent_repository = Arc::new(T::new());
19+
let torrent_repository = Arc::new(Repository::<T>::default());
1720

1821
let info_hash = InfoHash([0; 20]);
1922

@@ -32,15 +35,16 @@ pub async fn async_add_one_torrent<T: RepositoryAsync + Send + Sync + 'static>(s
3235
}
3336

3437
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
35-
pub async fn async_update_one_torrent_in_parallel<T: RepositoryAsync + Send + Sync + 'static>(
36-
runtime: &tokio::runtime::Runtime,
37-
samples: usize,
38-
) -> (Duration, Duration) {
38+
pub async fn async_update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
39+
where
40+
T: Send + Sync + 'static,
41+
Repository<T>: RepositoryAsync<T>,
42+
{
3943
let args = Args::parse();
4044
let mut results: Vec<Duration> = Vec::with_capacity(samples);
4145

4246
for _ in 0..samples {
43-
let torrent_repository = Arc::new(T::new());
47+
let torrent_repository = Arc::new(Repository::<T>::default());
4448
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
4549
let handles = FuturesUnordered::new();
4650

@@ -81,15 +85,16 @@ pub async fn async_update_one_torrent_in_parallel<T: RepositoryAsync + Send + Sy
8185
}
8286

8387
// Add ten thousand torrents in parallel (depending on the set worker threads)
84-
pub async fn async_add_multiple_torrents_in_parallel<T: RepositoryAsync + Send + Sync + 'static>(
85-
runtime: &tokio::runtime::Runtime,
86-
samples: usize,
87-
) -> (Duration, Duration) {
88+
pub async fn async_add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
89+
where
90+
T: Send + Sync + 'static,
91+
Repository<T>: RepositoryAsync<T>,
92+
{
8893
let args = Args::parse();
8994
let mut results: Vec<Duration> = Vec::with_capacity(samples);
9095

9196
for _ in 0..samples {
92-
let torrent_repository = Arc::new(T::new());
97+
let torrent_repository = Arc::new(Repository::<T>::default());
9398
let info_hashes = generate_unique_info_hashes(10_000);
9499
let handles = FuturesUnordered::new();
95100

@@ -125,15 +130,19 @@ pub async fn async_add_multiple_torrents_in_parallel<T: RepositoryAsync + Send +
125130
}
126131

127132
// Async update ten thousand torrents in parallel (depending on the set worker threads)
128-
pub async fn async_update_multiple_torrents_in_parallel<T: RepositoryAsync + Send + Sync + 'static>(
133+
pub async fn async_update_multiple_torrents_in_parallel<T>(
129134
runtime: &tokio::runtime::Runtime,
130135
samples: usize,
131-
) -> (Duration, Duration) {
136+
) -> (Duration, Duration)
137+
where
138+
T: Send + Sync + 'static,
139+
Repository<T>: RepositoryAsync<T>,
140+
{
132141
let args = Args::parse();
133142
let mut results: Vec<Duration> = Vec::with_capacity(samples);
134143

135144
for _ in 0..samples {
136-
let torrent_repository = Arc::new(T::new());
145+
let torrent_repository = Arc::new(Repository::<T>::default());
137146
let info_hashes = generate_unique_info_hashes(10_000);
138147
let handles = FuturesUnordered::new();
139148

packages/torrent-repository-benchmarks/src/benches/sync.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@ use std::time::Duration;
33

44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
6-
use torrust_tracker::core::torrent::repository_sync::RepositorySync;
6+
use torrust_tracker::core::torrent::repository_sync::{Repository, RepositorySync};
77
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
88

99
use crate::args::Args;
1010
use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjusted_average_from_results, DEFAULT_PEER};
1111

1212
// Simply add one torrent
1313
#[must_use]
14-
pub fn add_one_torrent<T: RepositorySync + Send + Sync + 'static>(samples: usize) -> (Duration, Duration) {
14+
pub fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
15+
where
16+
Repository<T>: RepositorySync<T>,
17+
{
1518
let mut results: Vec<Duration> = Vec::with_capacity(samples);
1619

1720
for _ in 0..samples {
18-
let torrent_repository = Arc::new(T::new());
21+
let torrent_repository = Arc::new(Repository::<T>::default());
1922

2023
let info_hash = InfoHash([0; 20]);
2124

@@ -32,15 +35,16 @@ pub fn add_one_torrent<T: RepositorySync + Send + Sync + 'static>(samples: usize
3235
}
3336

3437
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
35-
pub async fn update_one_torrent_in_parallel<T: RepositorySync + Send + Sync + 'static>(
36-
runtime: &tokio::runtime::Runtime,
37-
samples: usize,
38-
) -> (Duration, Duration) {
38+
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
39+
where
40+
T: Send + Sync + 'static,
41+
Repository<T>: RepositorySync<T>,
42+
{
3943
let args = Args::parse();
4044
let mut results: Vec<Duration> = Vec::with_capacity(samples);
4145

4246
for _ in 0..samples {
43-
let torrent_repository = Arc::new(T::new());
47+
let torrent_repository = Arc::new(Repository::<T>::default());
4448
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
4549
let handles = FuturesUnordered::new();
4650

@@ -77,15 +81,16 @@ pub async fn update_one_torrent_in_parallel<T: RepositorySync + Send + Sync + 's
7781
}
7882

7983
// Add ten thousand torrents in parallel (depending on the set worker threads)
80-
pub async fn add_multiple_torrents_in_parallel<T: RepositorySync + Send + Sync + 'static>(
81-
runtime: &tokio::runtime::Runtime,
82-
samples: usize,
83-
) -> (Duration, Duration) {
84+
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
85+
where
86+
T: Send + Sync + 'static,
87+
Repository<T>: RepositorySync<T>,
88+
{
8489
let args = Args::parse();
8590
let mut results: Vec<Duration> = Vec::with_capacity(samples);
8691

8792
for _ in 0..samples {
88-
let torrent_repository = Arc::new(T::new());
93+
let torrent_repository = Arc::new(Repository::<T>::default());
8994
let info_hashes = generate_unique_info_hashes(10_000);
9095
let handles = FuturesUnordered::new();
9196

@@ -119,15 +124,16 @@ pub async fn add_multiple_torrents_in_parallel<T: RepositorySync + Send + Sync +
119124
}
120125

121126
// Update ten thousand torrents in parallel (depending on the set worker threads)
122-
pub async fn update_multiple_torrents_in_parallel<T: RepositorySync + Send + Sync + 'static>(
123-
runtime: &tokio::runtime::Runtime,
124-
samples: usize,
125-
) -> (Duration, Duration) {
127+
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
128+
where
129+
T: Send + Sync + 'static,
130+
Repository<T>: RepositorySync<T>,
131+
{
126132
let args = Args::parse();
127133
let mut results: Vec<Duration> = Vec::with_capacity(samples);
128134

129135
for _ in 0..samples {
130-
let torrent_repository = Arc::new(T::new());
136+
let torrent_repository = Arc::new(Repository::<T>::default());
131137
let info_hashes = generate_unique_info_hashes(10_000);
132138
let handles = FuturesUnordered::new();
133139

packages/torrent-repository-benchmarks/src/main.rs

+26-22
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use torrust_torrent_repository_benchmarks::benches::asyn::{
77
use torrust_torrent_repository_benchmarks::benches::sync::{
88
add_multiple_torrents_in_parallel, add_one_torrent, update_multiple_torrents_in_parallel, update_one_torrent_in_parallel,
99
};
10-
use torrust_tracker::core::torrent::repository_asyn::{Async, AsyncSync, RepositoryAsyncSingle};
11-
use torrust_tracker::core::torrent::repository_sync::{Sync, SyncSingle};
10+
use torrust_tracker::core::torrent::repository_asyn::{TorrentsAsync, TorrentsAsyncSingle, TorrentsAsyncSync};
11+
use torrust_tracker::core::torrent::repository_sync::{TorrentsSync, TorrentsSyncSingle};
1212

1313
#[allow(clippy::too_many_lines)]
1414
#[allow(clippy::print_literal)]
@@ -26,22 +26,22 @@ fn main() {
2626
println!(
2727
"{}: Avg/AdjAvg: {:?}",
2828
"add_one_torrent",
29-
rt.block_on(async_add_one_torrent::<RepositoryAsyncSingle>(1_000_000))
29+
rt.block_on(async_add_one_torrent::<TorrentsAsyncSingle>(1_000_000))
3030
);
3131
println!(
3232
"{}: Avg/AdjAvg: {:?}",
3333
"update_one_torrent_in_parallel",
34-
rt.block_on(async_update_one_torrent_in_parallel::<RepositoryAsyncSingle>(&rt, 10))
34+
rt.block_on(async_update_one_torrent_in_parallel::<TorrentsAsyncSingle>(&rt, 10))
3535
);
3636
println!(
3737
"{}: Avg/AdjAvg: {:?}",
3838
"add_multiple_torrents_in_parallel",
39-
rt.block_on(async_add_multiple_torrents_in_parallel::<RepositoryAsyncSingle>(&rt, 10))
39+
rt.block_on(async_add_multiple_torrents_in_parallel::<TorrentsAsyncSingle>(&rt, 10))
4040
);
4141
println!(
4242
"{}: Avg/AdjAvg: {:?}",
4343
"update_multiple_torrents_in_parallel",
44-
rt.block_on(async_update_multiple_torrents_in_parallel::<RepositoryAsyncSingle>(&rt, 10))
44+
rt.block_on(async_update_multiple_torrents_in_parallel::<TorrentsAsyncSingle>(&rt, 10))
4545
);
4646

4747
if let Some(true) = args.compare {
@@ -51,42 +51,46 @@ fn main() {
5151
println!(
5252
"{}: Avg/AdjAvg: {:?}",
5353
"add_one_torrent",
54-
add_one_torrent::<SyncSingle>(1_000_000)
54+
add_one_torrent::<TorrentsSyncSingle>(1_000_000)
5555
);
5656
println!(
5757
"{}: Avg/AdjAvg: {:?}",
5858
"update_one_torrent_in_parallel",
59-
rt.block_on(update_one_torrent_in_parallel::<SyncSingle>(&rt, 10))
59+
rt.block_on(update_one_torrent_in_parallel::<TorrentsSyncSingle>(&rt, 10))
6060
);
6161
println!(
6262
"{}: Avg/AdjAvg: {:?}",
6363
"add_multiple_torrents_in_parallel",
64-
rt.block_on(add_multiple_torrents_in_parallel::<SyncSingle>(&rt, 10))
64+
rt.block_on(add_multiple_torrents_in_parallel::<TorrentsSyncSingle>(&rt, 10))
6565
);
6666
println!(
6767
"{}: Avg/AdjAvg: {:?}",
6868
"update_multiple_torrents_in_parallel",
69-
rt.block_on(update_multiple_torrents_in_parallel::<SyncSingle>(&rt, 10))
69+
rt.block_on(update_multiple_torrents_in_parallel::<TorrentsSyncSingle>(&rt, 10))
7070
);
7171

7272
println!();
7373

7474
println!("std::sync::RwLock<std::collections::BTreeMap<InfoHash, Arc<std::sync::Mutex<Entry>>>>");
75-
println!("{}: Avg/AdjAvg: {:?}", "add_one_torrent", add_one_torrent::<Sync>(1_000_000));
75+
println!(
76+
"{}: Avg/AdjAvg: {:?}",
77+
"add_one_torrent",
78+
add_one_torrent::<TorrentsSync>(1_000_000)
79+
);
7680
println!(
7781
"{}: Avg/AdjAvg: {:?}",
7882
"update_one_torrent_in_parallel",
79-
rt.block_on(update_one_torrent_in_parallel::<Sync>(&rt, 10))
83+
rt.block_on(update_one_torrent_in_parallel::<TorrentsSync>(&rt, 10))
8084
);
8185
println!(
8286
"{}: Avg/AdjAvg: {:?}",
8387
"add_multiple_torrents_in_parallel",
84-
rt.block_on(add_multiple_torrents_in_parallel::<Sync>(&rt, 10))
88+
rt.block_on(add_multiple_torrents_in_parallel::<TorrentsSync>(&rt, 10))
8589
);
8690
println!(
8791
"{}: Avg/AdjAvg: {:?}",
8892
"update_multiple_torrents_in_parallel",
89-
rt.block_on(update_multiple_torrents_in_parallel::<Sync>(&rt, 10))
93+
rt.block_on(update_multiple_torrents_in_parallel::<TorrentsSync>(&rt, 10))
9094
);
9195

9296
println!();
@@ -95,22 +99,22 @@ fn main() {
9599
println!(
96100
"{}: Avg/AdjAvg: {:?}",
97101
"add_one_torrent",
98-
rt.block_on(async_add_one_torrent::<AsyncSync>(1_000_000))
102+
rt.block_on(async_add_one_torrent::<TorrentsAsyncSync>(1_000_000))
99103
);
100104
println!(
101105
"{}: Avg/AdjAvg: {:?}",
102106
"update_one_torrent_in_parallel",
103-
rt.block_on(async_update_one_torrent_in_parallel::<AsyncSync>(&rt, 10))
107+
rt.block_on(async_update_one_torrent_in_parallel::<TorrentsAsyncSync>(&rt, 10))
104108
);
105109
println!(
106110
"{}: Avg/AdjAvg: {:?}",
107111
"add_multiple_torrents_in_parallel",
108-
rt.block_on(async_add_multiple_torrents_in_parallel::<AsyncSync>(&rt, 10))
112+
rt.block_on(async_add_multiple_torrents_in_parallel::<TorrentsAsyncSync>(&rt, 10))
109113
);
110114
println!(
111115
"{}: Avg/AdjAvg: {:?}",
112116
"update_multiple_torrents_in_parallel",
113-
rt.block_on(async_update_multiple_torrents_in_parallel::<AsyncSync>(&rt, 10))
117+
rt.block_on(async_update_multiple_torrents_in_parallel::<TorrentsAsyncSync>(&rt, 10))
114118
);
115119

116120
println!();
@@ -119,22 +123,22 @@ fn main() {
119123
println!(
120124
"{}: Avg/AdjAvg: {:?}",
121125
"add_one_torrent",
122-
rt.block_on(async_add_one_torrent::<Async>(1_000_000))
126+
rt.block_on(async_add_one_torrent::<TorrentsAsync>(1_000_000))
123127
);
124128
println!(
125129
"{}: Avg/AdjAvg: {:?}",
126130
"update_one_torrent_in_parallel",
127-
rt.block_on(async_update_one_torrent_in_parallel::<Async>(&rt, 10))
131+
rt.block_on(async_update_one_torrent_in_parallel::<TorrentsAsync>(&rt, 10))
128132
);
129133
println!(
130134
"{}: Avg/AdjAvg: {:?}",
131135
"add_multiple_torrents_in_parallel",
132-
rt.block_on(async_add_multiple_torrents_in_parallel::<Async>(&rt, 10))
136+
rt.block_on(async_add_multiple_torrents_in_parallel::<TorrentsAsync>(&rt, 10))
133137
);
134138
println!(
135139
"{}: Avg/AdjAvg: {:?}",
136140
"update_multiple_torrents_in_parallel",
137-
rt.block_on(async_update_multiple_torrents_in_parallel::<Async>(&rt, 10))
141+
rt.block_on(async_update_multiple_torrents_in_parallel::<TorrentsAsync>(&rt, 10))
138142
);
139143
}
140144
}

src/core/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ use torrust_tracker_primitives::TrackerMode;
455455
use self::auth::Key;
456456
use self::error::Error;
457457
use self::peer::Peer;
458-
use self::torrent::repository_asyn::{RepositoryAsync, RepositoryAsyncSingle};
458+
use self::torrent::repository_asyn::{Repository, RepositoryAsync, TorrentsAsyncSingle};
459459
use crate::core::databases::Database;
460460
use crate::core::torrent::{SwarmMetadata, SwarmStats};
461461
use crate::shared::bit_torrent::info_hash::InfoHash;
@@ -481,7 +481,7 @@ pub struct Tracker {
481481
policy: TrackerPolicy,
482482
keys: tokio::sync::RwLock<std::collections::HashMap<Key, auth::ExpiringKey>>,
483483
whitelist: tokio::sync::RwLock<std::collections::HashSet<InfoHash>>,
484-
pub torrents: Arc<RepositoryAsyncSingle>,
484+
pub torrents: Arc<Repository<TorrentsAsyncSingle>>,
485485
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
486486
stats_repository: statistics::Repo,
487487
external_ip: Option<IpAddr>,
@@ -579,7 +579,7 @@ impl Tracker {
579579
mode,
580580
keys: tokio::sync::RwLock::new(std::collections::HashMap::new()),
581581
whitelist: tokio::sync::RwLock::new(std::collections::HashSet::new()),
582-
torrents: Arc::new(RepositoryAsyncSingle::new()),
582+
torrents: Arc::new(Repository::<TorrentsAsyncSingle>::default()),
583583
stats_event_sender,
584584
stats_repository,
585585
database,
@@ -1754,6 +1754,7 @@ mod tests {
17541754
use aquatic_udp_protocol::AnnounceEvent;
17551755

17561756
use crate::core::tests::the_tracker::{sample_info_hash, sample_peer, tracker_persisting_torrents_in_database};
1757+
use crate::core::torrent::repository_asyn::RepositoryAsync;
17571758

17581759
#[tokio::test]
17591760
async fn it_should_persist_the_number_of_completed_peers_for_all_torrents_into_the_database() {

src/core/services/torrent.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::sync::Arc;
99
use serde::Deserialize;
1010

1111
use crate::core::peer::Peer;
12+
use crate::core::torrent::repository_asyn::RepositoryAsync;
1213
use crate::core::Tracker;
1314
use crate::shared::bit_torrent::info_hash::InfoHash;
1415

0 commit comments

Comments
 (0)