Skip to content

Commit 4483e76

Browse files
committed
feat: [#1358] inject Swarms into InMemoryTorrentRepository in production code
todo: do the same for testing code.
1 parent 17f4e0c commit 4483e76

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

packages/torrent-repository/src/swarms.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use torrust_tracker_primitives::swarm_metadata::{AggregateSwarmMetadata, SwarmMe
99
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrent, PersistentTorrents};
1010

1111
use crate::swarm::Swarm;
12-
use crate::SwarmHandle;
12+
use crate::{SwarmHandle, TORRENT_REPOSITORY_LOG_TARGET};
1313

1414
#[derive(Default, Debug)]
1515
pub struct Swarms {
@@ -43,6 +43,8 @@ impl Swarms {
4343
peer: &peer::Peer,
4444
opt_persistent_torrent: Option<PersistentTorrent>,
4545
) -> Result<bool, Error> {
46+
tracing::trace!(target: TORRENT_REPOSITORY_LOG_TARGET, "Handling announcement for torrent: {info_hash}");
47+
4648
let swarm_handle = if let Some(number_of_downloads) = opt_persistent_torrent {
4749
SwarmHandle::new(Swarm::new(number_of_downloads).into())
4850
} else {

packages/tracker-core/src/container.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::sync::Arc;
22

33
use torrust_tracker_configuration::Core;
4+
use torrust_tracker_torrent_repository::container::TorrentRepositoryContainer;
5+
use torrust_tracker_torrent_repository::Swarms;
46

57
use crate::announce_handler::AnnounceHandler;
68
use crate::authentication::handler::KeysHandler;
@@ -35,8 +37,19 @@ pub struct TrackerCoreContainer {
3537
}
3638

3739
impl TrackerCoreContainer {
40+
#[must_use]
41+
pub fn initialize_from(core_config: &Arc<Core>, torrent_repository_container: &Arc<TorrentRepositoryContainer>) -> Self {
42+
Self::inner_initialize(core_config, &torrent_repository_container.swarms)
43+
}
44+
3845
#[must_use]
3946
pub fn initialize(core_config: &Arc<Core>) -> Self {
47+
let swarms = Arc::new(Swarms::default());
48+
Self::inner_initialize(core_config, &swarms)
49+
}
50+
51+
#[must_use]
52+
fn inner_initialize(core_config: &Arc<Core>, swarms: &Arc<Swarms>) -> Self {
4053
let database = initialize_database(core_config);
4154
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
4255
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(core_config, &in_memory_whitelist.clone()));
@@ -48,7 +61,7 @@ impl TrackerCoreContainer {
4861
&db_key_repository.clone(),
4962
&in_memory_key_repository.clone(),
5063
));
51-
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
64+
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::new(swarms.clone()));
5265
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
5366

5467
let torrents_manager = Arc::new(TorrentsManager::new(

packages/tracker-core/src/torrent/repository/in_memory.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ pub struct InMemoryTorrentRepository {
2525
}
2626

2727
impl InMemoryTorrentRepository {
28+
#[must_use]
29+
pub fn new(swarms: Arc<Swarms>) -> Self {
30+
Self { swarms }
31+
}
32+
2833
/// Inserts or updates a peer in the torrent entry corresponding to the
2934
/// given infohash.
3035
///

src/container.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ impl AppContainer {
6464

6565
// Core
6666

67-
let tracker_core_container = Arc::new(TrackerCoreContainer::initialize(&core_config));
67+
let tracker_core_container = Arc::new(TrackerCoreContainer::initialize_from(
68+
&core_config,
69+
&torrent_repository_container,
70+
));
6871

6972
// HTTP
7073

0 commit comments

Comments
 (0)