Skip to content

Commit 374ede2

Browse files
committed
dev: cleanup http responses: announce
1 parent 1316090 commit 374ede2

File tree

8 files changed

+372
-402
lines changed

8 files changed

+372
-402
lines changed

src/core/databases/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub trait Database: Sync + Send {
134134
/// # Errors
135135
///
136136
/// Will return `Err` if unable to save.
137-
async fn save_persistent_torrent(&self, info_hash: &InfoHash, completed: u32) -> Result<(), Error>;
137+
async fn save_persistent_torrent(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error>;
138138

139139
// Whitelist
140140

src/core/mod.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@
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;
445445
use std::sync::Arc;
446446
use std::time::Duration;
447447

448+
use derive_more::Constructor;
448449
use futures::future::join_all;
449450
use tokio::sync::mpsc::error::SendError;
450451
use 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)]
491492
pub 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)]
504535
pub 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);

src/core/torrent/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub mod repository;
3333
use std::time::Duration;
3434

3535
use aquatic_udp_protocol::AnnounceEvent;
36+
use derive_more::Constructor;
3637
use serde::{Deserialize, Serialize};
3738

3839
use super::peer::{self, Peer};
@@ -56,13 +57,13 @@ pub struct Entry {
5657
/// Swarm metadata dictionary in the scrape response.
5758
///
5859
/// See [BEP 48: Tracker Protocol Extension: Scrape](https://www.bittorrent.org/beps/bep_0048.html)
59-
#[derive(Debug, PartialEq, Default)]
60+
#[derive(Copy, Clone, Debug, PartialEq, Default, Constructor)]
6061
pub struct SwarmMetadata {
61-
/// The number of peers that have ever completed downloading
62-
pub downloaded: u32,
63-
/// The number of active peers that have completed downloading (seeders)
64-
pub complete: u32,
65-
/// The number of active peers that have not completed downloading (leechers)
62+
/// (i.e `completed`): The number of peers that have ever completed downloading
63+
pub downloaded: u32, //
64+
/// (i.e `seeders`): The number of active peers that have completed downloading (seeders)
65+
pub complete: u32, //seeders
66+
/// (i.e `leechers`): The number of active peers that have not completed downloading (leechers)
6667
pub incomplete: u32,
6768
}
6869

@@ -73,7 +74,7 @@ impl SwarmMetadata {
7374
}
7475
}
7576

76-
/// [`SwarmStats`] is has the same type as [`SwarmMetadata`]
77+
/// We use the same format for [`SwarmStats`] as [`SwarmMetadata`]
7778
pub type SwarmStats = SwarmMetadata;
7879

7980
impl Entry {

src/servers/http/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
//! 000000f0: 65 e
153153
//! ```
154154
//!
155-
//! Refer to the [`NonCompact`](crate::servers::http::v1::responses::announce::NonCompact)
155+
//! Refer to the [`Normal`](crate::servers::http::v1::responses::announce::Normal), i.e. `Non-Compact`
156156
//! response for more information about the response.
157157
//!
158158
//! **Sample compact response**

src/servers/http/v1/requests/announce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl fmt::Display for Event {
180180
/// Depending on the value of this param, the tracker will return a different
181181
/// response:
182182
///
183-
/// - [`NonCompact`](crate::servers::http::v1::responses::announce::NonCompact) response.
183+
/// - [`Normal`](crate::servers::http::v1::responses::announce::Normal), i.e. a `non-compact` response.
184184
/// - [`Compact`](crate::servers::http::v1::responses::announce::Compact) response.
185185
///
186186
/// Refer to [BEP 23. Tracker Returns Compact Peer Lists](https://www.bittorrent.org/beps/bep_0023.html)

0 commit comments

Comments
 (0)