Skip to content

Commit

Permalink
dev: responses::announce work
Browse files Browse the repository at this point in the history
Fixed up Generic Trait to auto pick correct implementation.
  • Loading branch information
da2ce7 committed Jan 9, 2024
1 parent 374ede2 commit 05e11e3
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ serde_urlencoded = "0"
torrust-tracker-test-helpers = { version = "3.0.0-alpha.12-develop", path = "packages/test-helpers" }

[workspace]
members = ["contrib/bencode", "packages/configuration", "packages/located-error", "packages/primitives", "packages/test-helpers", "packages/torrent-repository-benchmarks"]
members = [
"contrib/bencode",
"packages/configuration",
"packages/located-error",
"packages/primitives",
"packages/test-helpers",
"packages/torrent-repository-benchmarks",
]

[profile.dev]
debug = 1
Expand Down
1 change: 1 addition & 0 deletions packages/configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ thiserror = "1"
toml = "0"
torrust-tracker-located-error = { version = "3.0.0-alpha.12-develop", path = "../located-error" }
torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "../primitives" }
derive_more = "0"

[dev-dependencies]
uuid = { version = "1", features = ["v4"] }
48 changes: 46 additions & 2 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ use std::sync::Arc;
use std::{env, fs};

use config::{Config, ConfigError, File, FileFormat};
use derive_more::Constructor;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, NoneAsEmptyString};
use thiserror::Error;
Expand Down Expand Up @@ -303,6 +304,47 @@ impl Info {
}
}

/// Announce policy
#[derive(Copy, Clone, Debug, PartialEq, Constructor)]
pub struct AnnouncePolicy {
/// Interval in seconds that the client should wait between sending regular
/// announce requests to the tracker.
///
/// It's a **recommended** wait time between announcements.
///
/// This is the standard amount of time that clients should wait between
/// sending consecutive announcements to the tracker. This value is set by
/// the tracker and is typically provided in the tracker's response to a
/// 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.
pub interval: u32,

/// Minimum announce interval. Clients must not reannounce more frequently
/// than this.
///
/// It establishes the shortest allowed wait time.
///
/// This is an optional parameter in the protocol that the tracker may
/// provide in its response. It sets a lower limit on the frequency at which
/// clients are allowed to send announcements. Clients should respect this
/// 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.
pub interval_min: u32,
}

impl Default for AnnouncePolicy {
fn default() -> Self {
let announce_interval = 120;
let min_announce_interval = 120;
Self {
interval: announce_interval,
interval_min: min_announce_interval,
}
}
}

/// Configuration for each UDP tracker.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct UdpTracker {
Expand Down Expand Up @@ -516,13 +558,15 @@ impl From<ConfigError> for Error {

impl Default for Configuration {
fn default() -> Self {
let announce_policy = AnnouncePolicy::default();

let mut configuration = Configuration {
log_level: Option::from(String::from("info")),
mode: TrackerMode::Public,
db_driver: DatabaseDriver::Sqlite3,
db_path: String::from("./storage/tracker/lib/database/sqlite3.db"),
announce_interval: 120,
min_announce_interval: 120,
announce_interval: announce_policy.interval,
min_announce_interval: announce_policy.interval_min,
max_peer_timeout: 900,
on_reverse_proxy: false,
external_ip: Some(String::from("0.0.0.0")),
Expand Down
38 changes: 5 additions & 33 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ use std::time::Duration;
use derive_more::Constructor;
use futures::future::join_all;
use tokio::sync::mpsc::error::SendError;
use torrust_tracker_configuration::Configuration;
use torrust_tracker_configuration::{AnnouncePolicy, Configuration};
use torrust_tracker_primitives::TrackerMode;

use self::auth::Key;
Expand Down Expand Up @@ -500,38 +500,8 @@ pub struct TorrentsMetrics {
pub torrents: u64,
}

/// Announce policy
#[derive(Copy, Clone, Debug, PartialEq, Constructor)]
pub struct AnnouncePolicy {
/// Interval in seconds that the client should wait between sending regular
/// announce requests to the tracker.
///
/// It's a **recommended** wait time between announcements.
///
/// This is the standard amount of time that clients should wait between
/// sending consecutive announcements to the tracker. This value is set by
/// the tracker and is typically provided in the tracker's response to a
/// 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.
pub interval: u32,

/// Minimum announce interval. Clients must not reannounce more frequently
/// than this.
///
/// It establishes the shortest allowed wait time.
///
/// This is an optional parameter in the protocol that the tracker may
/// provide in its response. It sets a lower limit on the frequency at which
/// clients are allowed to send announcements. Clients should respect this
/// 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.
pub interval_min: u32,
}

/// Structure that holds the data returned by the `announce` request.
#[derive(Clone, Debug, PartialEq, Constructor)]
#[derive(Clone, Debug, PartialEq, Constructor, Default)]
pub struct AnnounceData {
/// The list of peers that are downloading the same torrent.
/// It excludes the peer that made the request.
Expand Down Expand Up @@ -653,10 +623,12 @@ impl Tracker {

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

let policy = AnnouncePolicy::new(self.config.announce_interval, self.config.min_announce_interval);

AnnounceData {
peers,
swarm_stats,
policy: AnnouncePolicy::new(self.config.announce_interval, self.config.min_announce_interval),
policy,
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/servers/http/v1/responses/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use std::panic::Location;

use axum::http::StatusCode;
use thiserror::Error;
use torrust_tracker_configuration::AnnouncePolicy;
use torrust_tracker_contrib_bencode::{ben_bytes, ben_int, ben_list, ben_map, BMutAccess, BencodeMut};

use crate::core::torrent::SwarmStats;
use crate::core::{self, AnnounceData, AnnouncePolicy};
use crate::core::{self, AnnounceData};
use crate::servers::http::v1::responses;

/// Trait that defines the Announce Response Format
Expand Down Expand Up @@ -401,8 +402,9 @@ mod tests {

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

use torrust_tracker_configuration::AnnouncePolicy;

use crate::core::torrent::SwarmStats;
use crate::core::AnnouncePolicy;
use crate::servers::http::v1::responses::announce::{Announce, CompactPeer, CompactPeerData, NormalPeer, Response};

// Some ascii values used in tests:
Expand Down
3 changes: 2 additions & 1 deletion src/servers/http/v1/services/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ mod tests {
use std::sync::Arc;

use mockall::predicate::eq;
use torrust_tracker_configuration::AnnouncePolicy;
use torrust_tracker_test_helpers::configuration;

use super::{sample_peer_using_ipv4, sample_peer_using_ipv6};
Expand All @@ -118,7 +119,7 @@ mod tests {
complete: 1,
incomplete: 0,
},
policy: announce_data.policy,
policy: AnnouncePolicy::default(),
};

assert_eq!(announce_data, expected_announce_data);
Expand Down

0 comments on commit 05e11e3

Please sign in to comment.