@@ -451,20 +451,18 @@ pub mod peer_tests;
451
451
452
452
use std:: net:: IpAddr ;
453
453
use std:: sync:: Arc ;
454
- use std:: time:: Duration ;
455
454
456
455
use bittorrent_primitives:: info_hash:: InfoHash ;
456
+ use torrent:: manager:: TorrentsManager ;
457
457
use torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
458
458
use torrent:: repository:: persisted:: DatabasePersistentTorrentRepository ;
459
- use torrust_tracker_clock:: clock:: Time ;
460
459
use torrust_tracker_configuration:: { AnnouncePolicy , Core , TORRENT_PEERS_LIMIT } ;
461
460
use torrust_tracker_primitives:: core:: { AnnounceData , ScrapeData } ;
462
461
use torrust_tracker_primitives:: peer;
463
462
use torrust_tracker_primitives:: swarm_metadata:: SwarmMetadata ;
464
463
use torrust_tracker_primitives:: torrent_metrics:: TorrentsMetrics ;
465
464
466
465
use crate :: core:: databases:: Database ;
467
- use crate :: CurrentClock ;
468
466
469
467
/// The domain layer tracker service.
470
468
///
@@ -491,6 +489,9 @@ pub struct Tracker {
491
489
492
490
/// The persistent torrents repository.
493
491
db_torrent_repository : Arc < DatabasePersistentTorrentRepository > ,
492
+
493
+ /// The service to run torrents tasks.
494
+ torrents_manager : Arc < TorrentsManager > ,
494
495
}
495
496
496
497
/// How many peers the peer announcing wants in the announce response.
@@ -546,12 +547,20 @@ impl Tracker {
546
547
database : & Arc < Box < dyn Database > > ,
547
548
whitelist_authorization : & Arc < whitelist:: authorization:: Authorization > ,
548
549
) -> Result < Tracker , databases:: error:: Error > {
550
+ let in_memory_torrent_repository = Arc :: new ( InMemoryTorrentRepository :: default ( ) ) ;
551
+ let db_torrent_repository = Arc :: new ( DatabasePersistentTorrentRepository :: new ( database) ) ;
552
+
549
553
Ok ( Tracker {
550
554
config : config. clone ( ) ,
551
555
database : database. clone ( ) ,
552
556
whitelist_authorization : whitelist_authorization. clone ( ) ,
553
- in_memory_torrent_repository : Arc :: new ( InMemoryTorrentRepository :: default ( ) ) ,
554
- db_torrent_repository : Arc :: new ( DatabasePersistentTorrentRepository :: new ( database) ) ,
557
+ in_memory_torrent_repository : in_memory_torrent_repository. clone ( ) ,
558
+ db_torrent_repository : db_torrent_repository. clone ( ) ,
559
+ torrents_manager : Arc :: new ( TorrentsManager :: new (
560
+ config,
561
+ & in_memory_torrent_repository,
562
+ & db_torrent_repository,
563
+ ) ) ,
555
564
} )
556
565
}
557
566
@@ -670,11 +679,7 @@ impl Tracker {
670
679
///
671
680
/// Will return a `database::Error` if unable to load the list of `persistent_torrents` from the database.
672
681
pub fn load_torrents_from_database ( & self ) -> Result < ( ) , databases:: error:: Error > {
673
- let persistent_torrents = self . db_torrent_repository . load_all ( ) ?;
674
-
675
- self . in_memory_torrent_repository . import_persistent ( & persistent_torrents) ;
676
-
677
- Ok ( ( ) )
682
+ self . torrents_manager . load_torrents_from_database ( )
678
683
}
679
684
680
685
/// # Context: Tracker
@@ -748,15 +753,7 @@ impl Tracker {
748
753
///
749
754
/// # Context: Tracker
750
755
pub fn cleanup_torrents ( & self ) {
751
- let current_cutoff = CurrentClock :: now_sub ( & Duration :: from_secs ( u64:: from ( self . config . tracker_policy . max_peer_timeout ) ) )
752
- . unwrap_or_default ( ) ;
753
-
754
- self . in_memory_torrent_repository . remove_inactive_peers ( current_cutoff) ;
755
-
756
- if self . config . tracker_policy . remove_peerless_torrents {
757
- self . in_memory_torrent_repository
758
- . remove_peerless_torrents ( & self . config . tracker_policy ) ;
759
- }
756
+ self . torrents_manager . cleanup_torrents ( ) ;
760
757
}
761
758
762
759
/// It drops the database tables.
0 commit comments