Skip to content

Commit d4adfa7

Browse files
josecelanoda2ce7
authored andcommitted
refactor: extract config struct AnnouncePolicy
1 parent cca17d5 commit d4adfa7

File tree

4 files changed

+83
-91
lines changed

4 files changed

+83
-91
lines changed

packages/configuration/src/lib.rs

+44-23
Original file line numberDiff line numberDiff line change
@@ -387,26 +387,9 @@ pub struct HealthCheckApi {
387387
pub bind_address: String,
388388
}
389389

390-
/// Core configuration for the tracker.
391-
#[allow(clippy::struct_excessive_bools)]
392-
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
393-
pub struct Configuration {
394-
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
395-
/// `Debug` and `Trace`. Default is `Info`.
396-
pub log_level: Option<String>,
397-
/// Tracker mode. See [`TrackerMode`] for more information.
398-
pub mode: TrackerMode,
399-
400-
// Database configuration
401-
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
402-
pub db_driver: DatabaseDriver,
403-
/// Database connection string. The format depends on the database driver.
404-
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
405-
/// `./storage/tracker/lib/database/sqlite3.db`.
406-
/// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
407-
/// example: `root:password@localhost:3306/torrust`.
408-
pub db_path: String,
409-
390+
/// Announce policy
391+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy)]
392+
pub struct AnnouncePolicy {
410393
/// Interval in seconds that the client should wait between sending regular
411394
/// announce requests to the tracker.
412395
///
@@ -418,7 +401,8 @@ pub struct Configuration {
418401
/// client's initial request. It serves as a guideline for clients to know
419402
/// how often they should contact the tracker for updates on the peer list,
420403
/// while ensuring that the tracker is not overwhelmed with requests.
421-
pub announce_interval: u32,
404+
pub interval: u32,
405+
422406
/// Minimum announce interval. Clients must not reannounce more frequently
423407
/// than this.
424408
///
@@ -430,6 +414,42 @@ pub struct Configuration {
430414
/// value to prevent sending too many requests in a short period, which
431415
/// could lead to excessive load on the tracker or even getting banned by
432416
/// the tracker for not adhering to the rules.
417+
pub interval_min: u32,
418+
}
419+
420+
impl Default for AnnouncePolicy {
421+
fn default() -> Self {
422+
Self {
423+
interval: 120,
424+
interval_min: 120,
425+
}
426+
}
427+
}
428+
429+
/// Core configuration for the tracker.
430+
#[allow(clippy::struct_excessive_bools)]
431+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
432+
pub struct Configuration {
433+
/// Logging level. Possible values are: `Off`, `Error`, `Warn`, `Info`,
434+
/// `Debug` and `Trace`. Default is `Info`.
435+
pub log_level: Option<String>,
436+
/// Tracker mode. See [`TrackerMode`] for more information.
437+
pub mode: TrackerMode,
438+
439+
// Database configuration
440+
/// Database driver. Possible values are: `Sqlite3`, and `MySQL`.
441+
pub db_driver: DatabaseDriver,
442+
/// Database connection string. The format depends on the database driver.
443+
/// For `Sqlite3`, the format is `path/to/database.db`, for example:
444+
/// `./storage/tracker/lib/database/sqlite3.db`.
445+
/// For `Mysql`, the format is `mysql://db_user:db_user_password:port/db_name`, for
446+
/// example: `root:password@localhost:3306/torrust`.
447+
pub db_path: String,
448+
449+
/// See [`AnnouncePolicy::interval`]
450+
pub announce_interval: u32,
451+
452+
/// See [`AnnouncePolicy::interval_min`]
433453
pub min_announce_interval: u32,
434454
/// Weather the tracker is behind a reverse proxy or not.
435455
/// If the tracker is behind a reverse proxy, the `X-Forwarded-For` header
@@ -516,13 +536,14 @@ impl From<ConfigError> for Error {
516536

517537
impl Default for Configuration {
518538
fn default() -> Self {
539+
let announce_policy = AnnouncePolicy::default();
519540
let mut configuration = Configuration {
520541
log_level: Option::from(String::from("info")),
521542
mode: TrackerMode::Public,
522543
db_driver: DatabaseDriver::Sqlite3,
523544
db_path: String::from("./storage/tracker/lib/database/sqlite3.db"),
524-
announce_interval: 120,
525-
min_announce_interval: 120,
545+
announce_interval: announce_policy.interval,
546+
min_announce_interval: announce_policy.interval_min,
526547
max_peer_timeout: 900,
527548
on_reverse_proxy: false,
528549
external_ip: Some(String::from("0.0.0.0")),

src/servers/http/mod.rs

+1-1
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)
156156
//! response for more information about the response.
157157
//!
158158
//! **Sample compact response**

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

+1-1
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) 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)

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

+37-66
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use axum::http::StatusCode;
99
use axum::response::{IntoResponse, Response};
1010
use serde::{self, Deserialize, Serialize};
1111
use thiserror::Error;
12+
use torrust_tracker_configuration::AnnouncePolicy;
1213
use torrust_tracker_contrib_bencode::{ben_bytes, ben_int, ben_list, ben_map, BMutAccess, BencodeMut};
1314

1415
use crate::core::{self, AnnounceData};
@@ -20,11 +21,14 @@ use crate::servers::http::v1::responses;
2021
///
2122
/// ```rust
2223
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
24+
/// use torrust_tracker_configuration::AnnouncePolicy;
2325
/// use torrust_tracker::servers::http::v1::responses::announce::{Normal, NormalPeer};
2426
///
2527
/// let response = Normal {
26-
/// interval: 111,
27-
/// interval_min: 222,
28+
/// policy: AnnouncePolicy {
29+
/// interval: 111,
30+
/// interval_min: 222,
31+
/// },
2832
/// complete: 333,
2933
/// incomplete: 444,
3034
/// peers: vec![
@@ -58,31 +62,8 @@ use crate::servers::http::v1::responses;
5862
/// for more information.
5963
#[derive(Serialize, Deserialize, Debug, PartialEq)]
6064
pub struct Normal {
61-
/// Interval in seconds that the client should wait between sending regular
62-
/// announce requests to the tracker.
63-
///
64-
/// It's a **recommended** wait time between announcements.
65-
///
66-
/// This is the standard amount of time that clients should wait between
67-
/// sending consecutive announcements to the tracker. This value is set by
68-
/// the tracker and is typically provided in the tracker's response to a
69-
/// client's initial request. It serves as a guideline for clients to know
70-
/// how often they should contact the tracker for updates on the peer list,
71-
/// while ensuring that the tracker is not overwhelmed with requests.
72-
pub interval: u32,
73-
/// Minimum announce interval. Clients must not reannounce more frequently
74-
/// than this.
75-
///
76-
/// It establishes the shortest allowed wait time.
77-
///
78-
/// This is an optional parameter in the protocol that the tracker may
79-
/// provide in its response. It sets a lower limit on the frequency at which
80-
/// clients are allowed to send announcements. Clients should respect this
81-
/// value to prevent sending too many requests in a short period, which
82-
/// could lead to excessive load on the tracker or even getting banned by
83-
/// the tracker for not adhering to the rules.
84-
#[serde(rename = "min interval")]
85-
pub interval_min: u32,
65+
/// Announce policy
66+
pub policy: AnnouncePolicy,
8667
/// Number of peers with the entire file, i.e. seeders.
8768
pub complete: u32,
8869
/// Number of non-seeder peers, aka "leechers".
@@ -152,8 +133,8 @@ impl Normal {
152133
(ben_map! {
153134
"complete" => ben_int!(i64::from(self.complete)),
154135
"incomplete" => ben_int!(i64::from(self.incomplete)),
155-
"interval" => ben_int!(i64::from(self.interval)),
156-
"min interval" => ben_int!(i64::from(self.interval_min)),
136+
"interval" => ben_int!(i64::from(self.policy.interval)),
137+
"min interval" => ben_int!(i64::from(self.policy.interval_min)),
157138
"peers" => peers_list.clone()
158139
})
159140
.encode()
@@ -175,8 +156,10 @@ impl From<AnnounceData> for Normal {
175156
.collect();
176157

177158
Self {
178-
interval: domain_announce_response.interval,
179-
interval_min: domain_announce_response.interval_min,
159+
policy: AnnouncePolicy {
160+
interval: domain_announce_response.interval,
161+
interval_min: domain_announce_response.interval_min,
162+
},
180163
complete: domain_announce_response.swarm_stats.seeders,
181164
incomplete: domain_announce_response.swarm_stats.leechers,
182165
peers,
@@ -192,11 +175,14 @@ impl From<AnnounceData> for Normal {
192175
///
193176
/// ```rust
194177
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
178+
/// use torrust_tracker_configuration::AnnouncePolicy;
195179
/// use torrust_tracker::servers::http::v1::responses::announce::{Compact, CompactPeer};
196180
///
197181
/// let response = Compact {
198-
/// interval: 111,
199-
/// interval_min: 222,
182+
/// policy: AnnouncePolicy {
183+
/// interval: 111,
184+
/// interval_min: 222,
185+
/// },
200186
/// complete: 333,
201187
/// incomplete: 444,
202188
/// peers: vec![
@@ -232,31 +218,8 @@ impl From<AnnounceData> for Normal {
232218
/// - [BEP 07: IPv6 Tracker Extension](https://www.bittorrent.org/beps/bep_0007.html)
233219
#[derive(Serialize, Deserialize, Debug, PartialEq)]
234220
pub struct Compact {
235-
/// Interval in seconds that the client should wait between sending regular
236-
/// announce requests to the tracker.
237-
///
238-
/// It's a **recommended** wait time between announcements.
239-
///
240-
/// This is the standard amount of time that clients should wait between
241-
/// sending consecutive announcements to the tracker. This value is set by
242-
/// the tracker and is typically provided in the tracker's response to a
243-
/// client's initial request. It serves as a guideline for clients to know
244-
/// how often they should contact the tracker for updates on the peer list,
245-
/// while ensuring that the tracker is not overwhelmed with requests.
246-
pub interval: u32,
247-
/// Minimum announce interval. Clients must not reannounce more frequently
248-
/// than this.
249-
///
250-
/// It establishes the shortest allowed wait time.
251-
///
252-
/// This is an optional parameter in the protocol that the tracker may
253-
/// provide in its response. It sets a lower limit on the frequency at which
254-
/// clients are allowed to send announcements. Clients should respect this
255-
/// value to prevent sending too many requests in a short period, which
256-
/// could lead to excessive load on the tracker or even getting banned by
257-
/// the tracker for not adhering to the rules.
258-
#[serde(rename = "min interval")]
259-
pub interval_min: u32,
221+
/// Announce policy
222+
pub policy: AnnouncePolicy,
260223
/// Number of seeders, aka "completed".
261224
pub complete: u32,
262225
/// Number of non-seeder peers, aka "incomplete".
@@ -335,8 +298,8 @@ impl Compact {
335298
let bytes = (ben_map! {
336299
"complete" => ben_int!(i64::from(self.complete)),
337300
"incomplete" => ben_int!(i64::from(self.incomplete)),
338-
"interval" => ben_int!(i64::from(self.interval)),
339-
"min interval" => ben_int!(i64::from(self.interval_min)),
301+
"interval" => ben_int!(i64::from(self.policy.interval)),
302+
"min interval" => ben_int!(i64::from(self.policy.interval_min)),
340303
"peers" => ben_bytes!(self.peers_v4_bytes()?),
341304
"peers6" => ben_bytes!(self.peers_v6_bytes()?)
342305
})
@@ -414,8 +377,10 @@ impl From<AnnounceData> for Compact {
414377
.collect();
415378

416379
Self {
417-
interval: domain_announce_response.interval,
418-
interval_min: domain_announce_response.interval_min,
380+
policy: AnnouncePolicy {
381+
interval: domain_announce_response.interval,
382+
interval_min: domain_announce_response.interval_min,
383+
},
419384
complete: domain_announce_response.swarm_stats.seeders,
420385
incomplete: domain_announce_response.swarm_stats.leechers,
421386
peers,
@@ -428,6 +393,8 @@ mod tests {
428393

429394
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
430395

396+
use torrust_tracker_configuration::AnnouncePolicy;
397+
431398
use super::{Normal, NormalPeer};
432399
use crate::servers::http::v1::responses::announce::{Compact, CompactPeer};
433400

@@ -446,8 +413,10 @@ mod tests {
446413
#[test]
447414
fn normal_announce_response_can_be_bencoded() {
448415
let response = Normal {
449-
interval: 111,
450-
interval_min: 222,
416+
policy: AnnouncePolicy {
417+
interval: 111,
418+
interval_min: 222,
419+
},
451420
complete: 333,
452421
incomplete: 444,
453422
peers: vec![
@@ -480,8 +449,10 @@ mod tests {
480449
#[test]
481450
fn compact_announce_response_can_be_bencoded() {
482451
let response = Compact {
483-
interval: 111,
484-
interval_min: 222,
452+
policy: AnnouncePolicy {
453+
interval: 111,
454+
interval_min: 222,
455+
},
485456
complete: 333,
486457
incomplete: 444,
487458
peers: vec![

0 commit comments

Comments
 (0)