Skip to content

Commit 6a22b1e

Browse files
committed
Merge #1363: Refactor: remove counter for HTTP connections internally
89607cc refactor: [#1342] remove counter for HTTP connetions internally (Jose Celano) Pull request description: The number of HTTP tracker connections don't make sense. There are connection requests only in the UDP tracker. That code is removed but, in order to keep backward compatibility, the API still exposes that value which is the: number of announce requests + number of scrape requests ACKs for top commit: josecelano: ACK 89607cc Tree-SHA512: 066132e56f5e7e14050d78a15981c07f79acfe0b11903da35f15ca4214d16873799479f0aa98bb767eb23b496988e808d3f1ab1544b78ab072393711f28598b7
2 parents cd3d4fd + 89607cc commit 6a22b1e

File tree

11 files changed

+15
-167
lines changed

11 files changed

+15
-167
lines changed

packages/axum-http-tracker-server/tests/server/v1/contract.rs

-85
Original file line numberDiff line numberDiff line change
@@ -666,91 +666,6 @@ mod for_all_config_modes {
666666
compact_announce.is_ok()
667667
}
668668

669-
#[tokio::test]
670-
async fn should_increase_the_number_of_tcp4_connections_handled_in_statistics() {
671-
logging::setup();
672-
673-
let env = Started::new(&configuration::ephemeral_public().into()).await;
674-
675-
Client::new(*env.bind_address())
676-
.announce(&QueryBuilder::default().query())
677-
.await;
678-
679-
let stats = env
680-
.container
681-
.http_tracker_core_container
682-
.http_stats_repository
683-
.get_stats()
684-
.await;
685-
686-
assert_eq!(stats.tcp4_connections_handled, 1);
687-
688-
drop(stats);
689-
690-
env.stop().await;
691-
}
692-
693-
#[tokio::test]
694-
async fn should_increase_the_number_of_tcp6_connections_handled_in_statistics() {
695-
logging::setup();
696-
697-
if TcpListener::bind(SocketAddrV6::new(Ipv6Addr::LOCALHOST, 0, 0, 0))
698-
.await
699-
.is_err()
700-
{
701-
return; // we cannot bind to a ipv6 socket, so we will skip this test
702-
}
703-
704-
let env = Started::new(&configuration::ephemeral_ipv6().into()).await;
705-
706-
Client::bind(*env.bind_address(), IpAddr::from_str("::1").unwrap())
707-
.announce(&QueryBuilder::default().query())
708-
.await;
709-
710-
let stats = env
711-
.container
712-
.http_tracker_core_container
713-
.http_stats_repository
714-
.get_stats()
715-
.await;
716-
717-
assert_eq!(stats.tcp6_connections_handled, 1);
718-
719-
drop(stats);
720-
721-
env.stop().await;
722-
}
723-
724-
#[tokio::test]
725-
async fn should_not_increase_the_number_of_tcp6_connections_handled_if_the_client_is_not_using_an_ipv6_ip() {
726-
logging::setup();
727-
728-
// The tracker ignores the peer address in the request param. It uses the client remote ip address.
729-
730-
let env = Started::new(&configuration::ephemeral_public().into()).await;
731-
732-
Client::new(*env.bind_address())
733-
.announce(
734-
&QueryBuilder::default()
735-
.with_peer_addr(&IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))
736-
.query(),
737-
)
738-
.await;
739-
740-
let stats = env
741-
.container
742-
.http_tracker_core_container
743-
.http_stats_repository
744-
.get_stats()
745-
.await;
746-
747-
assert_eq!(stats.tcp6_connections_handled, 0);
748-
749-
drop(stats);
750-
751-
env.stop().await;
752-
}
753-
754669
#[tokio::test]
755670
async fn should_increase_the_number_of_tcp4_announce_requests_handled_in_statistics() {
756671
logging::setup();

packages/axum-rest-tracker-api-server/src/v1/context/stats/resources.rs

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub struct Stats {
7777
}
7878

7979
impl From<TrackerMetrics> for Stats {
80+
#[allow(deprecated)]
8081
fn from(metrics: TrackerMetrics) -> Self {
8182
Self {
8283
torrents: metrics.torrents_metrics.total_torrents,
@@ -124,6 +125,7 @@ mod tests {
124125
use super::Stats;
125126

126127
#[test]
128+
#[allow(deprecated)]
127129
fn stats_resource_should_be_converted_from_tracker_metrics() {
128130
assert_eq!(
129131
Stats::from(TrackerMetrics {

packages/axum-rest-tracker-api-server/src/v1/context/stats/responses.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response {
1212
}
1313

1414
/// `200` response that contains the [`Stats`] resource in Prometheus Text Exposition Format .
15+
#[allow(deprecated)]
1516
#[must_use]
1617
pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
1718
let mut lines = vec![];

packages/http-tracker-core/src/services/announce.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ use crate::statistics;
2828
///
2929
/// The service sends an statistics event that increments:
3030
///
31-
/// - The number of TCP connections handled by the HTTP tracker.
3231
/// - The number of TCP `announce` requests handled by the HTTP tracker.
33-
///
34-
/// > **NOTICE**: as the HTTP tracker does not requires a connection request
35-
/// > like the UDP tracker, the number of TCP connections is incremented for
36-
/// > each `announce` request.
32+
/// - The number of TCP `scrape` requests handled by the HTTP tracker.
3733
pub struct AnnounceService {
3834
core_config: Arc<Core>,
3935
announce_handler: Arc<AnnounceHandler>,

packages/http-tracker-core/src/services/scrape.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@ use crate::statistics;
2525
///
2626
/// The service sends an statistics event that increments:
2727
///
28-
/// - The number of TCP connections handled by the HTTP tracker.
28+
/// - The number of TCP `announce` requests handled by the HTTP tracker.
2929
/// - The number of TCP `scrape` requests handled by the HTTP tracker.
3030
///
31-
/// > **NOTICE**: as the HTTP tracker does not requires a connection request
32-
/// > like the UDP tracker, the number of TCP connections is incremented for
33-
/// > each `scrape` request.
34-
///
3531
/// # Errors
3632
///
3733
/// This function will return an error if:

packages/http-tracker-core/src/statistics/event/handler.rs

-48
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
66
// TCP4
77
Event::Tcp4Announce => {
88
stats_repository.increase_tcp4_announces().await;
9-
stats_repository.increase_tcp4_connections().await;
109
}
1110
Event::Tcp4Scrape => {
1211
stats_repository.increase_tcp4_scrapes().await;
13-
stats_repository.increase_tcp4_connections().await;
1412
}
1513

1614
// TCP6
1715
Event::Tcp6Announce => {
1816
stats_repository.increase_tcp6_announces().await;
19-
stats_repository.increase_tcp6_connections().await;
2017
}
2118
Event::Tcp6Scrape => {
2219
stats_repository.increase_tcp6_scrapes().await;
23-
stats_repository.increase_tcp6_connections().await;
2420
}
2521
}
2622

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

47-
#[tokio::test]
48-
async fn should_increase_the_tcp4_connections_counter_when_it_receives_a_tcp4_announce_event() {
49-
let stats_repository = Repository::new();
50-
51-
handle_event(Event::Tcp4Announce, &stats_repository).await;
52-
53-
let stats = stats_repository.get_stats().await;
54-
55-
assert_eq!(stats.tcp4_connections_handled, 1);
56-
}
57-
5843
#[tokio::test]
5944
async fn should_increase_the_tcp4_scrapes_counter_when_it_receives_a_tcp4_scrape_event() {
6045
let stats_repository = Repository::new();
@@ -66,17 +51,6 @@ mod tests {
6651
assert_eq!(stats.tcp4_scrapes_handled, 1);
6752
}
6853

69-
#[tokio::test]
70-
async fn should_increase_the_tcp4_connections_counter_when_it_receives_a_tcp4_scrape_event() {
71-
let stats_repository = Repository::new();
72-
73-
handle_event(Event::Tcp4Scrape, &stats_repository).await;
74-
75-
let stats = stats_repository.get_stats().await;
76-
77-
assert_eq!(stats.tcp4_connections_handled, 1);
78-
}
79-
8054
#[tokio::test]
8155
async fn should_increase_the_tcp6_announces_counter_when_it_receives_a_tcp6_announce_event() {
8256
let stats_repository = Repository::new();
@@ -88,17 +62,6 @@ mod tests {
8862
assert_eq!(stats.tcp6_announces_handled, 1);
8963
}
9064

91-
#[tokio::test]
92-
async fn should_increase_the_tcp6_connections_counter_when_it_receives_a_tcp6_announce_event() {
93-
let stats_repository = Repository::new();
94-
95-
handle_event(Event::Tcp6Announce, &stats_repository).await;
96-
97-
let stats = stats_repository.get_stats().await;
98-
99-
assert_eq!(stats.tcp6_connections_handled, 1);
100-
}
101-
10265
#[tokio::test]
10366
async fn should_increase_the_tcp6_scrapes_counter_when_it_receives_a_tcp6_scrape_event() {
10467
let stats_repository = Repository::new();
@@ -109,15 +72,4 @@ mod tests {
10972

11073
assert_eq!(stats.tcp6_scrapes_handled, 1);
11174
}
112-
113-
#[tokio::test]
114-
async fn should_increase_the_tcp6_connections_counter_when_it_receives_a_tcp6_scrape_event() {
115-
let stats_repository = Repository::new();
116-
117-
handle_event(Event::Tcp6Scrape, &stats_repository).await;
118-
119-
let stats = stats_repository.get_stats().await;
120-
121-
assert_eq!(stats.tcp6_connections_handled, 1);
122-
}
12375
}

packages/http-tracker-core/src/statistics/metrics.rs

-8
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,12 @@
88
/// and also for each IP version used by the peers: IPv4 and IPv6.
99
#[derive(Debug, PartialEq, Default)]
1010
pub struct Metrics {
11-
/// Total number of TCP (HTTP tracker) connections from IPv4 peers.
12-
/// Since the HTTP tracker spec does not require a handshake, this metric
13-
/// increases for every HTTP request.
14-
pub tcp4_connections_handled: u64,
15-
1611
/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
1712
pub tcp4_announces_handled: u64,
1813

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

22-
/// Total number of TCP (HTTP tracker) connections from IPv6 peers.
23-
pub tcp6_connections_handled: u64,
24-
2517
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
2618
pub tcp6_announces_handled: u64,
2719

packages/http-tracker-core/src/statistics/repository.rs

-12
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ impl Repository {
3434
drop(stats_lock);
3535
}
3636

37-
pub async fn increase_tcp4_connections(&self) {
38-
let mut stats_lock = self.stats.write().await;
39-
stats_lock.tcp4_connections_handled += 1;
40-
drop(stats_lock);
41-
}
42-
4337
pub async fn increase_tcp4_scrapes(&self) {
4438
let mut stats_lock = self.stats.write().await;
4539
stats_lock.tcp4_scrapes_handled += 1;
@@ -52,12 +46,6 @@ impl Repository {
5246
drop(stats_lock);
5347
}
5448

55-
pub async fn increase_tcp6_connections(&self) {
56-
let mut stats_lock = self.stats.write().await;
57-
stats_lock.tcp6_connections_handled += 1;
58-
drop(stats_lock);
59-
}
60-
6149
pub async fn increase_tcp6_scrapes(&self) {
6250
let mut stats_lock = self.stats.write().await;
6351
stats_lock.tcp6_scrapes_handled += 1;

packages/http-tracker-core/src/statistics/services.rs

-2
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,9 @@ pub async fn get_metrics(
5454
torrents_metrics,
5555
protocol_metrics: Metrics {
5656
// TCPv4
57-
tcp4_connections_handled: stats.tcp4_connections_handled,
5857
tcp4_announces_handled: stats.tcp4_announces_handled,
5958
tcp4_scrapes_handled: stats.tcp4_scrapes_handled,
6059
// TCPv6
61-
tcp6_connections_handled: stats.tcp6_connections_handled,
6260
tcp6_announces_handled: stats.tcp6_announces_handled,
6361
tcp6_scrapes_handled: stats.tcp6_scrapes_handled,
6462
},

packages/rest-tracker-api-core/src/statistics/metrics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct Metrics {
1111
/// Total number of TCP (HTTP tracker) connections from IPv4 peers.
1212
/// Since the HTTP tracker spec does not require a handshake, this metric
1313
/// increases for every HTTP request.
14+
#[deprecated(since = "3.1.0")]
1415
pub tcp4_connections_handled: u64,
1516

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

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

2527
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.

packages/rest-tracker-api-core/src/statistics/services.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct TrackerMetrics {
2424
}
2525

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

41+
// For backward compatibility we keep the `tcp4_connections_handled` and
42+
// `tcp6_connections_handled` metrics. They don't make sense for the HTTP
43+
// tracker, but we keep them for now. In new major versions we should remove
44+
// them.
45+
4046
TrackerMetrics {
4147
torrents_metrics,
4248
protocol_metrics: Metrics {
4349
// TCPv4
44-
tcp4_connections_handled: http_stats.tcp4_connections_handled,
50+
tcp4_connections_handled: http_stats.tcp4_announces_handled + http_stats.tcp4_scrapes_handled,
4551
tcp4_announces_handled: http_stats.tcp4_announces_handled,
4652
tcp4_scrapes_handled: http_stats.tcp4_scrapes_handled,
4753
// TCPv6
48-
tcp6_connections_handled: http_stats.tcp6_connections_handled,
54+
tcp6_connections_handled: http_stats.tcp6_announces_handled + http_stats.tcp6_scrapes_handled,
4955
tcp6_announces_handled: http_stats.tcp6_announces_handled,
5056
tcp6_scrapes_handled: http_stats.tcp6_scrapes_handled,
5157
// UDP

0 commit comments

Comments
 (0)