Skip to content

Commit

Permalink
refactor: [#1203] use directly the InMemoryTorrentRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 24, 2025
1 parent 0f1b2fb commit 046578d
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 139 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<
if let Some(http_api_config) = &config.http_api {
if let Some(job) = tracker_apis::start_job(
http_api_config,
app_container.tracker.clone(),
app_container.in_memory_torrent_repository.clone(),
app_container.keys_handler.clone(),
app_container.whitelist_manager.clone(),
app_container.ban_service.clone(),
Expand Down
14 changes: 6 additions & 8 deletions src/bootstrap/jobs/tracker_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use super::make_rust_tls;
use crate::core::authentication::handler::KeysHandler;
use crate::core::statistics::event::sender::Sender;
use crate::core::statistics::repository::Repository;
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
use crate::core::whitelist::manager::WhiteListManager;
use crate::core::{self};
use crate::servers::apis::server::{ApiServer, Launcher};
use crate::servers::apis::Version;
use crate::servers::registar::ServiceRegistrationForm;
Expand Down Expand Up @@ -63,7 +63,6 @@ pub struct ApiServerJobStarted();
#[allow(clippy::too_many_arguments)]
#[instrument(skip(
config,
tracker,
keys_handler,
whitelist_manager,
ban_service,
Expand All @@ -73,7 +72,7 @@ pub struct ApiServerJobStarted();
))]
pub async fn start_job(
config: &HttpApi,
tracker: Arc<core::Tracker>,
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
keys_handler: Arc<KeysHandler>,
whitelist_manager: Arc<WhiteListManager>,
ban_service: Arc<RwLock<BanService>>,
Expand All @@ -95,7 +94,7 @@ pub async fn start_job(
start_v1(
bind_to,
tls,
tracker.clone(),
in_memory_torrent_repository.clone(),
keys_handler.clone(),
whitelist_manager.clone(),
ban_service.clone(),
Expand All @@ -114,7 +113,6 @@ pub async fn start_job(
#[instrument(skip(
socket,
tls,
tracker,
keys_handler,
whitelist_manager,
ban_service,
Expand All @@ -126,7 +124,7 @@ pub async fn start_job(
async fn start_v1(
socket: SocketAddr,
tls: Option<RustlsConfig>,
tracker: Arc<core::Tracker>,
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
keys_handler: Arc<KeysHandler>,
whitelist_manager: Arc<WhiteListManager>,
ban_service: Arc<RwLock<BanService>>,
Expand All @@ -137,7 +135,7 @@ async fn start_v1(
) -> JoinHandle<()> {
let server = ApiServer::new(Launcher::new(socket, tls))
.start(
tracker,
in_memory_torrent_repository,
keys_handler,
whitelist_manager,
stats_event_sender,
Expand Down Expand Up @@ -179,7 +177,7 @@ mod tests {

start_job(
config,
app_container.tracker,
app_container.in_memory_torrent_repository,
app_container.keys_handler,
app_container.whitelist_manager,
app_container.ban_service,
Expand Down
12 changes: 6 additions & 6 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,10 @@ pub struct Tracker {
config: Core,

/// The service to check is a torrent is whitelisted.
pub whitelist_authorization: Arc<whitelist::authorization::Authorization>,
whitelist_authorization: Arc<whitelist::authorization::Authorization>,

/// The in-memory torrents repository.
pub in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,

/// The persistent torrents repository.
db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
Expand Down Expand Up @@ -1325,24 +1325,24 @@ mod tests {

#[tokio::test]
async fn it_should_authorize_the_announce_and_scrape_actions_on_whitelisted_torrents() {
let (tracker, _whitelist_authorization, whitelist_manager) = whitelisted_tracker();
let (_tracker, whitelist_authorization, whitelist_manager) = whitelisted_tracker();

let info_hash = sample_info_hash();

let result = whitelist_manager.add_torrent_to_whitelist(&info_hash).await;
assert!(result.is_ok());

let result = tracker.whitelist_authorization.authorize(&info_hash).await;
let result = whitelist_authorization.authorize(&info_hash).await;
assert!(result.is_ok());
}

#[tokio::test]
async fn it_should_not_authorize_the_announce_and_scrape_actions_on_not_whitelisted_torrents() {
let (tracker, _whitelist_authorization, _whitelist_manager) = whitelisted_tracker();
let (_tracker, whitelist_authorization, _whitelist_manager) = whitelisted_tracker();

let info_hash = sample_info_hash();

let result = tracker.whitelist_authorization.authorize(&info_hash).await;
let result = whitelist_authorization.authorize(&info_hash).await;
assert!(result.is_err());
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/core/services/statistics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;

use crate::core::statistics::metrics::Metrics;
use crate::core::statistics::repository::Repository;
use crate::core::Tracker;
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
use crate::servers::udp::server::banning::BanService;

/// All the metrics collected by the tracker.
Expand All @@ -64,11 +64,11 @@ pub struct TrackerMetrics {

/// It returns all the [`TrackerMetrics`]
pub async fn get_metrics(
tracker: Arc<Tracker>,
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
ban_service: Arc<RwLock<BanService>>,
stats_repository: Arc<Repository>,
) -> TrackerMetrics {
let torrents_metrics = tracker.in_memory_torrent_repository.get_torrents_metrics();
let torrents_metrics = in_memory_torrent_repository.get_torrents_metrics();
let stats = stats_repository.get_stats().await;
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();

Expand Down Expand Up @@ -145,7 +145,7 @@ mod tests {
let (_stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
let stats_repository = Arc::new(stats_repository);

let tracker = Arc::new(initialize_tracker(
let _tracker = Arc::new(initialize_tracker(
&config,
&whitelist_authorization,
&in_memory_torrent_repository,
Expand All @@ -154,7 +154,12 @@ mod tests {

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(), stats_repository.clone()).await;
let tracker_metrics = get_metrics(
in_memory_torrent_repository.clone(),
ban_service.clone(),
stats_repository.clone(),
)
.await;

assert_eq!(
tracker_metrics,
Expand Down
Loading

0 comments on commit 046578d

Please sign in to comment.