Skip to content

Commit d0f3806

Browse files
committed
dev: refactor torrent/repository
1 parent 7fa8c53 commit d0f3806

File tree

14 files changed

+803
-753
lines changed

14 files changed

+803
-753
lines changed

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use std::time::Duration;
33

44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
6-
use torrust_tracker::core::torrent::repository::tokio_sync::RepositoryTokioRwLock;
7-
use torrust_tracker::core::torrent::repository::UpdateTorrentAsync;
6+
use torrust_tracker::core::torrent::repository::{TorrentsTokioRwLock, UpdateTorrentAsync};
87
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
98

109
use crate::args::Args;
@@ -13,12 +12,12 @@ use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjuste
1312
pub async fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
1413
where
1514
T: Default,
16-
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
15+
TorrentsTokioRwLock<T>: UpdateTorrentAsync + Default,
1716
{
1817
let mut results: Vec<Duration> = Vec::with_capacity(samples);
1918

2019
for _ in 0..samples {
21-
let torrent_repository = Arc::new(RepositoryTokioRwLock::<T>::default());
20+
let torrent_repository = Arc::new(TorrentsTokioRwLock::<T>::default());
2221

2322
let info_hash = InfoHash([0; 20]);
2423

@@ -40,13 +39,13 @@ where
4039
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
4140
where
4241
T: Default + Send + Sync + 'static,
43-
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
42+
TorrentsTokioRwLock<T>: UpdateTorrentAsync + Default,
4443
{
4544
let args = Args::parse();
4645
let mut results: Vec<Duration> = Vec::with_capacity(samples);
4746

4847
for _ in 0..samples {
49-
let torrent_repository = Arc::new(RepositoryTokioRwLock::<T>::default());
48+
let torrent_repository = Arc::new(TorrentsTokioRwLock::<T>::default());
5049
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
5150
let handles = FuturesUnordered::new();
5251

@@ -90,13 +89,13 @@ where
9089
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
9190
where
9291
T: Default + Send + Sync + 'static,
93-
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
92+
TorrentsTokioRwLock<T>: UpdateTorrentAsync + Default,
9493
{
9594
let args = Args::parse();
9695
let mut results: Vec<Duration> = Vec::with_capacity(samples);
9796

9897
for _ in 0..samples {
99-
let torrent_repository = Arc::new(RepositoryTokioRwLock::<T>::default());
98+
let torrent_repository = Arc::new(TorrentsTokioRwLock::<T>::default());
10099
let info_hashes = generate_unique_info_hashes(10_000);
101100
let handles = FuturesUnordered::new();
102101

@@ -135,13 +134,13 @@ where
135134
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
136135
where
137136
T: Default + Send + Sync + 'static,
138-
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
137+
TorrentsTokioRwLock<T>: UpdateTorrentAsync + Default,
139138
{
140139
let args = Args::parse();
141140
let mut results: Vec<Duration> = Vec::with_capacity(samples);
142141

143142
for _ in 0..samples {
144-
let torrent_repository = Arc::new(RepositoryTokioRwLock::<T>::default());
143+
let torrent_repository = Arc::new(TorrentsTokioRwLock::<T>::default());
145144
let info_hashes = generate_unique_info_hashes(10_000);
146145
let handles = FuturesUnordered::new();
147146

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use std::time::Duration;
33

44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
6-
use torrust_tracker::core::torrent::repository::std_sync::RepositoryStdRwLock;
7-
use torrust_tracker::core::torrent::repository::UpdateTorrentSync;
6+
use torrust_tracker::core::torrent::repository::{TorrentsStdRwLock, UpdateTorrentSync};
87
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
98

109
use crate::args::Args;
@@ -14,12 +13,12 @@ use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjuste
1413
#[must_use]
1514
pub fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
1615
where
17-
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
16+
TorrentsStdRwLock<T>: UpdateTorrentSync + Default,
1817
{
1918
let mut results: Vec<Duration> = Vec::with_capacity(samples);
2019

2120
for _ in 0..samples {
22-
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
21+
let torrent_repository = Arc::new(TorrentsStdRwLock::<T>::default());
2322

2423
let info_hash = InfoHash([0; 20]);
2524

@@ -39,13 +38,13 @@ where
3938
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
4039
where
4140
T: Send + Sync + 'static,
42-
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
41+
TorrentsStdRwLock<T>: UpdateTorrentSync + Default,
4342
{
4443
let args = Args::parse();
4544
let mut results: Vec<Duration> = Vec::with_capacity(samples);
4645

4746
for _ in 0..samples {
48-
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
47+
let torrent_repository = Arc::new(TorrentsStdRwLock::<T>::default());
4948
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
5049
let handles = FuturesUnordered::new();
5150

@@ -85,13 +84,13 @@ where
8584
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
8685
where
8786
T: Send + Sync + 'static,
88-
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
87+
TorrentsStdRwLock<T>: UpdateTorrentSync + Default,
8988
{
9089
let args = Args::parse();
9190
let mut results: Vec<Duration> = Vec::with_capacity(samples);
9291

9392
for _ in 0..samples {
94-
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
93+
let torrent_repository = Arc::new(TorrentsStdRwLock::<T>::default());
9594
let info_hashes = generate_unique_info_hashes(10_000);
9695
let handles = FuturesUnordered::new();
9796

@@ -128,13 +127,13 @@ where
128127
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
129128
where
130129
T: Send + Sync + 'static,
131-
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
130+
TorrentsStdRwLock<T>: UpdateTorrentSync + Default,
132131
{
133132
let args = Args::parse();
134133
let mut results: Vec<Duration> = Vec::with_capacity(samples);
135134

136135
for _ in 0..samples {
137-
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
136+
let torrent_repository = Arc::new(TorrentsStdRwLock::<T>::default());
138137
let info_hashes = generate_unique_info_hashes(10_000);
139138
let handles = FuturesUnordered::new();
140139

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use std::time::Duration;
33

44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
6-
use torrust_tracker::core::torrent::repository::std_sync::RepositoryStdRwLock;
7-
use torrust_tracker::core::torrent::repository::UpdateTorrentAsync;
6+
use torrust_tracker::core::torrent::repository::{TorrentsStdRwLock, UpdateTorrentAsync};
87
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
98

109
use crate::args::Args;
@@ -14,12 +13,12 @@ use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjuste
1413
#[must_use]
1514
pub async fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
1615
where
17-
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
16+
TorrentsStdRwLock<T>: UpdateTorrentAsync + Default,
1817
{
1918
let mut results: Vec<Duration> = Vec::with_capacity(samples);
2019

2120
for _ in 0..samples {
22-
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
21+
let torrent_repository = Arc::new(TorrentsStdRwLock::<T>::default());
2322

2423
let info_hash = InfoHash([0; 20]);
2524

@@ -41,13 +40,13 @@ where
4140
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
4241
where
4342
T: Send + Sync + 'static,
44-
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
43+
TorrentsStdRwLock<T>: UpdateTorrentAsync + Default,
4544
{
4645
let args = Args::parse();
4746
let mut results: Vec<Duration> = Vec::with_capacity(samples);
4847

4948
for _ in 0..samples {
50-
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
49+
let torrent_repository = Arc::new(TorrentsStdRwLock::<T>::default());
5150
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
5251
let handles = FuturesUnordered::new();
5352

@@ -91,13 +90,13 @@ where
9190
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
9291
where
9392
T: Send + Sync + 'static,
94-
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
93+
TorrentsStdRwLock<T>: UpdateTorrentAsync + Default,
9594
{
9695
let args = Args::parse();
9796
let mut results: Vec<Duration> = Vec::with_capacity(samples);
9897

9998
for _ in 0..samples {
100-
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
99+
let torrent_repository = Arc::new(TorrentsStdRwLock::<T>::default());
101100
let info_hashes = generate_unique_info_hashes(10_000);
102101
let handles = FuturesUnordered::new();
103102

@@ -136,13 +135,13 @@ where
136135
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
137136
where
138137
T: Send + Sync + 'static,
139-
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
138+
TorrentsStdRwLock<T>: UpdateTorrentAsync + Default,
140139
{
141140
let args = Args::parse();
142141
let mut results: Vec<Duration> = Vec::with_capacity(samples);
143142

144143
for _ in 0..samples {
145-
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
144+
let torrent_repository = Arc::new(TorrentsStdRwLock::<T>::default());
146145
let info_hashes = generate_unique_info_hashes(10_000);
147146
let handles = FuturesUnordered::new();
148147

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn main() {
6464

6565
println!();
6666

67-
println!("std::sync::RwLock<std::collections::BTreeMap<InfoHash, Arc<std::sync::Mutex<Entry>>>>");
67+
println!("std::sync::RwLock<std::collections::BTreeMap<InfoHash, MutexStd>");
6868
println!(
6969
"{}: Avg/AdjAvg: {:?}",
7070
"add_one_torrent",
@@ -88,7 +88,7 @@ fn main() {
8888

8989
println!();
9090

91-
println!("std::sync::RwLock<std::collections::BTreeMap<InfoHash, Arc<tokio::sync::Mutex<Entry>>>>");
91+
println!("std::sync::RwLock<std::collections::BTreeMap<InfoHash, MutexTokio>");
9292
println!(
9393
"{}: Avg/AdjAvg: {:?}",
9494
"add_one_torrent",
@@ -112,7 +112,7 @@ fn main() {
112112

113113
println!();
114114

115-
println!("tokio::sync::RwLock<std::collections::BTreeMap<InfoHash, Arc<std::sync::Mutex<Entry>>>>");
115+
println!("tokio::sync::RwLock<std::collections::BTreeMap<InfoHash, MutexStd>");
116116
println!(
117117
"{}: Avg/AdjAvg: {:?}",
118118
"add_one_torrent",
@@ -136,7 +136,7 @@ fn main() {
136136

137137
println!();
138138

139-
println!("tokio::sync::RwLock<std::collections::BTreeMap<InfoHash, Arc<tokio::sync::Mutex<Entry>>>>");
139+
println!("tokio::sync::RwLock<std::collections::BTreeMap<InfoHash, MutexTokio>");
140140
println!(
141141
"{}: Avg/AdjAvg: {:?}",
142142
"add_one_torrent",

src/core/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,7 @@ use self::auth::Key;
450450
use self::error::Error;
451451
use self::peer::Peer;
452452
use self::torrent::entry::{Entry, ReadInfo, ReadPeers};
453-
use self::torrent::repository::tokio_sync::RepositoryTokioRwLock;
454-
use self::torrent::repository::{Repository, UpdateTorrentAsync};
453+
use self::torrent::repository::{Repository, TorrentsTokioRwLock, UpdateTorrentAsync};
455454
use crate::core::databases::Database;
456455
use crate::core::torrent::SwarmMetadata;
457456
use crate::shared::bit_torrent::info_hash::InfoHash;
@@ -477,7 +476,7 @@ pub struct Tracker {
477476
policy: TrackerPolicy,
478477
keys: tokio::sync::RwLock<std::collections::HashMap<Key, auth::ExpiringKey>>,
479478
whitelist: tokio::sync::RwLock<std::collections::HashSet<InfoHash>>,
480-
pub torrents: Arc<RepositoryTokioRwLock<Entry>>,
479+
pub torrents: Arc<TorrentsTokioRwLock<Entry>>,
481480
stats_event_sender: Option<Box<dyn statistics::EventSender>>,
482481
stats_repository: statistics::Repo,
483482
external_ip: Option<IpAddr>,
@@ -575,7 +574,7 @@ impl Tracker {
575574
mode,
576575
keys: tokio::sync::RwLock::new(std::collections::HashMap::new()),
577576
whitelist: tokio::sync::RwLock::new(std::collections::HashSet::new()),
578-
torrents: Arc::new(RepositoryTokioRwLock::<Entry>::default()),
577+
torrents: Arc::new(TorrentsTokioRwLock::<Entry>::default()),
579578
stats_event_sender,
580579
stats_repository,
581580
database,

src/core/torrent/repository/mod.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ use crate::core::services::torrent::Pagination;
44
use crate::core::{peer, TorrentsMetrics, TrackerPolicy};
55
use crate::shared::bit_torrent::info_hash::InfoHash;
66

7-
pub mod std_sync;
8-
pub mod tokio_sync;
7+
pub mod std_single;
8+
pub mod std_std;
9+
pub mod std_tokio;
10+
pub mod tokio_single;
11+
pub mod tokio_std;
12+
pub mod tokio_tokio;
913

1014
pub trait Repository<T>: Default {
1115
fn get(&self, key: &InfoHash) -> impl std::future::Future<Output = Option<T>> + Send;
@@ -17,6 +21,16 @@ pub trait Repository<T>: Default {
1721
fn remove_peerless_torrents(&self, policy: &TrackerPolicy) -> impl std::future::Future<Output = ()> + Send;
1822
}
1923

24+
#[derive(Default)]
25+
pub struct TorrentsTokioRwLock<T: Default> {
26+
torrents: tokio::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
27+
}
28+
29+
#[derive(Default)]
30+
pub struct TorrentsStdRwLock<T> {
31+
torrents: std::sync::RwLock<std::collections::BTreeMap<InfoHash, T>>,
32+
}
33+
2034
pub trait UpdateTorrentSync {
2135
fn update_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (bool, SwarmMetadata);
2236
}

0 commit comments

Comments
 (0)