Skip to content

Commit

Permalink
feat!: [torrust#878] extract tracker_policy section in core config se…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
josecelano authored and da2ce7 committed Jun 20, 2024
1 parent 063caf3 commit 2708f5d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 58 deletions.
44 changes: 42 additions & 2 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,51 @@ pub type HealthCheckApi = v1::health_check_api::HealthCheckApi;

pub type AccessTokens = HashMap<String, String>;

#[derive(Copy, Clone, Debug, PartialEq, Constructor)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Constructor)]
pub struct TrackerPolicy {
pub remove_peerless_torrents: bool,
// Cleanup job configuration
/// Maximum time in seconds that a peer can be inactive before being
/// considered an inactive peer. If a peer is inactive for more than this
/// time, it will be removed from the torrent peer list.
#[serde(default = "TrackerPolicy::default_max_peer_timeout")]
pub max_peer_timeout: u32,

/// If enabled the tracker will persist the number of completed downloads.
/// That's how many times a torrent has been downloaded completely.
#[serde(default = "TrackerPolicy::default_persistent_torrent_completed_stat")]
pub persistent_torrent_completed_stat: bool,

/// If enabled, the tracker will remove torrents that have no peers.
/// The clean up torrent job runs every `inactive_peer_cleanup_interval`
/// seconds and it removes inactive peers. Eventually, the peer list of a
/// torrent could be empty and the torrent will be removed if this option is
/// enabled.
#[serde(default = "TrackerPolicy::default_remove_peerless_torrents")]
pub remove_peerless_torrents: bool,
}

impl Default for TrackerPolicy {
fn default() -> Self {
Self {
max_peer_timeout: Self::default_max_peer_timeout(),
persistent_torrent_completed_stat: Self::default_persistent_torrent_completed_stat(),
remove_peerless_torrents: Self::default_remove_peerless_torrents(),
}
}
}

impl TrackerPolicy {
fn default_max_peer_timeout() -> u32 {
900
}

fn default_persistent_torrent_completed_stat() -> bool {
false
}

fn default_remove_peerless_torrents() -> bool {
true
}
}

/// Information required for loading config
Expand Down
40 changes: 7 additions & 33 deletions packages/configuration/src/v1/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use torrust_tracker_primitives::TrackerMode;

use super::network::Network;
use crate::v1::database::Database;
use crate::AnnouncePolicy;
use crate::{AnnouncePolicy, TrackerPolicy};

#[allow(clippy::struct_excessive_bools)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
Expand All @@ -20,30 +20,14 @@ pub struct Core {
#[serde(default = "Core::default_tracker_usage_statistics")]
pub tracker_usage_statistics: bool,

/// If enabled the tracker will persist the number of completed downloads.
/// That's how many times a torrent has been downloaded completely.
#[serde(default = "Core::default_persistent_torrent_completed_stat")]
pub persistent_torrent_completed_stat: bool,

// Cleanup job configuration
/// Maximum time in seconds that a peer can be inactive before being
/// considered an inactive peer. If a peer is inactive for more than this
/// time, it will be removed from the torrent peer list.
#[serde(default = "Core::default_max_peer_timeout")]
pub max_peer_timeout: u32,

/// Interval in seconds that the cleanup job will run to remove inactive
/// peers from the torrent peer list.
#[serde(default = "Core::default_inactive_peer_cleanup_interval")]
pub inactive_peer_cleanup_interval: u64,

/// If enabled, the tracker will remove torrents that have no peers.
/// The clean up torrent job runs every `inactive_peer_cleanup_interval`
/// seconds and it removes inactive peers. Eventually, the peer list of a
/// torrent could be empty and the torrent will be removed if this option is
/// enabled.
#[serde(default = "Core::default_remove_peerless_torrents")]
pub remove_peerless_torrents: bool,
// Tracker policy configuration.
#[serde(default = "Core::default_tracker_policy")]
pub tracker_policy: TrackerPolicy,

// Announce policy configuration.
#[serde(default = "Core::default_announce_policy")]
Expand All @@ -62,11 +46,9 @@ impl Default for Core {
fn default() -> Self {
Self {
mode: Self::default_mode(),
max_peer_timeout: Self::default_max_peer_timeout(),
tracker_usage_statistics: Self::default_tracker_usage_statistics(),
persistent_torrent_completed_stat: Self::default_persistent_torrent_completed_stat(),
inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(),
remove_peerless_torrents: Self::default_remove_peerless_torrents(),
tracker_policy: Self::default_tracker_policy(),
announce_policy: Self::default_announce_policy(),
database: Self::default_database(),
net: Self::default_network(),
Expand All @@ -83,20 +65,12 @@ impl Core {
true
}

fn default_persistent_torrent_completed_stat() -> bool {
false
}

fn default_max_peer_timeout() -> u32 {
900
}

fn default_inactive_peer_cleanup_interval() -> u64 {
600
}

fn default_remove_peerless_torrents() -> bool {
true
fn default_tracker_policy() -> TrackerPolicy {
TrackerPolicy::default()
}

fn default_announce_policy() -> AnnouncePolicy {
Expand Down
12 changes: 8 additions & 4 deletions packages/configuration/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,11 @@
//! [core]
//! mode = "public"
//! tracker_usage_statistics = true
//! persistent_torrent_completed_stat = false
//! max_peer_timeout = 900
//! inactive_peer_cleanup_interval = 600
//!
//! [core.tracker_policy]
//! max_peer_timeout = 900
//! persistent_torrent_completed_stat = false
//! remove_peerless_torrents = true
//!
//! [core.announce_policy]
Expand Down Expand Up @@ -391,9 +393,11 @@ mod tests {
[core]
mode = "public"
tracker_usage_statistics = true
persistent_torrent_completed_stat = false
max_peer_timeout = 900
inactive_peer_cleanup_interval = 600
[core.tracker_policy]
max_peer_timeout = 900
persistent_torrent_completed_stat = false
remove_peerless_torrents = true
[core.announce_policy]
Expand Down
8 changes: 4 additions & 4 deletions packages/torrent-repository/tests/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ fn rw_lock_parking_lot() -> Torrent {

#[fixture]
fn policy_none() -> TrackerPolicy {
TrackerPolicy::new(false, 0, false)
TrackerPolicy::new(0, false, false)
}

#[fixture]
fn policy_persist() -> TrackerPolicy {
TrackerPolicy::new(false, 0, true)
TrackerPolicy::new(0, true, false)
}

#[fixture]
fn policy_remove() -> TrackerPolicy {
TrackerPolicy::new(true, 0, false)
TrackerPolicy::new(0, false, true)
}

#[fixture]
fn policy_remove_persist() -> TrackerPolicy {
TrackerPolicy::new(true, 0, true)
TrackerPolicy::new(0, true, true)
}

pub enum Makes {
Expand Down
8 changes: 4 additions & 4 deletions packages/torrent-repository/tests/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,22 @@ fn paginated_limit_one_offset_one() -> Pagination {

#[fixture]
fn policy_none() -> TrackerPolicy {
TrackerPolicy::new(false, 0, false)
TrackerPolicy::new(0, false, false)
}

#[fixture]
fn policy_persist() -> TrackerPolicy {
TrackerPolicy::new(false, 0, true)
TrackerPolicy::new(0, true, false)
}

#[fixture]
fn policy_remove() -> TrackerPolicy {
TrackerPolicy::new(true, 0, false)
TrackerPolicy::new(0, false, true)
}

#[fixture]
fn policy_remove_persist() -> TrackerPolicy {
TrackerPolicy::new(true, 0, true)
TrackerPolicy::new(0, true, true)
}

#[rstest]
Expand Down
16 changes: 7 additions & 9 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,13 @@
//!
//! [core]
//! mode = "public"
//! max_peer_timeout = 900
//! tracker_usage_statistics = true
//! persistent_torrent_completed_stat = true
//! inactive_peer_cleanup_interval = 600
//! remove_peerless_torrents = false
//!
//! [core.tracker_policy]
//! max_peer_timeout = 900
//! persistent_torrent_completed_stat = false
//! remove_peerless_torrents = true
//!
//! [core.announce_policy]
//! interval = 120
Expand Down Expand Up @@ -571,11 +573,7 @@ impl Tracker {
stats_repository,
database,
external_ip: config.net.external_ip,
policy: TrackerPolicy::new(
config.remove_peerless_torrents,
config.max_peer_timeout,
config.persistent_torrent_completed_stat,
),
policy: config.tracker_policy.clone(),
on_reverse_proxy: config.net.on_reverse_proxy,
})
}
Expand Down Expand Up @@ -1045,7 +1043,7 @@ mod tests {

pub fn tracker_persisting_torrents_in_database() -> Tracker {
let mut configuration = configuration::ephemeral();
configuration.core.persistent_torrent_completed_stat = true;
configuration.core.tracker_policy.persistent_torrent_completed_stat = true;
tracker_factory(&configuration)
}

Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@
//!
//! [core]
//! inactive_peer_cleanup_interval = 600
//! max_peer_timeout = 900
//! mode = "public"
//! tracker_usage_statistics = true
//!
//! [core.tracker_policy]
//! max_peer_timeout = 900
//! persistent_torrent_completed_stat = false
//! remove_peerless_torrents = true
//! tracker_usage_statistics = true
//!
//! [core.announce_policy]
//! interval = 120
Expand Down

0 comments on commit 2708f5d

Please sign in to comment.