Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: remove counter for HTTP connections internally #1363

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 0 additions & 85 deletions packages/axum-http-tracker-server/tests/server/v1/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,91 +666,6 @@ mod for_all_config_modes {
compact_announce.is_ok()
}

#[tokio::test]
async fn should_increase_the_number_of_tcp4_connections_handled_in_statistics() {
logging::setup();

let env = Started::new(&configuration::ephemeral_public().into()).await;

Client::new(*env.bind_address())
.announce(&QueryBuilder::default().query())
.await;

let stats = env
.container
.http_tracker_core_container
.http_stats_repository
.get_stats()
.await;

assert_eq!(stats.tcp4_connections_handled, 1);

drop(stats);

env.stop().await;
}

#[tokio::test]
async fn should_increase_the_number_of_tcp6_connections_handled_in_statistics() {
logging::setup();

if TcpListener::bind(SocketAddrV6::new(Ipv6Addr::LOCALHOST, 0, 0, 0))
.await
.is_err()
{
return; // we cannot bind to a ipv6 socket, so we will skip this test
}

let env = Started::new(&configuration::ephemeral_ipv6().into()).await;

Client::bind(*env.bind_address(), IpAddr::from_str("::1").unwrap())
.announce(&QueryBuilder::default().query())
.await;

let stats = env
.container
.http_tracker_core_container
.http_stats_repository
.get_stats()
.await;

assert_eq!(stats.tcp6_connections_handled, 1);

drop(stats);

env.stop().await;
}

#[tokio::test]
async fn should_not_increase_the_number_of_tcp6_connections_handled_if_the_client_is_not_using_an_ipv6_ip() {
logging::setup();

// The tracker ignores the peer address in the request param. It uses the client remote ip address.

let env = Started::new(&configuration::ephemeral_public().into()).await;

Client::new(*env.bind_address())
.announce(
&QueryBuilder::default()
.with_peer_addr(&IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))
.query(),
)
.await;

let stats = env
.container
.http_tracker_core_container
.http_stats_repository
.get_stats()
.await;

assert_eq!(stats.tcp6_connections_handled, 0);

drop(stats);

env.stop().await;
}

#[tokio::test]
async fn should_increase_the_number_of_tcp4_announce_requests_handled_in_statistics() {
logging::setup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct Stats {
}

impl From<TrackerMetrics> for Stats {
#[allow(deprecated)]
fn from(metrics: TrackerMetrics) -> Self {
Self {
torrents: metrics.torrents_metrics.total_torrents,
Expand Down Expand Up @@ -124,6 +125,7 @@ mod tests {
use super::Stats;

#[test]
#[allow(deprecated)]
fn stats_resource_should_be_converted_from_tracker_metrics() {
assert_eq!(
Stats::from(TrackerMetrics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response {
}

/// `200` response that contains the [`Stats`] resource in Prometheus Text Exposition Format .
#[allow(deprecated)]
#[must_use]
pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
let mut lines = vec![];
Expand Down
6 changes: 1 addition & 5 deletions packages/http-tracker-core/src/services/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ use crate::statistics;
///
/// The service sends an statistics event that increments:
///
/// - The number of TCP connections handled by the HTTP tracker.
/// - The number of TCP `announce` requests handled by the HTTP tracker.
///
/// > **NOTICE**: as the HTTP tracker does not requires a connection request
/// > like the UDP tracker, the number of TCP connections is incremented for
/// > each `announce` request.
/// - The number of TCP `scrape` requests handled by the HTTP tracker.
pub struct AnnounceService {
core_config: Arc<Core>,
announce_handler: Arc<AnnounceHandler>,
Expand Down
6 changes: 1 addition & 5 deletions packages/http-tracker-core/src/services/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ use crate::statistics;
///
/// The service sends an statistics event that increments:
///
/// - The number of TCP connections handled by the HTTP tracker.
/// - The number of TCP `announce` requests handled by the HTTP tracker.
/// - The number of TCP `scrape` requests handled by the HTTP tracker.
///
/// > **NOTICE**: as the HTTP tracker does not requires a connection request
/// > like the UDP tracker, the number of TCP connections is incremented for
/// > each `scrape` request.
///
/// # Errors
///
/// This function will return an error if:
Expand Down
48 changes: 0 additions & 48 deletions packages/http-tracker-core/src/statistics/event/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
// TCP4
Event::Tcp4Announce => {
stats_repository.increase_tcp4_announces().await;
stats_repository.increase_tcp4_connections().await;
}
Event::Tcp4Scrape => {
stats_repository.increase_tcp4_scrapes().await;
stats_repository.increase_tcp4_connections().await;
}

// TCP6
Event::Tcp6Announce => {
stats_repository.increase_tcp6_announces().await;
stats_repository.increase_tcp6_connections().await;
}
Event::Tcp6Scrape => {
stats_repository.increase_tcp6_scrapes().await;
stats_repository.increase_tcp6_connections().await;
}
}

Expand All @@ -44,17 +40,6 @@ mod tests {
assert_eq!(stats.tcp4_announces_handled, 1);
}

#[tokio::test]
async fn should_increase_the_tcp4_connections_counter_when_it_receives_a_tcp4_announce_event() {
let stats_repository = Repository::new();

handle_event(Event::Tcp4Announce, &stats_repository).await;

let stats = stats_repository.get_stats().await;

assert_eq!(stats.tcp4_connections_handled, 1);
}

#[tokio::test]
async fn should_increase_the_tcp4_scrapes_counter_when_it_receives_a_tcp4_scrape_event() {
let stats_repository = Repository::new();
Expand All @@ -66,17 +51,6 @@ mod tests {
assert_eq!(stats.tcp4_scrapes_handled, 1);
}

#[tokio::test]
async fn should_increase_the_tcp4_connections_counter_when_it_receives_a_tcp4_scrape_event() {
let stats_repository = Repository::new();

handle_event(Event::Tcp4Scrape, &stats_repository).await;

let stats = stats_repository.get_stats().await;

assert_eq!(stats.tcp4_connections_handled, 1);
}

#[tokio::test]
async fn should_increase_the_tcp6_announces_counter_when_it_receives_a_tcp6_announce_event() {
let stats_repository = Repository::new();
Expand All @@ -88,17 +62,6 @@ mod tests {
assert_eq!(stats.tcp6_announces_handled, 1);
}

#[tokio::test]
async fn should_increase_the_tcp6_connections_counter_when_it_receives_a_tcp6_announce_event() {
let stats_repository = Repository::new();

handle_event(Event::Tcp6Announce, &stats_repository).await;

let stats = stats_repository.get_stats().await;

assert_eq!(stats.tcp6_connections_handled, 1);
}

#[tokio::test]
async fn should_increase_the_tcp6_scrapes_counter_when_it_receives_a_tcp6_scrape_event() {
let stats_repository = Repository::new();
Expand All @@ -109,15 +72,4 @@ mod tests {

assert_eq!(stats.tcp6_scrapes_handled, 1);
}

#[tokio::test]
async fn should_increase_the_tcp6_connections_counter_when_it_receives_a_tcp6_scrape_event() {
let stats_repository = Repository::new();

handle_event(Event::Tcp6Scrape, &stats_repository).await;

let stats = stats_repository.get_stats().await;

assert_eq!(stats.tcp6_connections_handled, 1);
}
}
8 changes: 0 additions & 8 deletions packages/http-tracker-core/src/statistics/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@
/// and also for each IP version used by the peers: IPv4 and IPv6.
#[derive(Debug, PartialEq, Default)]
pub struct Metrics {
/// Total number of TCP (HTTP tracker) connections from IPv4 peers.
/// Since the HTTP tracker spec does not require a handshake, this metric
/// increases for every HTTP request.
pub tcp4_connections_handled: u64,

/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
pub tcp4_announces_handled: u64,

/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
pub tcp4_scrapes_handled: u64,

/// Total number of TCP (HTTP tracker) connections from IPv6 peers.
pub tcp6_connections_handled: u64,

/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
pub tcp6_announces_handled: u64,

Expand Down
12 changes: 0 additions & 12 deletions packages/http-tracker-core/src/statistics/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ impl Repository {
drop(stats_lock);
}

pub async fn increase_tcp4_connections(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.tcp4_connections_handled += 1;
drop(stats_lock);
}

pub async fn increase_tcp4_scrapes(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.tcp4_scrapes_handled += 1;
Expand All @@ -52,12 +46,6 @@ impl Repository {
drop(stats_lock);
}

pub async fn increase_tcp6_connections(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.tcp6_connections_handled += 1;
drop(stats_lock);
}

pub async fn increase_tcp6_scrapes(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.tcp6_scrapes_handled += 1;
Expand Down
2 changes: 0 additions & 2 deletions packages/http-tracker-core/src/statistics/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ pub async fn get_metrics(
torrents_metrics,
protocol_metrics: Metrics {
// TCPv4
tcp4_connections_handled: stats.tcp4_connections_handled,
tcp4_announces_handled: stats.tcp4_announces_handled,
tcp4_scrapes_handled: stats.tcp4_scrapes_handled,
// TCPv6
tcp6_connections_handled: stats.tcp6_connections_handled,
tcp6_announces_handled: stats.tcp6_announces_handled,
tcp6_scrapes_handled: stats.tcp6_scrapes_handled,
},
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-tracker-api-core/src/statistics/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Metrics {
/// Total number of TCP (HTTP tracker) connections from IPv4 peers.
/// Since the HTTP tracker spec does not require a handshake, this metric
/// increases for every HTTP request.
#[deprecated(since = "3.1.0")]
pub tcp4_connections_handled: u64,

/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
Expand All @@ -20,6 +21,7 @@ pub struct Metrics {
pub tcp4_scrapes_handled: u64,

/// Total number of TCP (HTTP tracker) connections from IPv6 peers.
#[deprecated(since = "3.1.0")]
pub tcp6_connections_handled: u64,

/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
Expand Down
10 changes: 8 additions & 2 deletions packages/rest-tracker-api-core/src/statistics/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct TrackerMetrics {
}

/// It returns all the [`TrackerMetrics`]
#[allow(deprecated)]
pub async fn get_metrics(
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
ban_service: Arc<RwLock<BanService>>,
Expand All @@ -37,15 +38,20 @@ pub async fn get_metrics(
let udp_core_stats = udp_core_stats_repository.get_stats().await;
let udp_server_stats = udp_server_stats_repository.get_stats().await;

// For backward compatibility we keep the `tcp4_connections_handled` and
// `tcp6_connections_handled` metrics. They don't make sense for the HTTP
// tracker, but we keep them for now. In new major versions we should remove
// them.

TrackerMetrics {
torrents_metrics,
protocol_metrics: Metrics {
// TCPv4
tcp4_connections_handled: http_stats.tcp4_connections_handled,
tcp4_connections_handled: http_stats.tcp4_announces_handled + http_stats.tcp4_scrapes_handled,
tcp4_announces_handled: http_stats.tcp4_announces_handled,
tcp4_scrapes_handled: http_stats.tcp4_scrapes_handled,
// TCPv6
tcp6_connections_handled: http_stats.tcp6_connections_handled,
tcp6_connections_handled: http_stats.tcp6_announces_handled + http_stats.tcp6_scrapes_handled,
tcp6_announces_handled: http_stats.tcp6_announces_handled,
tcp6_scrapes_handled: http_stats.tcp6_scrapes_handled,
// UDP
Expand Down
Loading