Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul core Tracker: extract torrents context (part 1) #1202

Merged
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub async fn start(config: &Configuration, app_container: &AppContainer) -> Vec<

// Start runners to remove torrents without peers, every interval
if config.core.inactive_peer_cleanup_interval > 0 {
jobs.push(torrent_cleanup::start_job(&config.core, &app_container.tracker));
jobs.push(torrent_cleanup::start_job(&config.core, &app_container.torrents_manager));
}

// Start Health Check API
Expand Down
23 changes: 22 additions & 1 deletion src/app_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use crate::core::authentication::key::repository::persisted::DatabaseKeyReposito
use crate::core::authentication::service::{self, AuthenticationService};
use crate::core::databases::Database;
use crate::core::services::initialize_database;
use crate::core::torrent::manager::TorrentsManager;
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
use crate::core::whitelist;
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;

Expand All @@ -22,6 +25,9 @@ pub fn initialize_tracker_dependencies(
Arc<InMemoryWhitelist>,
Arc<whitelist::authorization::Authorization>,
Arc<AuthenticationService>,
Arc<InMemoryTorrentRepository>,
Arc<DatabasePersistentTorrentRepository>,
Arc<TorrentsManager>,
) {
let database = initialize_database(config);
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
Expand All @@ -36,6 +42,21 @@ pub fn initialize_tracker_dependencies(
&db_key_repository.clone(),
&in_memory_key_repository.clone(),
));
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
let torrents_manager = Arc::new(TorrentsManager::new(
&config.core,
&in_memory_torrent_repository,
&db_torrent_repository,
));

(database, in_memory_whitelist, whitelist_authorization, authentication_service)
(
database,
in_memory_whitelist,
whitelist_authorization,
authentication_service,
in_memory_torrent_repository,
db_torrent_repository,
torrents_manager,
)
}
21 changes: 20 additions & 1 deletion src/bootstrap/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use crate::core::authentication::key::repository::in_memory::InMemoryKeyReposito
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
use crate::core::authentication::service;
use crate::core::services::{initialize_database, initialize_tracker, initialize_whitelist_manager, statistics};
use crate::core::torrent::manager::TorrentsManager;
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
use crate::core::whitelist;
use crate::core::whitelist::repository::in_memory::InMemoryWhitelist;
use crate::servers::udp::server::banning::BanService;
Expand Down Expand Up @@ -103,10 +106,23 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
&db_key_repository.clone(),
&in_memory_key_repository.clone(),
));
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
let torrents_manager = Arc::new(TorrentsManager::new(
&configuration.core,
&in_memory_torrent_repository,
&db_torrent_repository,
));

let tracker = Arc::new(initialize_tracker(configuration, &database, &whitelist_authorization));
let tracker = Arc::new(initialize_tracker(
configuration,
&whitelist_authorization,
&in_memory_torrent_repository,
&db_torrent_repository,
));

AppContainer {
database,
tracker,
keys_handler,
authentication_service,
Expand All @@ -115,6 +131,9 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
stats_event_sender,
stats_repository,
whitelist_manager,
in_memory_torrent_repository,
db_torrent_repository,
torrents_manager,
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/bootstrap/jobs/torrent_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ use tokio::task::JoinHandle;
use torrust_tracker_configuration::Core;
use tracing::instrument;

use crate::core;
use crate::core::torrent::manager::TorrentsManager;

/// It starts a jobs for cleaning up the torrent data in the tracker.
///
/// The cleaning task is executed on an `inactive_peer_cleanup_interval`.
///
/// Refer to [`torrust-tracker-configuration documentation`](https://docs.rs/torrust-tracker-configuration) for more info about that option.
#[must_use]
#[instrument(skip(config, tracker))]
pub fn start_job(config: &Core, tracker: &Arc<core::Tracker>) -> JoinHandle<()> {
let weak_tracker = std::sync::Arc::downgrade(tracker);
#[instrument(skip(config, torrents_manager))]
pub fn start_job(config: &Core, torrents_manager: &Arc<TorrentsManager>) -> JoinHandle<()> {
let weak_torrents_manager = std::sync::Arc::downgrade(torrents_manager);
let interval = config.inactive_peer_cleanup_interval;

tokio::spawn(async move {
Expand All @@ -42,10 +42,10 @@ pub fn start_job(config: &Core, tracker: &Arc<core::Tracker>) -> JoinHandle<()>
break;
}
_ = interval.tick() => {
if let Some(tracker) = weak_tracker.upgrade() {
if let Some(torrents_manager) = weak_torrents_manager.upgrade() {
let start_time = Utc::now().time();
tracing::info!("Cleaning up torrents..");
tracker.cleanup_torrents();
torrents_manager.cleanup_torrents();
tracing::info!("Cleaned up torrents in: {}ms", (Utc::now().time() - start_time).num_milliseconds());
} else {
break;
Expand Down
8 changes: 8 additions & 0 deletions src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ use tokio::sync::RwLock;

use crate::core::authentication::handler::KeysHandler;
use crate::core::authentication::service::AuthenticationService;
use crate::core::databases::Database;
use crate::core::statistics::event::sender::Sender;
use crate::core::statistics::repository::Repository;
use crate::core::torrent::manager::TorrentsManager;
use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository;
use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
use crate::core::whitelist::manager::WhiteListManager;
use crate::core::{whitelist, Tracker};
use crate::servers::udp::server::banning::BanService;

pub struct AppContainer {
pub database: Arc<Box<dyn Database>>,
pub tracker: Arc<Tracker>,
pub keys_handler: Arc<KeysHandler>,
pub authentication_service: Arc<AuthenticationService>,
Expand All @@ -19,4 +24,7 @@ pub struct AppContainer {
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
pub stats_repository: Arc<Repository>,
pub whitelist_manager: Arc<WhiteListManager>,
pub in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
pub db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
pub torrents_manager: Arc<TorrentsManager>,
}
Loading
Loading