Skip to content

Commit 0b62136

Browse files
committed
refactor: [#1358] inject event sender in Swarm type
It required to use `tokio::sync::Mutex` for the `SwarmHandle` (`Arc<Mutex<Swarm>>`). Otherwise it's not safe to pass the Swarm lock between threads.
1 parent e4a8d1c commit 0b62136

20 files changed

Lines changed: 510 additions & 402 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/axum-http-tracker-server/tests/server/v1/contract.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,8 @@ mod for_all_config_modes {
787787
.container
788788
.tracker_core_container
789789
.in_memory_torrent_repository
790-
.get_torrent_peers(&info_hash);
790+
.get_torrent_peers(&info_hash)
791+
.await;
791792
let peer_addr = peers[0].peer_addr;
792793

793794
assert_eq!(peer_addr.ip(), client_ip);
@@ -829,7 +830,8 @@ mod for_all_config_modes {
829830
.container
830831
.tracker_core_container
831832
.in_memory_torrent_repository
832-
.get_torrent_peers(&info_hash);
833+
.get_torrent_peers(&info_hash)
834+
.await;
833835
let peer_addr = peers[0].peer_addr;
834836

835837
assert_eq!(
@@ -878,7 +880,8 @@ mod for_all_config_modes {
878880
.container
879881
.tracker_core_container
880882
.in_memory_torrent_repository
881-
.get_torrent_peers(&info_hash);
883+
.get_torrent_peers(&info_hash)
884+
.await;
882885
let peer_addr = peers[0].peer_addr;
883886

884887
assert_eq!(
@@ -925,7 +928,8 @@ mod for_all_config_modes {
925928
.container
926929
.tracker_core_container
927930
.in_memory_torrent_repository
928-
.get_torrent_peers(&info_hash);
931+
.get_torrent_peers(&info_hash)
932+
.await;
929933
let peer_addr = peers[0].peer_addr;
930934

931935
assert_eq!(peer_addr.ip(), IpAddr::from_str("150.172.238.178").unwrap());

packages/axum-rest-tracker-api-server/src/v1/context/torrent/handlers.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub async fn get_torrent_handler(
3333
) -> Response {
3434
match InfoHash::from_str(&info_hash.0) {
3535
Err(_) => invalid_info_hash_param_response(&info_hash.0),
36-
Ok(info_hash) => match get_torrent_info(&in_memory_torrent_repository, &info_hash) {
36+
Ok(info_hash) => match get_torrent_info(&in_memory_torrent_repository, &info_hash).await {
3737
Some(info) => torrent_info_response(info).into_response(),
3838
None => torrent_not_known_response(),
3939
},
@@ -85,14 +85,19 @@ pub async fn get_torrents_handler(
8585
tracing::debug!("pagination: {:?}", pagination);
8686

8787
if pagination.0.info_hashes.is_empty() {
88-
torrent_list_response(&get_torrents_page(
89-
&in_memory_torrent_repository,
90-
Some(&Pagination::new_with_options(pagination.0.offset, pagination.0.limit)),
91-
))
88+
torrent_list_response(
89+
&get_torrents_page(
90+
&in_memory_torrent_repository,
91+
Some(&Pagination::new_with_options(pagination.0.offset, pagination.0.limit)),
92+
)
93+
.await,
94+
)
9295
.into_response()
9396
} else {
9497
match parse_info_hashes(pagination.0.info_hashes) {
95-
Ok(info_hashes) => torrent_list_response(&get_torrents(&in_memory_torrent_repository, &info_hashes)).into_response(),
98+
Ok(info_hashes) => {
99+
torrent_list_response(&get_torrents(&in_memory_torrent_repository, &info_hashes).await).into_response()
100+
}
96101
Err(err) => match err {
97102
QueryParamError::InvalidInfoHash { info_hash } => invalid_info_hash_param_response(&info_hash),
98103
},

packages/http-tracker-core/src/statistics/services.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub async fn get_metrics(
4747
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
4848
stats_repository: Arc<Repository>,
4949
) -> TrackerMetrics {
50-
let torrents_metrics = in_memory_torrent_repository.get_aggregate_swarm_metadata();
50+
let torrents_metrics = in_memory_torrent_repository.get_aggregate_swarm_metadata().await;
5151
let stats = stats_repository.get_stats().await;
5252

5353
TrackerMetrics {

packages/rest-tracker-api-core/src/statistics/services.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub async fn get_metrics(
3232
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
3333
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
3434
) -> TrackerMetrics {
35-
let torrents_metrics = in_memory_torrent_repository.get_aggregate_swarm_metadata();
35+
let torrents_metrics = in_memory_torrent_repository.get_aggregate_swarm_metadata().await;
3636
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
3737
let http_stats = http_stats_repository.get_stats().await;
3838
let udp_server_stats = udp_server_stats_repository.get_stats().await;

packages/torrent-repository/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ version.workspace = true
1919
aquatic_udp_protocol = "0"
2020
bittorrent-primitives = "0.1.0"
2121
crossbeam-skiplist = "0"
22+
futures = "0"
2223
serde = "1.0.219"
2324
thiserror = "2.0.12"
2425
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }

packages/torrent-repository/src/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ pub mod statistics;
44
pub mod swarm;
55
pub mod swarms;
66

7-
use std::sync::{Arc, Mutex, MutexGuard};
7+
use std::sync::Arc;
88

9+
use tokio::sync::Mutex;
910
use torrust_tracker_clock::clock;
1011

1112
pub type Swarms = swarms::Swarms;
@@ -24,16 +25,6 @@ pub(crate) type CurrentClock = clock::Stopped;
2425

2526
pub const TORRENT_REPOSITORY_LOG_TARGET: &str = "TORRENT_REPOSITORY";
2627

27-
pub trait LockTrackedTorrent {
28-
fn lock_or_panic(&self) -> MutexGuard<'_, Swarm>;
29-
}
30-
31-
impl LockTrackedTorrent for SwarmHandle {
32-
fn lock_or_panic(&self) -> MutexGuard<'_, Swarm> {
33-
self.lock().expect("can't acquire lock for tracked torrent handle")
34-
}
35-
}
36-
3728
#[cfg(test)]
3829
pub(crate) mod tests {
3930
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

0 commit comments

Comments
 (0)