Skip to content

Commit f57e7dd

Browse files
committed
dev: responses::announce work
Fixed up Generic Trait to auto pick correct implementation.
1 parent 374ede2 commit f57e7dd

File tree

7 files changed

+68
-39
lines changed

7 files changed

+68
-39
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,14 @@ serde_urlencoded = "0"
7979
torrust-tracker-test-helpers = { version = "3.0.0-alpha.12-develop", path = "packages/test-helpers" }
8080

8181
[workspace]
82-
members = ["contrib/bencode", "packages/configuration", "packages/located-error", "packages/primitives", "packages/test-helpers", "packages/torrent-repository-benchmarks"]
82+
members = [
83+
"contrib/bencode",
84+
"packages/configuration",
85+
"packages/located-error",
86+
"packages/primitives",
87+
"packages/test-helpers",
88+
"packages/torrent-repository-benchmarks",
89+
]
8390

8491
[profile.dev]
8592
debug = 1

packages/configuration/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ thiserror = "1"
2323
toml = "0"
2424
torrust-tracker-located-error = { version = "3.0.0-alpha.12-develop", path = "../located-error" }
2525
torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "../primitives" }
26+
derive_more = "0"
2627

2728
[dev-dependencies]
2829
uuid = { version = "1", features = ["v4"] }

packages/configuration/src/lib.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ use std::sync::Arc;
236236
use std::{env, fs};
237237

238238
use config::{Config, ConfigError, File, FileFormat};
239+
use derive_more::Constructor;
239240
use serde::{Deserialize, Serialize};
240241
use serde_with::{serde_as, NoneAsEmptyString};
241242
use thiserror::Error;
@@ -303,6 +304,47 @@ impl Info {
303304
}
304305
}
305306

307+
/// Announce policy
308+
#[derive(Copy, Clone, Debug, PartialEq, Constructor)]
309+
pub struct AnnouncePolicy {
310+
/// Interval in seconds that the client should wait between sending regular
311+
/// announce requests to the tracker.
312+
///
313+
/// It's a **recommended** wait time between announcements.
314+
///
315+
/// This is the standard amount of time that clients should wait between
316+
/// sending consecutive announcements to the tracker. This value is set by
317+
/// the tracker and is typically provided in the tracker's response to a
318+
/// client's initial request. It serves as a guideline for clients to know
319+
/// how often they should contact the tracker for updates on the peer list,
320+
/// while ensuring that the tracker is not overwhelmed with requests.
321+
pub interval: u32,
322+
323+
/// Minimum announce interval. Clients must not reannounce more frequently
324+
/// than this.
325+
///
326+
/// It establishes the shortest allowed wait time.
327+
///
328+
/// This is an optional parameter in the protocol that the tracker may
329+
/// provide in its response. It sets a lower limit on the frequency at which
330+
/// clients are allowed to send announcements. Clients should respect this
331+
/// value to prevent sending too many requests in a short period, which
332+
/// could lead to excessive load on the tracker or even getting banned by
333+
/// the tracker for not adhering to the rules.
334+
pub interval_min: u32,
335+
}
336+
337+
impl Default for AnnouncePolicy {
338+
fn default() -> Self {
339+
let announce_interval = 120;
340+
let min_announce_interval = 120;
341+
Self {
342+
interval: announce_interval,
343+
interval_min: min_announce_interval,
344+
}
345+
}
346+
}
347+
306348
/// Configuration for each UDP tracker.
307349
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
308350
pub struct UdpTracker {
@@ -516,13 +558,15 @@ impl From<ConfigError> for Error {
516558

517559
impl Default for Configuration {
518560
fn default() -> Self {
561+
let announce_policy = AnnouncePolicy::default();
562+
519563
let mut configuration = Configuration {
520564
log_level: Option::from(String::from("info")),
521565
mode: TrackerMode::Public,
522566
db_driver: DatabaseDriver::Sqlite3,
523567
db_path: String::from("./storage/tracker/lib/database/sqlite3.db"),
524-
announce_interval: 120,
525-
min_announce_interval: 120,
568+
announce_interval: announce_policy.interval,
569+
min_announce_interval: announce_policy.interval_min,
526570
max_peer_timeout: 900,
527571
on_reverse_proxy: false,
528572
external_ip: Some(String::from("0.0.0.0")),

src/core/mod.rs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ use std::time::Duration;
448448
use derive_more::Constructor;
449449
use futures::future::join_all;
450450
use tokio::sync::mpsc::error::SendError;
451-
use torrust_tracker_configuration::Configuration;
451+
use torrust_tracker_configuration::{AnnouncePolicy, Configuration};
452452
use torrust_tracker_primitives::TrackerMode;
453453

454454
use self::auth::Key;
@@ -500,38 +500,8 @@ pub struct TorrentsMetrics {
500500
pub torrents: u64,
501501
}
502502

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-
533503
/// Structure that holds the data returned by the `announce` request.
534-
#[derive(Clone, Debug, PartialEq, Constructor)]
504+
#[derive(Clone, Debug, PartialEq, Constructor, Default)]
535505
pub struct AnnounceData {
536506
/// The list of peers that are downloading the same torrent.
537507
/// It excludes the peer that made the request.
@@ -653,10 +623,12 @@ impl Tracker {
653623

654624
let peers = self.get_torrent_peers_for_peer(info_hash, peer).await;
655625

626+
let policy = AnnouncePolicy::new(self.config.announce_interval, self.config.min_announce_interval);
627+
656628
AnnounceData {
629+
policy,
657630
peers,
658631
swarm_stats,
659-
policy: AnnouncePolicy::new(self.config.announce_interval, self.config.min_announce_interval),
660632
}
661633
}
662634

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ use std::panic::Location;
88

99
use axum::http::StatusCode;
1010
use thiserror::Error;
11+
use torrust_tracker_configuration::AnnouncePolicy;
1112
use torrust_tracker_contrib_bencode::{ben_bytes, ben_int, ben_list, ben_map, BMutAccess, BencodeMut};
1213

1314
use crate::core::torrent::SwarmStats;
14-
use crate::core::{self, AnnounceData, AnnouncePolicy};
15+
use crate::core::{self, AnnounceData};
1516
use crate::servers::http::v1::responses;
1617

1718
/// Trait that defines the Announce Response Format
@@ -401,8 +402,9 @@ mod tests {
401402

402403
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
403404

405+
use torrust_tracker_configuration::AnnouncePolicy;
406+
404407
use crate::core::torrent::SwarmStats;
405-
use crate::core::AnnouncePolicy;
406408
use crate::servers::http::v1::responses::announce::{Announce, CompactPeer, CompactPeerData, NormalPeer, Response};
407409

408410
// Some ascii values used in tests:

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ mod tests {
9999
use super::{sample_peer_using_ipv4, sample_peer_using_ipv6};
100100
use crate::core::peer::Peer;
101101
use crate::core::torrent::SwarmStats;
102-
use crate::core::{statistics, AnnounceData, Tracker};
102+
use crate::core::{statistics, AnnounceData, AnnouncePolicy, Tracker};
103103
use crate::servers::http::v1::services::announce::invoke;
104104
use crate::servers::http::v1::services::announce::tests::{public_tracker, sample_info_hash, sample_peer};
105105

@@ -111,6 +111,8 @@ mod tests {
111111

112112
let announce_data = invoke(tracker.clone(), sample_info_hash(), &mut peer).await;
113113

114+
let policy = AnnouncePolicy::new(tracker.config.announce_interval, tracker.config.min_announce_interval);
115+
114116
let expected_announce_data = AnnounceData {
115117
peers: vec![],
116118
swarm_stats: SwarmStats {

0 commit comments

Comments
 (0)