Skip to content

Commit

Permalink
refactor: [#1182] inject database and whitelist manager in tracker fa…
Browse files Browse the repository at this point in the history
…ctory

Refactor in progress. The final goal is to inject the whitelist manager
directly wherever is needed (for example, test evns) to avoid injecting
the whole tracker. Adn to finally remove the whitelist manager from the
Tracker (A higer level refator in progress: remove responsabilities
fromcore Tracker).
  • Loading branch information
josecelano committed Jan 15, 2025
1 parent 4253d0f commit 658d2be
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 41 deletions.
8 changes: 6 additions & 2 deletions src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tracing::instrument;

use super::config::initialize_configuration;
use crate::bootstrap;
use crate::core::services::tracker_factory;
use crate::core::services::{initialize_database, initialize_whitelist, tracker_factory};
use crate::core::Tracker;
use crate::servers::udp::server::banning::BanService;
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
Expand Down Expand Up @@ -105,7 +105,11 @@ pub fn initialize_static() {
#[must_use]
#[instrument(skip(config))]
pub fn initialize_tracker(config: &Configuration) -> Tracker {
tracker_factory(config)
let database = initialize_database(config);

let whitelist_manager = initialize_whitelist(database.clone());

tracker_factory(config, &database, &whitelist_manager)
}

/// It initializes the log threshold, format and channel.
Expand Down
25 changes: 18 additions & 7 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,25 +1154,36 @@ mod tests {
use torrust_tracker_test_helpers::configuration;

use crate::core::peer::Peer;
use crate::core::services::tracker_factory;
use crate::core::services::{initialize_database, initialize_whitelist, tracker_factory};
use crate::core::{TorrentsMetrics, Tracker};

fn public_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_public())
let config = configuration::ephemeral_public();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn private_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_private())
let config = configuration::ephemeral_private();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn whitelisted_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_listed())
let config = configuration::ephemeral_listed();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

pub fn tracker_persisting_torrents_in_database() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.core.tracker_policy.persistent_torrent_completed_stat = true;
tracker_factory(&configuration)
let mut config = configuration::ephemeral_listed();
config.core.tracker_policy.persistent_torrent_completed_stat = true;
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn sample_info_hash() -> InfoHash {
Expand Down
14 changes: 7 additions & 7 deletions src/core/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ use crate::core::Tracker;
///
/// Will panic if tracker cannot be instantiated.
#[must_use]
pub fn tracker_factory(config: &Configuration) -> Tracker {
let database = initialize_database(config);

let whitelist_manager = initialize_whitelist(database.clone());

pub fn tracker_factory(
config: &Configuration,
database: &Arc<Box<dyn Database>>,
whitelist_manager: &Arc<WhiteListManager>,
) -> Tracker {
let (stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);

match Tracker::new(
&Arc::new(config).core,
&database,
&whitelist_manager,
database,
whitelist_manager,
stats_event_sender,
stats_repository,
) {
Expand Down
7 changes: 5 additions & 2 deletions src/core/services/statistics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ mod tests {

use crate::core;
use crate::core::services::statistics::{get_metrics, TrackerMetrics};
use crate::core::services::tracker_factory;
use crate::core::services::{initialize_database, initialize_whitelist, tracker_factory};
use crate::servers::udp::server::banning::BanService;
use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;

Expand All @@ -124,7 +124,10 @@ mod tests {

#[tokio::test]
async fn the_statistics_service_should_return_the_tracker_metrics() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let config = tracker_configuration();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
let tracker = Arc::new(tracker_factory(&tracker_configuration(), &database, &whitelist_manager));
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));

let tracker_metrics = get_metrics(tracker.clone(), ban_service.clone()).await;
Expand Down
39 changes: 30 additions & 9 deletions src/core/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,18 @@ mod tests {

use crate::core::services::torrent::tests::sample_peer;
use crate::core::services::torrent::{get_torrent_info, Info};
use crate::core::services::tracker_factory;
use crate::core::services::{initialize_database, initialize_whitelist, tracker_factory};

pub fn tracker_configuration() -> Configuration {
configuration::ephemeral()
}

#[tokio::test]
async fn should_return_none_if_the_tracker_does_not_have_the_torrent() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let config = tracker_configuration();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));

let torrent_info = get_torrent_info(
tracker.clone(),
Expand All @@ -152,7 +155,10 @@ mod tests {

#[tokio::test]
async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let config = tracker_configuration();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
Expand Down Expand Up @@ -184,15 +190,18 @@ mod tests {

use crate::core::services::torrent::tests::sample_peer;
use crate::core::services::torrent::{get_torrents_page, BasicInfo, Pagination};
use crate::core::services::tracker_factory;
use crate::core::services::{initialize_database, initialize_whitelist, tracker_factory};

pub fn tracker_configuration() -> Configuration {
configuration::ephemeral()
}

#[tokio::test]
async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let config = tracker_configuration();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));

let torrents = get_torrents_page(tracker.clone(), Some(&Pagination::default())).await;

Expand All @@ -201,7 +210,10 @@ mod tests {

#[tokio::test]
async fn should_return_a_summarized_info_for_all_torrents() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let config = tracker_configuration();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
Expand All @@ -223,7 +235,10 @@ mod tests {

#[tokio::test]
async fn should_allow_limiting_the_number_of_torrents_in_the_result() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let config = tracker_configuration();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand All @@ -243,7 +258,10 @@ mod tests {

#[tokio::test]
async fn should_allow_using_pagination_in_the_result() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let config = tracker_configuration();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand Down Expand Up @@ -272,7 +290,10 @@ mod tests {

#[tokio::test]
async fn should_return_torrents_ordered_by_info_hash() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));
let config = tracker_configuration();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand Down
22 changes: 17 additions & 5 deletions src/servers/http/v1/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,35 @@ mod tests {
use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_test_helpers::configuration;

use crate::core::services::tracker_factory;
use crate::core::services::{initialize_database, initialize_whitelist, tracker_factory};
use crate::core::Tracker;

fn private_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_private())
let config = configuration::ephemeral_private();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn whitelisted_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_listed())
let config = configuration::ephemeral_listed();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn tracker_on_reverse_proxy() -> Tracker {
tracker_factory(&configuration::ephemeral_with_reverse_proxy())
let config = configuration::ephemeral_with_reverse_proxy();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn tracker_not_on_reverse_proxy() -> Tracker {
tracker_factory(&configuration::ephemeral_without_reverse_proxy())
let config = configuration::ephemeral_without_reverse_proxy();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn sample_announce_request() -> Announce {
Expand Down
22 changes: 17 additions & 5 deletions src/servers/http/v1/handlers/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,35 @@ mod tests {
use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_test_helpers::configuration;

use crate::core::services::tracker_factory;
use crate::core::services::{initialize_database, initialize_whitelist, tracker_factory};
use crate::core::Tracker;

fn private_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_private())
let config = configuration::ephemeral_private();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn whitelisted_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_listed())
let config = configuration::ephemeral_listed();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn tracker_on_reverse_proxy() -> Tracker {
tracker_factory(&configuration::ephemeral_with_reverse_proxy())
let config = configuration::ephemeral_with_reverse_proxy();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn tracker_not_on_reverse_proxy() -> Tracker {
tracker_factory(&configuration::ephemeral_without_reverse_proxy())
let config = configuration::ephemeral_without_reverse_proxy();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn sample_scrape_request() -> Scrape {
Expand Down
7 changes: 5 additions & 2 deletions src/servers/http/v1/services/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ mod tests {
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
use torrust_tracker_test_helpers::configuration;

use crate::core::services::tracker_factory;
use crate::core::services::{initialize_database, initialize_whitelist, tracker_factory};
use crate::core::Tracker;

fn public_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_public())
let config = configuration::ephemeral_public();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn sample_info_hash() -> InfoHash {
Expand Down
5 changes: 4 additions & 1 deletion src/servers/http/v1/services/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ mod tests {
use crate::core::{statistics, Tracker};

fn public_tracker() -> Tracker {
tracker_factory(&configuration::ephemeral_public())
let config = configuration::ephemeral_public();
let database = initialize_database(&config);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(&config, &database, &whitelist_manager)
}

fn sample_info_hashes() -> Vec<InfoHash> {
Expand Down
4 changes: 3 additions & 1 deletion src/servers/udp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,9 @@ mod tests {
}

fn initialized_tracker(configuration: &Configuration) -> Arc<Tracker> {
tracker_factory(configuration).into()
let database = initialize_database(configuration);
let whitelist_manager = initialize_whitelist(database.clone());
tracker_factory(configuration, &database, &whitelist_manager).into()
}

fn sample_ipv4_remote_addr() -> SocketAddr {
Expand Down

0 comments on commit 658d2be

Please sign in to comment.