9898//!
9999//! ```rust,no_run
100100//! use torrust_tracker::core::peer::Peer;
101+ //! use torrust_tracker::core::AnnouncePolicy;
101102//!
102103//! pub struct AnnounceData {
103104//! pub peers: Vec<Peer>,
104105//! pub swarm_stats: SwarmStats,
105- //! pub interval: u32, // Option `announce_interval` from core tracker configuration
106- //! pub interval_min: u32, // Option `min_announce_interval` from core tracker configuration
106+ //! pub policy: AnnouncePolicy, // the tracker announce policy.
107107//! }
108108//!
109109//! pub struct SwarmStats {
@@ -445,6 +445,7 @@ use std::panic::Location;
445445use std:: sync:: Arc ;
446446use std:: time:: Duration ;
447447
448+ use derive_more:: Constructor ;
448449use futures:: future:: join_all;
449450use tokio:: sync:: mpsc:: error:: SendError ;
450451use torrust_tracker_configuration:: Configuration ;
@@ -487,7 +488,7 @@ pub struct Tracker {
487488/// Structure that holds general `Tracker` torrents metrics.
488489///
489490/// Metrics are aggregate values for all torrents.
490- #[ derive( Debug , PartialEq , Default ) ]
491+ #[ derive( Copy , Clone , Debug , PartialEq , Default ) ]
491492pub struct TorrentsMetrics {
492493 /// Total number of seeders for all torrents
493494 pub seeders : u64 ,
@@ -499,21 +500,45 @@ pub struct TorrentsMetrics {
499500 pub torrents : u64 ,
500501}
501502
503+ /// Announce policy
504+ #[ derive( Copy , Clone , Debug , PartialEq , Constructor ) ]
505+ pub struct AnnouncePolicy {
506+ /// Interval in seconds that the client should wait between sending regular
507+ /// announce requests to the tracker.
508+ ///
509+ /// It's a **recommended** wait time between announcements.
510+ ///
511+ /// This is the standard amount of time that clients should wait between
512+ /// sending consecutive announcements to the tracker. This value is set by
513+ /// the tracker and is typically provided in the tracker's response to a
514+ /// client's initial request. It serves as a guideline for clients to know
515+ /// how often they should contact the tracker for updates on the peer list,
516+ /// while ensuring that the tracker is not overwhelmed with requests.
517+ pub interval : u32 ,
518+
519+ /// Minimum announce interval. Clients must not reannounce more frequently
520+ /// than this.
521+ ///
522+ /// It establishes the shortest allowed wait time.
523+ ///
524+ /// This is an optional parameter in the protocol that the tracker may
525+ /// provide in its response. It sets a lower limit on the frequency at which
526+ /// clients are allowed to send announcements. Clients should respect this
527+ /// value to prevent sending too many requests in a short period, which
528+ /// could lead to excessive load on the tracker or even getting banned by
529+ /// the tracker for not adhering to the rules.
530+ pub interval_min : u32 ,
531+ }
532+
502533/// Structure that holds the data returned by the `announce` request.
503- #[ derive( Debug , PartialEq , Default ) ]
534+ #[ derive( Clone , Debug , PartialEq , Constructor ) ]
504535pub struct AnnounceData {
505536 /// The list of peers that are downloading the same torrent.
506537 /// It excludes the peer that made the request.
507538 pub peers : Vec < Peer > ,
508539 /// Swarm statistics
509540 pub swarm_stats : SwarmStats ,
510- /// The interval in seconds that the client should wait between sending
511- /// regular requests to the tracker.
512- /// Refer to [`announce_interval`](torrust_tracker_configuration::Configuration::announce_interval).
513- pub interval : u32 ,
514- /// The minimum announce interval in seconds that the client should wait.
515- /// Refer to [`min_announce_interval`](torrust_tracker_configuration::Configuration::min_announce_interval).
516- pub interval_min : u32 ,
541+ pub policy : AnnouncePolicy ,
517542}
518543
519544/// Structure that holds the data returned by the `scrape` request.
@@ -631,8 +656,7 @@ impl Tracker {
631656 AnnounceData {
632657 peers,
633658 swarm_stats,
634- interval : self . config . announce_interval ,
635- interval_min : self . config . min_announce_interval ,
659+ policy : AnnouncePolicy :: new ( self . config . announce_interval , self . config . min_announce_interval ) ,
636660 }
637661 }
638662
@@ -732,7 +756,7 @@ impl Tracker {
732756 let ( stats, stats_updated) = self . torrents . update_torrent_with_peer_and_get_stats ( info_hash, peer) . await ;
733757
734758 if self . config . persistent_torrent_completed_stat && stats_updated {
735- let completed = stats. downloaded ;
759+ let completed = stats. downloaded ; // i.e `completed`
736760 let info_hash = * info_hash;
737761
738762 drop ( self . database . save_persistent_torrent ( & info_hash, completed) . await ) ;
0 commit comments