Skip to content

Commit 2c479a1

Browse files
committed
refactor: [#1358] inject event sender in Swarms type
1 parent 68b930d commit 2c479a1

File tree

17 files changed

+231
-132
lines changed

17 files changed

+231
-132
lines changed

packages/axum-http-tracker-server/src/environment.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ pub struct Environment<S> {
2525

2626
impl<S> Environment<S> {
2727
/// Add a torrent to the tracker
28-
pub fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
29-
let _number_of_downloads_increased = self
30-
.container
28+
pub async fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> bool {
29+
self.container
3130
.tracker_core_container
3231
.in_memory_torrent_repository
33-
.upsert_peer(info_hash, peer, None);
32+
.upsert_peer(info_hash, peer, None)
33+
.await
3434
}
3535
}
3636

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ mod for_all_config_modes {
474474
let previously_announced_peer = PeerBuilder::default().with_peer_id(&PeerId(*b"-qB00000000000000001")).build();
475475

476476
// Add the Peer 1
477-
env.add_torrent_peer(&info_hash, &previously_announced_peer);
477+
env.add_torrent_peer(&info_hash, &previously_announced_peer).await;
478478

479479
// Announce the new Peer 2. This new peer is non included on the response peer list
480480
let response = Client::new(*env.bind_address())
@@ -517,7 +517,7 @@ mod for_all_config_modes {
517517
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
518518
.with_peer_addr(&SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0x69, 0x69, 0x69, 0x69)), 8080))
519519
.build();
520-
env.add_torrent_peer(&info_hash, &peer_using_ipv4);
520+
env.add_torrent_peer(&info_hash, &peer_using_ipv4).await;
521521

522522
// Announce a peer using IPV6
523523
let peer_using_ipv6 = PeerBuilder::default()
@@ -527,7 +527,7 @@ mod for_all_config_modes {
527527
8080,
528528
))
529529
.build();
530-
env.add_torrent_peer(&info_hash, &peer_using_ipv6);
530+
env.add_torrent_peer(&info_hash, &peer_using_ipv6).await;
531531

532532
// Announce the new Peer.
533533
let response = Client::new(*env.bind_address())
@@ -625,7 +625,7 @@ mod for_all_config_modes {
625625
let previously_announced_peer = PeerBuilder::default().with_peer_id(&PeerId(*b"-qB00000000000000001")).build();
626626

627627
// Add the Peer 1
628-
env.add_torrent_peer(&info_hash, &previously_announced_peer);
628+
env.add_torrent_peer(&info_hash, &previously_announced_peer).await;
629629

630630
// Announce the new Peer 2 accepting compact responses
631631
let response = Client::new(*env.bind_address())
@@ -666,7 +666,7 @@ mod for_all_config_modes {
666666
let previously_announced_peer = PeerBuilder::default().with_peer_id(&PeerId(*b"-qB00000000000000001")).build();
667667

668668
// Add the Peer 1
669-
env.add_torrent_peer(&info_hash, &previously_announced_peer);
669+
env.add_torrent_peer(&info_hash, &previously_announced_peer).await;
670670

671671
// Announce the new Peer 2 without passing the "compact" param
672672
// By default it should respond with the compact peer list
@@ -1010,7 +1010,8 @@ mod for_all_config_modes {
10101010
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
10111011
.with_bytes_pending_to_download(1)
10121012
.build(),
1013-
);
1013+
)
1014+
.await;
10141015

10151016
let response = Client::new(*env.bind_address())
10161017
.scrape(
@@ -1050,7 +1051,8 @@ mod for_all_config_modes {
10501051
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
10511052
.with_no_bytes_pending_to_download()
10521053
.build(),
1053-
);
1054+
)
1055+
.await;
10541056

10551057
let response = Client::new(*env.bind_address())
10561058
.scrape(
@@ -1282,7 +1284,8 @@ mod configured_as_whitelisted {
12821284
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
12831285
.with_bytes_pending_to_download(1)
12841286
.build(),
1285-
);
1287+
)
1288+
.await;
12861289

12871290
let response = Client::new(*env.bind_address())
12881291
.scrape(
@@ -1318,7 +1321,8 @@ mod configured_as_whitelisted {
13181321
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
13191322
.with_bytes_pending_to_download(1)
13201323
.build(),
1321-
);
1324+
)
1325+
.await;
13221326

13231327
env.container
13241328
.tracker_core_container
@@ -1494,7 +1498,8 @@ mod configured_as_private {
14941498
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
14951499
.with_bytes_pending_to_download(1)
14961500
.build(),
1497-
);
1501+
)
1502+
.await;
14981503

14991504
let response = Client::new(*env.bind_address())
15001505
.scrape(
@@ -1525,7 +1530,8 @@ mod configured_as_private {
15251530
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
15261531
.with_bytes_pending_to_download(1)
15271532
.build(),
1528-
);
1533+
)
1534+
.await;
15291535

15301536
let expiring_key = env
15311537
.container
@@ -1576,7 +1582,8 @@ mod configured_as_private {
15761582
.with_peer_id(&PeerId(*b"-qB00000000000000001"))
15771583
.with_bytes_pending_to_download(1)
15781584
.build(),
1579-
);
1585+
)
1586+
.await;
15801587

15811588
let false_key: Key = "YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ".parse().unwrap();
15821589

packages/axum-rest-tracker-api-server/src/environment.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ where
3333
S: std::fmt::Debug + std::fmt::Display,
3434
{
3535
/// Add a torrent to the tracker
36-
pub fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
37-
let _number_of_downloads_increased = self
38-
.container
36+
pub async fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> bool {
37+
self.container
3938
.tracker_core_container
4039
.in_memory_torrent_repository
41-
.upsert_peer(info_hash, peer, None);
40+
.upsert_peer(info_hash, peer, None)
41+
.await
4242
}
4343
}
4444

packages/axum-rest-tracker-api-server/tests/server/v1/contract/context/stats.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ async fn should_allow_getting_tracker_statistics() {
2121
env.add_torrent_peer(
2222
&InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap(), // DevSkim: ignore DS173237
2323
&PeerBuilder::default().into(),
24-
);
24+
)
25+
.await;
2526

2627
let request_id = Uuid::new_v4();
2728

packages/axum-rest-tracker-api-server/tests/server/v1/contract/context/torrent.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async fn should_allow_getting_all_torrents() {
2626

2727
let info_hash = InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap(); // DevSkim: ignore DS173237
2828

29-
env.add_torrent_peer(&info_hash, &PeerBuilder::default().into());
29+
env.add_torrent_peer(&info_hash, &PeerBuilder::default().into()).await;
3030

3131
let request_id = Uuid::new_v4();
3232

@@ -59,8 +59,8 @@ async fn should_allow_limiting_the_torrents_in_the_result() {
5959
let info_hash_1 = InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap(); // DevSkim: ignore DS173237
6060
let info_hash_2 = InfoHash::from_str("0b3aea4adc213ce32295be85d3883a63bca25446").unwrap(); // DevSkim: ignore DS173237
6161

62-
env.add_torrent_peer(&info_hash_1, &PeerBuilder::default().into());
63-
env.add_torrent_peer(&info_hash_2, &PeerBuilder::default().into());
62+
env.add_torrent_peer(&info_hash_1, &PeerBuilder::default().into()).await;
63+
env.add_torrent_peer(&info_hash_2, &PeerBuilder::default().into()).await;
6464

6565
let request_id = Uuid::new_v4();
6666

@@ -96,8 +96,8 @@ async fn should_allow_the_torrents_result_pagination() {
9696
let info_hash_1 = InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap(); // DevSkim: ignore DS173237
9797
let info_hash_2 = InfoHash::from_str("0b3aea4adc213ce32295be85d3883a63bca25446").unwrap(); // DevSkim: ignore DS173237
9898

99-
env.add_torrent_peer(&info_hash_1, &PeerBuilder::default().into());
100-
env.add_torrent_peer(&info_hash_2, &PeerBuilder::default().into());
99+
env.add_torrent_peer(&info_hash_1, &PeerBuilder::default().into()).await;
100+
env.add_torrent_peer(&info_hash_2, &PeerBuilder::default().into()).await;
101101

102102
let request_id = Uuid::new_v4();
103103

@@ -132,8 +132,8 @@ async fn should_allow_getting_a_list_of_torrents_providing_infohashes() {
132132
let info_hash_1 = InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap(); // DevSkim: ignore DS173237
133133
let info_hash_2 = InfoHash::from_str("0b3aea4adc213ce32295be85d3883a63bca25446").unwrap(); // DevSkim: ignore DS173237
134134

135-
env.add_torrent_peer(&info_hash_1, &PeerBuilder::default().into());
136-
env.add_torrent_peer(&info_hash_2, &PeerBuilder::default().into());
135+
env.add_torrent_peer(&info_hash_1, &PeerBuilder::default().into()).await;
136+
env.add_torrent_peer(&info_hash_2, &PeerBuilder::default().into()).await;
137137

138138
let request_id = Uuid::new_v4();
139139

@@ -307,7 +307,7 @@ async fn should_allow_getting_a_torrent_info() {
307307

308308
let peer = PeerBuilder::default().into();
309309

310-
env.add_torrent_peer(&info_hash, &peer);
310+
env.add_torrent_peer(&info_hash, &peer).await;
311311

312312
let request_id = Uuid::new_v4();
313313

@@ -389,7 +389,7 @@ async fn should_not_allow_getting_a_torrent_info_for_unauthenticated_users() {
389389

390390
let info_hash = InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap(); // DevSkim: ignore DS173237
391391

392-
env.add_torrent_peer(&info_hash, &PeerBuilder::default().into());
392+
env.add_torrent_peer(&info_hash, &PeerBuilder::default().into()).await;
393393

394394
let request_id = Uuid::new_v4();
395395

packages/events/src/sender.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt;
2+
use std::fmt::Debug;
23

34
use futures::future::BoxFuture;
45
#[cfg(test)]

packages/torrent-repository/src/container.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ pub struct TorrentRepositoryContainer {
1616
impl TorrentRepositoryContainer {
1717
#[must_use]
1818
pub fn initialize() -> Self {
19-
let swarms = Arc::new(Swarms::default());
20-
2119
// Torrent repository stats
2220
let broadcaster = Broadcaster::default();
2321
let stats_repository = Arc::new(Repository::new());
@@ -27,6 +25,8 @@ impl TorrentRepositoryContainer {
2725

2826
let stats_event_sender = event_bus.sender();
2927

28+
let swarms = Arc::new(Swarms::new(stats_event_sender.clone()));
29+
3030
Self {
3131
swarms,
3232
event_bus,

packages/torrent-repository/src/statistics/event/handler.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,25 @@ use crate::statistics::repository::Repository;
99
///
1010
/// This function panics if the client IP address is not the same as the IP
1111
/// version of the event.
12-
pub async fn handle_event(_event: Event, stats_repository: &Arc<Repository>, _now: DurationSinceUnixEpoch) {
13-
/*match event {
14-
Event::TorrentAdded { .. } => {}
15-
Event::TorrentRemoved { .. } => {}
16-
Event::PeerAdded { .. } => {}
17-
Event::PeerRemoved { .. } => {}
18-
}*/
12+
pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, _now: DurationSinceUnixEpoch) {
13+
match event {
14+
Event::TorrentAdded { info_hash, .. } => {
15+
// todo: update metrics
16+
tracing::debug!("Torrent added {info_hash}");
17+
}
18+
Event::TorrentRemoved { info_hash } => {
19+
// todo: update metrics
20+
tracing::debug!("Torrent removed {info_hash}");
21+
}
22+
Event::PeerAdded { announcement } => {
23+
// todo: update metrics
24+
tracing::debug!("Peer added {announcement:?}");
25+
}
26+
Event::PeerRemoved { socket_addr, peer_id } => {
27+
// todo: update metrics
28+
tracing::debug!("Peer removed: socket address {socket_addr:?}, peer ID: {peer_id:?}");
29+
}
30+
}
1931

2032
tracing::debug!("metrics: {:?}", stats_repository.get_metrics().await);
2133
}

0 commit comments

Comments
 (0)