From 063caf3c5d854ba90cf92d348c7ea4b60c295603 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 17 Jun 2024 17:21:03 +0100 Subject: [PATCH] feat!: [#878] extract announce_policy section in core config section --- packages/configuration/src/lib.rs | 4 +++- packages/configuration/src/v1/core.rs | 21 +++++++++------------ packages/configuration/src/v1/mod.rs | 12 ++++++++---- src/core/mod.rs | 14 ++++++++------ src/lib.rs | 18 ++++++++++-------- 5 files changed, 38 insertions(+), 31 deletions(-) diff --git a/packages/configuration/src/lib.rs b/packages/configuration/src/lib.rs index 83d02a79..fe31b684 100644 --- a/packages/configuration/src/lib.rs +++ b/packages/configuration/src/lib.rs @@ -96,7 +96,7 @@ impl Info { } /// Announce policy -#[derive(PartialEq, Eq, Debug, Clone, Copy, Constructor)] +#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy, Constructor)] pub struct AnnouncePolicy { /// Interval in seconds that the client should wait between sending regular /// announce requests to the tracker. @@ -109,6 +109,7 @@ pub struct AnnouncePolicy { /// client's initial request. It serves as a guideline for clients to know /// how often they should contact the tracker for updates on the peer list, /// while ensuring that the tracker is not overwhelmed with requests. + #[serde(default = "AnnouncePolicy::default_interval")] pub interval: u32, /// Minimum announce interval. Clients must not reannounce more frequently @@ -122,6 +123,7 @@ pub struct AnnouncePolicy { /// value to prevent sending too many requests in a short period, which /// could lead to excessive load on the tracker or even getting banned by /// the tracker for not adhering to the rules. + #[serde(default = "AnnouncePolicy::default_interval_min")] pub interval_min: u32, } diff --git a/packages/configuration/src/v1/core.rs b/packages/configuration/src/v1/core.rs index 5d8ab0b3..266da21e 100644 --- a/packages/configuration/src/v1/core.rs +++ b/packages/configuration/src/v1/core.rs @@ -12,14 +12,6 @@ pub struct Core { #[serde(default = "Core::default_mode")] pub mode: TrackerMode, - /// See [`AnnouncePolicy::interval`] - #[serde(default = "AnnouncePolicy::default_interval")] - pub announce_interval: u32, - - /// See [`AnnouncePolicy::interval_min`] - #[serde(default = "AnnouncePolicy::default_interval_min")] - pub min_announce_interval: u32, - /// Weather the tracker should collect statistics about tracker usage. /// If enabled, the tracker will collect statistics like the number of /// connections handled, the number of announce requests handled, etc. @@ -53,6 +45,10 @@ pub struct Core { #[serde(default = "Core::default_remove_peerless_torrents")] pub remove_peerless_torrents: bool, + // Announce policy configuration. + #[serde(default = "Core::default_announce_policy")] + pub announce_policy: AnnouncePolicy, + // Database configuration. #[serde(default = "Core::default_database")] pub database: Database, @@ -64,17 +60,14 @@ pub struct Core { impl Default for Core { fn default() -> Self { - let announce_policy = AnnouncePolicy::default(); - Self { mode: Self::default_mode(), - announce_interval: announce_policy.interval, - min_announce_interval: announce_policy.interval_min, 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(), + announce_policy: Self::default_announce_policy(), database: Self::default_database(), net: Self::default_network(), } @@ -106,6 +99,10 @@ impl Core { true } + fn default_announce_policy() -> AnnouncePolicy { + AnnouncePolicy::default() + } + fn default_database() -> Database { Database::default() } diff --git a/packages/configuration/src/v1/mod.rs b/packages/configuration/src/v1/mod.rs index d96e1335..d2f2a301 100644 --- a/packages/configuration/src/v1/mod.rs +++ b/packages/configuration/src/v1/mod.rs @@ -198,14 +198,16 @@ //! //! [core] //! mode = "public" -//! announce_interval = 120 -//! min_announce_interval = 120 //! tracker_usage_statistics = true //! persistent_torrent_completed_stat = false //! max_peer_timeout = 900 //! inactive_peer_cleanup_interval = 600 //! remove_peerless_torrents = true //! +//! [core.announce_policy] +//! interval = 120 +//! interval_min = 120 +//! //! [core.database] //! driver = "Sqlite3" //! path = "./storage/tracker/lib/database/sqlite3.db" @@ -388,14 +390,16 @@ mod tests { [core] mode = "public" - announce_interval = 120 - min_announce_interval = 120 tracker_usage_statistics = true persistent_torrent_completed_stat = false max_peer_timeout = 900 inactive_peer_cleanup_interval = 600 remove_peerless_torrents = true + [core.announce_policy] + interval = 120 + interval_min = 120 + [core.database] driver = "Sqlite3" path = "./storage/tracker/lib/database/sqlite3.db" diff --git a/src/core/mod.rs b/src/core/mod.rs index 1e7083dd..6b3c2421 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -113,10 +113,10 @@ //! } //! //! // Core tracker configuration -//! pub struct Configuration { +//! pub struct AnnounceInterval { //! // ... -//! pub announce_interval: u32, // Interval in seconds that the client should wait between sending regular announce requests to the tracker -//! pub min_announce_interval: u32, // Minimum announce interval. Clients must not reannounce more frequently than this +//! pub interval: u32, // Interval in seconds that the client should wait between sending regular announce requests to the tracker +//! pub interval_min: u32, // Minimum announce interval. Clients must not reannounce more frequently than this //! // ... //! } //! ``` @@ -317,14 +317,16 @@ //! //! [core] //! mode = "public" -//! announce_interval = 120 -//! min_announce_interval = 120 //! max_peer_timeout = 900 //! tracker_usage_statistics = true //! persistent_torrent_completed_stat = true //! inactive_peer_cleanup_interval = 600 //! remove_peerless_torrents = false //! +//! [core.announce_policy] +//! interval = 120 +//! interval_min = 120 +//! //! [core.database] //! driver = "Sqlite3" //! path = "./storage/tracker/lib/database/sqlite3.db" @@ -560,7 +562,7 @@ impl Tracker { Ok(Tracker { //config, - announce_policy: AnnouncePolicy::new(config.announce_interval, config.min_announce_interval), + announce_policy: config.announce_policy, mode, keys: tokio::sync::RwLock::new(std::collections::HashMap::new()), whitelist: tokio::sync::RwLock::new(std::collections::HashSet::new()), diff --git a/src/lib.rs b/src/lib.rs index bf925712..5c0fd4b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,22 +171,24 @@ //! log_level = "info" //! //! [core] -//! announce_interval = 120 //! inactive_peer_cleanup_interval = 600 //! max_peer_timeout = 900 -//! min_announce_interval = 120 //! mode = "public" //! persistent_torrent_completed_stat = false //! remove_peerless_torrents = true //! tracker_usage_statistics = true //! -//! [core.database] -//! driver = "Sqlite3" -//! path = "./storage/tracker/lib/database/sqlite3.db" +//! [core.announce_policy] +//! interval = 120 +//! interval_min = 120 //! -//! [core.net] -//! external_ip = "0.0.0.0" -//! on_reverse_proxy = false +//! [core.database] +//! driver = "Sqlite3" +//! path = "./storage/tracker/lib/database/sqlite3.db" +//! +//! [core.net] +//! external_ip = "0.0.0.0" +//! on_reverse_proxy = false //! //! [[udp_trackers]] //! bind_address = "0.0.0.0:6969"