Skip to content

Commit 2708f5d

Browse files
josecelanoda2ce7
authored andcommitted
feat!: [torrust#878] extract tracker_policy section in core config section
1 parent 063caf3 commit 2708f5d

File tree

7 files changed

+76
-58
lines changed

7 files changed

+76
-58
lines changed

packages/configuration/src/lib.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,51 @@ pub type HealthCheckApi = v1::health_check_api::HealthCheckApi;
4747

4848
pub type AccessTokens = HashMap<String, String>;
4949

50-
#[derive(Copy, Clone, Debug, PartialEq, Constructor)]
50+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Constructor)]
5151
pub struct TrackerPolicy {
52-
pub remove_peerless_torrents: bool,
52+
// Cleanup job configuration
53+
/// Maximum time in seconds that a peer can be inactive before being
54+
/// considered an inactive peer. If a peer is inactive for more than this
55+
/// time, it will be removed from the torrent peer list.
56+
#[serde(default = "TrackerPolicy::default_max_peer_timeout")]
5357
pub max_peer_timeout: u32,
58+
59+
/// If enabled the tracker will persist the number of completed downloads.
60+
/// That's how many times a torrent has been downloaded completely.
61+
#[serde(default = "TrackerPolicy::default_persistent_torrent_completed_stat")]
5462
pub persistent_torrent_completed_stat: bool,
63+
64+
/// If enabled, the tracker will remove torrents that have no peers.
65+
/// The clean up torrent job runs every `inactive_peer_cleanup_interval`
66+
/// seconds and it removes inactive peers. Eventually, the peer list of a
67+
/// torrent could be empty and the torrent will be removed if this option is
68+
/// enabled.
69+
#[serde(default = "TrackerPolicy::default_remove_peerless_torrents")]
70+
pub remove_peerless_torrents: bool,
71+
}
72+
73+
impl Default for TrackerPolicy {
74+
fn default() -> Self {
75+
Self {
76+
max_peer_timeout: Self::default_max_peer_timeout(),
77+
persistent_torrent_completed_stat: Self::default_persistent_torrent_completed_stat(),
78+
remove_peerless_torrents: Self::default_remove_peerless_torrents(),
79+
}
80+
}
81+
}
82+
83+
impl TrackerPolicy {
84+
fn default_max_peer_timeout() -> u32 {
85+
900
86+
}
87+
88+
fn default_persistent_torrent_completed_stat() -> bool {
89+
false
90+
}
91+
92+
fn default_remove_peerless_torrents() -> bool {
93+
true
94+
}
5595
}
5696

5797
/// Information required for loading config

packages/configuration/src/v1/core.rs

+7-33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use torrust_tracker_primitives::TrackerMode;
33

44
use super::network::Network;
55
use crate::v1::database::Database;
6-
use crate::AnnouncePolicy;
6+
use crate::{AnnouncePolicy, TrackerPolicy};
77

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

23-
/// If enabled the tracker will persist the number of completed downloads.
24-
/// That's how many times a torrent has been downloaded completely.
25-
#[serde(default = "Core::default_persistent_torrent_completed_stat")]
26-
pub persistent_torrent_completed_stat: bool,
27-
28-
// Cleanup job configuration
29-
/// Maximum time in seconds that a peer can be inactive before being
30-
/// considered an inactive peer. If a peer is inactive for more than this
31-
/// time, it will be removed from the torrent peer list.
32-
#[serde(default = "Core::default_max_peer_timeout")]
33-
pub max_peer_timeout: u32,
34-
3523
/// Interval in seconds that the cleanup job will run to remove inactive
3624
/// peers from the torrent peer list.
3725
#[serde(default = "Core::default_inactive_peer_cleanup_interval")]
3826
pub inactive_peer_cleanup_interval: u64,
3927

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

4832
// Announce policy configuration.
4933
#[serde(default = "Core::default_announce_policy")]
@@ -62,11 +46,9 @@ impl Default for Core {
6246
fn default() -> Self {
6347
Self {
6448
mode: Self::default_mode(),
65-
max_peer_timeout: Self::default_max_peer_timeout(),
6649
tracker_usage_statistics: Self::default_tracker_usage_statistics(),
67-
persistent_torrent_completed_stat: Self::default_persistent_torrent_completed_stat(),
6850
inactive_peer_cleanup_interval: Self::default_inactive_peer_cleanup_interval(),
69-
remove_peerless_torrents: Self::default_remove_peerless_torrents(),
51+
tracker_policy: Self::default_tracker_policy(),
7052
announce_policy: Self::default_announce_policy(),
7153
database: Self::default_database(),
7254
net: Self::default_network(),
@@ -83,20 +65,12 @@ impl Core {
8365
true
8466
}
8567

86-
fn default_persistent_torrent_completed_stat() -> bool {
87-
false
88-
}
89-
90-
fn default_max_peer_timeout() -> u32 {
91-
900
92-
}
93-
9468
fn default_inactive_peer_cleanup_interval() -> u64 {
9569
600
9670
}
9771

98-
fn default_remove_peerless_torrents() -> bool {
99-
true
72+
fn default_tracker_policy() -> TrackerPolicy {
73+
TrackerPolicy::default()
10074
}
10175

10276
fn default_announce_policy() -> AnnouncePolicy {

packages/configuration/src/v1/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,11 @@
199199
//! [core]
200200
//! mode = "public"
201201
//! tracker_usage_statistics = true
202-
//! persistent_torrent_completed_stat = false
203-
//! max_peer_timeout = 900
204202
//! inactive_peer_cleanup_interval = 600
203+
//!
204+
//! [core.tracker_policy]
205+
//! max_peer_timeout = 900
206+
//! persistent_torrent_completed_stat = false
205207
//! remove_peerless_torrents = true
206208
//!
207209
//! [core.announce_policy]
@@ -391,9 +393,11 @@ mod tests {
391393
[core]
392394
mode = "public"
393395
tracker_usage_statistics = true
394-
persistent_torrent_completed_stat = false
395-
max_peer_timeout = 900
396396
inactive_peer_cleanup_interval = 600
397+
398+
[core.tracker_policy]
399+
max_peer_timeout = 900
400+
persistent_torrent_completed_stat = false
397401
remove_peerless_torrents = true
398402
399403
[core.announce_policy]

packages/torrent-repository/tests/entry/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,22 @@ fn rw_lock_parking_lot() -> Torrent {
4343

4444
#[fixture]
4545
fn policy_none() -> TrackerPolicy {
46-
TrackerPolicy::new(false, 0, false)
46+
TrackerPolicy::new(0, false, false)
4747
}
4848

4949
#[fixture]
5050
fn policy_persist() -> TrackerPolicy {
51-
TrackerPolicy::new(false, 0, true)
51+
TrackerPolicy::new(0, true, false)
5252
}
5353

5454
#[fixture]
5555
fn policy_remove() -> TrackerPolicy {
56-
TrackerPolicy::new(true, 0, false)
56+
TrackerPolicy::new(0, false, true)
5757
}
5858

5959
#[fixture]
6060
fn policy_remove_persist() -> TrackerPolicy {
61-
TrackerPolicy::new(true, 0, true)
61+
TrackerPolicy::new(0, true, true)
6262
}
6363

6464
pub enum Makes {

packages/torrent-repository/tests/repository/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,22 @@ fn paginated_limit_one_offset_one() -> Pagination {
220220

221221
#[fixture]
222222
fn policy_none() -> TrackerPolicy {
223-
TrackerPolicy::new(false, 0, false)
223+
TrackerPolicy::new(0, false, false)
224224
}
225225

226226
#[fixture]
227227
fn policy_persist() -> TrackerPolicy {
228-
TrackerPolicy::new(false, 0, true)
228+
TrackerPolicy::new(0, true, false)
229229
}
230230

231231
#[fixture]
232232
fn policy_remove() -> TrackerPolicy {
233-
TrackerPolicy::new(true, 0, false)
233+
TrackerPolicy::new(0, false, true)
234234
}
235235

236236
#[fixture]
237237
fn policy_remove_persist() -> TrackerPolicy {
238-
TrackerPolicy::new(true, 0, true)
238+
TrackerPolicy::new(0, true, true)
239239
}
240240

241241
#[rstest]

src/core/mod.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,13 @@
317317
//!
318318
//! [core]
319319
//! mode = "public"
320-
//! max_peer_timeout = 900
321320
//! tracker_usage_statistics = true
322-
//! persistent_torrent_completed_stat = true
323321
//! inactive_peer_cleanup_interval = 600
324-
//! remove_peerless_torrents = false
322+
//!
323+
//! [core.tracker_policy]
324+
//! max_peer_timeout = 900
325+
//! persistent_torrent_completed_stat = false
326+
//! remove_peerless_torrents = true
325327
//!
326328
//! [core.announce_policy]
327329
//! interval = 120
@@ -571,11 +573,7 @@ impl Tracker {
571573
stats_repository,
572574
database,
573575
external_ip: config.net.external_ip,
574-
policy: TrackerPolicy::new(
575-
config.remove_peerless_torrents,
576-
config.max_peer_timeout,
577-
config.persistent_torrent_completed_stat,
578-
),
576+
policy: config.tracker_policy.clone(),
579577
on_reverse_proxy: config.net.on_reverse_proxy,
580578
})
581579
}
@@ -1045,7 +1043,7 @@ mod tests {
10451043

10461044
pub fn tracker_persisting_torrents_in_database() -> Tracker {
10471045
let mut configuration = configuration::ephemeral();
1048-
configuration.core.persistent_torrent_completed_stat = true;
1046+
configuration.core.tracker_policy.persistent_torrent_completed_stat = true;
10491047
tracker_factory(&configuration)
10501048
}
10511049

src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,13 @@
172172
//!
173173
//! [core]
174174
//! inactive_peer_cleanup_interval = 600
175-
//! max_peer_timeout = 900
176175
//! mode = "public"
176+
//! tracker_usage_statistics = true
177+
//!
178+
//! [core.tracker_policy]
179+
//! max_peer_timeout = 900
177180
//! persistent_torrent_completed_stat = false
178181
//! remove_peerless_torrents = true
179-
//! tracker_usage_statistics = true
180182
//!
181183
//! [core.announce_policy]
182184
//! interval = 120

0 commit comments

Comments
 (0)