Skip to content

Commit

Permalink
refactor: [#1184] inject stats event sender and repository into the c…
Browse files Browse the repository at this point in the history
…ore tracker

This is part of a big refactor. We are extracting responsabilities from
the tracker. The first step is to inject the service into the tracker
and later we will use the extracted services directly. Finally we will
removed the injected service from the tracker when it's not used anymore
via the tracker.
  • Loading branch information
josecelano committed Jan 16, 2025
1 parent df924a0 commit c415430
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 71 deletions.
23 changes: 18 additions & 5 deletions src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ use tracing::instrument;
use super::config::initialize_configuration;
use crate::bootstrap;
use crate::core::databases::Database;
use crate::core::services::{initialize_database, initialize_whitelist, tracker_factory};
use crate::core::services::{initialize_database, initialize_whitelist, statistics, tracker_factory};
use crate::core::statistics::event::sender::Sender;
use crate::core::statistics::repository::Repository;
use crate::core::whitelist::WhiteListManager;
use crate::core::Tracker;
use crate::servers::udp::server::banning::BanService;
Expand Down Expand Up @@ -107,17 +109,28 @@ pub fn initialize_static() {
#[must_use]
#[instrument(skip(config))]
pub fn initialize_tracker(config: &Configuration) -> Tracker {
let (database, whitelist_manager) = initialize_tracker_dependencies(config);
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(config);

tracker_factory(config, &database, &whitelist_manager)
tracker_factory(config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

#[allow(clippy::type_complexity)]
#[must_use]
pub fn initialize_tracker_dependencies(config: &Configuration) -> (Arc<Box<dyn Database>>, Arc<WhiteListManager>) {
pub fn initialize_tracker_dependencies(
config: &Configuration,
) -> (
Arc<Box<dyn Database>>,
Arc<WhiteListManager>,
Arc<Option<Box<dyn Sender>>>,
Arc<Repository>,
) {
let database = initialize_database(config);
let whitelist_manager = initialize_whitelist(database.clone());
let (stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
let stats_event_sender = Arc::new(stats_event_sender);
let stats_repository = Arc::new(stats_repository);

(database, whitelist_manager)
(database, whitelist_manager, stats_event_sender, stats_repository)
}

/// It initializes the log threshold, format and channel.
Expand Down
30 changes: 15 additions & 15 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,10 @@ pub struct Tracker {
torrents: Arc<Torrents>,

/// Service to send stats events.
stats_event_sender: Option<Box<dyn statistics::event::sender::Sender>>,
stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,

/// The in-memory stats repo.
stats_repository: statistics::repository::Repository,
stats_repository: Arc<statistics::repository::Repository>,
}

/// How many peers the peer announcing wants in the announce response.
Expand Down Expand Up @@ -576,17 +576,17 @@ impl Tracker {
config: &Core,
database: &Arc<Box<dyn Database>>,
whitelist_manager: &Arc<WhiteListManager>,
stats_event_sender: Option<Box<dyn statistics::event::sender::Sender>>,
stats_repository: statistics::repository::Repository,
stats_event_sender: &Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
stats_repository: &Arc<statistics::repository::Repository>,
) -> Result<Tracker, databases::error::Error> {
Ok(Tracker {
config: config.clone(),
database: database.clone(),
keys: tokio::sync::RwLock::new(std::collections::HashMap::new()),
whitelist_manager: whitelist_manager.clone(),
torrents: Arc::default(),
stats_event_sender,
stats_repository,
stats_event_sender: stats_event_sender.clone(),
stats_repository: stats_repository.clone(),
})
}

Expand Down Expand Up @@ -1068,7 +1068,7 @@ impl Tracker {
&self,
event: statistics::event::Event,
) -> Option<Result<(), SendError<statistics::event::Event>>> {
match &self.stats_event_sender {
match &*self.stats_event_sender {
None => None,
Some(stats_event_sender) => stats_event_sender.send_event(event).await,
}
Expand Down Expand Up @@ -1119,29 +1119,29 @@ mod tests {

fn public_tracker() -> Tracker {
let config = configuration::ephemeral_public();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn private_tracker() -> Tracker {
let config = configuration::ephemeral_private();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn whitelisted_tracker() -> (Tracker, Arc<WhiteListManager>) {
let config = configuration::ephemeral_listed();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let tracker = tracker_factory(&config, &database, &whitelist_manager);
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
let tracker = tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository);

(tracker, whitelist_manager)
}

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

fn sample_info_hash() -> InfoHash {
Expand Down
6 changes: 5 additions & 1 deletion src/core/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use torrust_tracker_configuration::v2_0_0::database;
use torrust_tracker_configuration::Configuration;

use super::databases::{self, Database};
use super::statistics::event::sender::Sender;
use super::statistics::repository::Repository;
use super::whitelist::persisted::DatabaseWhitelist;
use super::whitelist::WhiteListManager;
use crate::core::Tracker;
Expand All @@ -28,8 +30,10 @@ pub fn tracker_factory(
config: &Configuration,
database: &Arc<Box<dyn Database>>,
whitelist_manager: &Arc<WhiteListManager>,
stats_event_sender: &Arc<Option<Box<dyn Sender>>>,
stats_repository: &Arc<Repository>,
) -> Tracker {
let (stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
//let (stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);

match Tracker::new(
&Arc::new(config).core,
Expand Down
12 changes: 10 additions & 2 deletions src/core/services/statistics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,16 @@ mod tests {
#[tokio::test]
async fn the_statistics_service_should_return_the_tracker_metrics() {
let config = tracker_configuration();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(&tracker_configuration(), &database, &whitelist_manager));

let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(
&config,
&database,
&whitelist_manager,
&stats_event_sender,
&stats_repository,
));

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
67 changes: 53 additions & 14 deletions src/core/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ mod tests {
#[tokio::test]
async fn should_return_none_if_the_tracker_does_not_have_the_torrent() {
let config = tracker_configuration();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));

let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
let tracker = tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository);

let tracker = Arc::new(tracker);

let torrent_info = get_torrent_info(
tracker.clone(),
Expand All @@ -156,8 +159,14 @@ mod tests {
#[tokio::test]
async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() {
let config = tracker_configuration();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(
&config,
&database,
&whitelist_manager,
&stats_event_sender,
&stats_repository,
));

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
Expand Down Expand Up @@ -199,8 +208,14 @@ mod tests {
#[tokio::test]
async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() {
let config = tracker_configuration();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(
&config,
&database,
&whitelist_manager,
&stats_event_sender,
&stats_repository,
));

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

Expand All @@ -210,8 +225,14 @@ mod tests {
#[tokio::test]
async fn should_return_a_summarized_info_for_all_torrents() {
let config = tracker_configuration();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(
&config,
&database,
&whitelist_manager,
&stats_event_sender,
&stats_repository,
));

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
Expand All @@ -234,8 +255,14 @@ mod tests {
#[tokio::test]
async fn should_allow_limiting_the_number_of_torrents_in_the_result() {
let config = tracker_configuration();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(
&config,
&database,
&whitelist_manager,
&stats_event_sender,
&stats_repository,
));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand All @@ -256,8 +283,14 @@ mod tests {
#[tokio::test]
async fn should_allow_using_pagination_in_the_result() {
let config = tracker_configuration();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(
&config,
&database,
&whitelist_manager,
&stats_event_sender,
&stats_repository,
));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand Down Expand Up @@ -287,8 +320,14 @@ mod tests {
#[tokio::test]
async fn should_return_torrents_ordered_by_info_hash() {
let config = tracker_configuration();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(&config, &database, &whitelist_manager));
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
let tracker = Arc::new(tracker_factory(
&config,
&database,
&whitelist_manager,
&stats_event_sender,
&stats_repository,
));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
Expand Down
16 changes: 8 additions & 8 deletions src/servers/http/v1/handlers/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,26 @@ mod tests {

fn private_tracker() -> Tracker {
let config = configuration::ephemeral_private();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn whitelisted_tracker() -> Tracker {
let config = configuration::ephemeral_listed();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn tracker_on_reverse_proxy() -> Tracker {
let config = configuration::ephemeral_with_reverse_proxy();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn tracker_not_on_reverse_proxy() -> Tracker {
let config = configuration::ephemeral_without_reverse_proxy();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn sample_announce_request() -> Announce {
Expand Down
16 changes: 8 additions & 8 deletions src/servers/http/v1/handlers/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,26 @@ mod tests {

fn private_tracker() -> Tracker {
let config = configuration::ephemeral_private();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn whitelisted_tracker() -> Tracker {
let config = configuration::ephemeral_listed();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn tracker_on_reverse_proxy() -> Tracker {
let config = configuration::ephemeral_with_reverse_proxy();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn tracker_not_on_reverse_proxy() -> Tracker {
let config = configuration::ephemeral_without_reverse_proxy();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn sample_scrape_request() -> Scrape {
Expand Down
14 changes: 9 additions & 5 deletions src/servers/http/v1/services/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ mod tests {

fn public_tracker() -> Tracker {
let config = configuration::ephemeral_public();
let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager)
let (database, whitelist_manager, stats_event_sender, stats_repository) = initialize_tracker_dependencies(&config);
tracker_factory(&config, &database, &whitelist_manager, &stats_event_sender, &stats_repository)
}

fn sample_info_hash() -> InfoHash {
Expand Down Expand Up @@ -118,14 +118,18 @@ mod tests {
fn test_tracker_factory(stats_event_sender: Option<Box<dyn statistics::event::sender::Sender>>) -> Tracker {
let config = configuration::ephemeral();

let (database, whitelist_manager) = initialize_tracker_dependencies(&config);
let (database, whitelist_manager, _stats_event_sender, _stats_repository) = initialize_tracker_dependencies(&config);

let stats_event_sender = Arc::new(stats_event_sender);

let stats_repository = Arc::new(statistics::repository::Repository::new());

Tracker::new(
&config.core,
&database,
&whitelist_manager,
stats_event_sender,
statistics::repository::Repository::new(),
&stats_event_sender,
&stats_repository,
)
.unwrap()
}
Expand Down
Loading

0 comments on commit c415430

Please sign in to comment.