Skip to content

Commit 2de6c14

Browse files
committed
refactor: [torrust#1373] merge HTTP stats events with different IP version
1 parent 6de2dd9 commit 2de6c14

File tree

5 files changed

+31
-73
lines changed

5 files changed

+31
-73
lines changed

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

+11-25
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,14 @@ impl AnnounceService {
125125

126126
async fn send_stats_event(&self, peer_ip: IpAddr, server_socket_addr: SocketAddr) {
127127
if let Some(http_stats_event_sender) = self.opt_http_stats_event_sender.as_deref() {
128-
match peer_ip {
129-
IpAddr::V4(_) => {
130-
http_stats_event_sender
131-
.send_event(statistics::event::Event::Tcp4Announce {
132-
connection: statistics::event::ConnectionContext {
133-
client_ip_addr: peer_ip,
134-
server_socket_addr,
135-
},
136-
})
137-
.await;
138-
}
139-
IpAddr::V6(_) => {
140-
http_stats_event_sender
141-
.send_event(statistics::event::Event::Tcp6Announce {
142-
connection: statistics::event::ConnectionContext {
143-
client_ip_addr: peer_ip,
144-
server_socket_addr,
145-
},
146-
})
147-
.await;
148-
}
149-
}
128+
http_stats_event_sender
129+
.send_event(statistics::event::Event::TcpAnnounce {
130+
connection: statistics::event::ConnectionContext {
131+
client_ip_addr: peer_ip,
132+
server_socket_addr,
133+
},
134+
})
135+
.await;
150136
}
151137
}
152138
}
@@ -394,7 +380,7 @@ mod tests {
394380
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
395381
http_stats_event_sender_mock
396382
.expect_send_event()
397-
.with(eq(statistics::event::Event::Tcp4Announce {
383+
.with(eq(statistics::event::Event::TcpAnnounce {
398384
connection: ConnectionContext {
399385
client_ip_addr: IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)),
400386
server_socket_addr,
@@ -453,7 +439,7 @@ mod tests {
453439
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
454440
http_stats_event_sender_mock
455441
.expect_send_event()
456-
.with(eq(statistics::event::Event::Tcp4Announce {
442+
.with(eq(statistics::event::Event::TcpAnnounce {
457443
connection: ConnectionContext {
458444
client_ip_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
459445
server_socket_addr,
@@ -495,7 +481,7 @@ mod tests {
495481
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
496482
http_stats_event_sender_mock
497483
.expect_send_event()
498-
.with(eq(statistics::event::Event::Tcp6Announce {
484+
.with(eq(statistics::event::Event::TcpAnnounce {
499485
connection: ConnectionContext {
500486
client_ip_addr: IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
501487
server_socket_addr,

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

+8-15
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,14 @@ impl ScrapeService {
106106

107107
async fn send_stats_event(&self, original_peer_ip: IpAddr, server_socket_addr: SocketAddr) {
108108
if let Some(http_stats_event_sender) = self.opt_http_stats_event_sender.as_deref() {
109-
let event = match original_peer_ip {
110-
IpAddr::V4(_) => statistics::event::Event::Tcp4Scrape {
109+
http_stats_event_sender
110+
.send_event(statistics::event::Event::TcpScrape {
111111
connection: ConnectionContext {
112112
client_ip_addr: original_peer_ip,
113113
server_socket_addr,
114114
},
115-
},
116-
IpAddr::V6(_) => statistics::event::Event::Tcp6Scrape {
117-
connection: ConnectionContext {
118-
client_ip_addr: original_peer_ip,
119-
server_socket_addr,
120-
},
121-
},
122-
};
123-
http_stats_event_sender.send_event(event).await;
115+
})
116+
.await;
124117
}
125118
}
126119
}
@@ -342,7 +335,7 @@ mod tests {
342335
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
343336
http_stats_event_sender_mock
344337
.expect_send_event()
345-
.with(eq(statistics::event::Event::Tcp4Scrape {
338+
.with(eq(statistics::event::Event::TcpScrape {
346339
connection: ConnectionContext {
347340
client_ip_addr: IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)),
348341
server_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070),
@@ -390,7 +383,7 @@ mod tests {
390383
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
391384
http_stats_event_sender_mock
392385
.expect_send_event()
393-
.with(eq(statistics::event::Event::Tcp6Scrape {
386+
.with(eq(statistics::event::Event::TcpScrape {
394387
connection: ConnectionContext {
395388
client_ip_addr: IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
396389
server_socket_addr,
@@ -510,7 +503,7 @@ mod tests {
510503
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
511504
http_stats_event_sender_mock
512505
.expect_send_event()
513-
.with(eq(statistics::event::Event::Tcp4Scrape {
506+
.with(eq(statistics::event::Event::TcpScrape {
514507
connection: ConnectionContext {
515508
client_ip_addr: IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)),
516509
server_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070),
@@ -558,7 +551,7 @@ mod tests {
558551
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
559552
http_stats_event_sender_mock
560553
.expect_send_event()
561-
.with(eq(statistics::event::Event::Tcp6Scrape {
554+
.with(eq(statistics::event::Event::TcpScrape {
562555
connection: ConnectionContext {
563556
client_ip_addr: IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
564557
server_socket_addr,

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

+7-25
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,17 @@ use crate::statistics::repository::Repository;
99
/// version of the event.
1010
pub async fn handle_event(event: Event, stats_repository: &Repository) {
1111
match event {
12-
// TCP4
13-
Event::Tcp4Announce { connection } => match connection.client_ip_addr {
12+
Event::TcpAnnounce { connection } => match connection.client_ip_addr {
1413
IpAddr::V4(_) => {
1514
stats_repository.increase_tcp4_announces().await;
1615
}
17-
IpAddr::V6(_) => {
18-
panic!("A client IPv6 address was received in a TCP4 announce event");
19-
}
20-
},
21-
Event::Tcp4Scrape { connection } => match connection.client_ip_addr {
22-
IpAddr::V4(_) => {
23-
stats_repository.increase_tcp4_scrapes().await;
24-
}
25-
IpAddr::V6(_) => {
26-
panic!("A client IPv6 address was received in a TCP4 scrape event");
27-
}
28-
},
29-
// TCP6
30-
Event::Tcp6Announce { connection } => match connection.client_ip_addr {
31-
IpAddr::V4(_) => {
32-
panic!("A client IPv4 address was received in a TCP6 announce event");
33-
}
3416
IpAddr::V6(_) => {
3517
stats_repository.increase_tcp6_announces().await;
3618
}
3719
},
38-
Event::Tcp6Scrape { connection } => match connection.client_ip_addr {
20+
Event::TcpScrape { connection } => match connection.client_ip_addr {
3921
IpAddr::V4(_) => {
40-
panic!("A client IPv4 address was received in a TCP6 scrape event");
22+
stats_repository.increase_tcp4_scrapes().await;
4123
}
4224
IpAddr::V6(_) => {
4325
stats_repository.increase_tcp6_scrapes().await;
@@ -61,7 +43,7 @@ mod tests {
6143
let stats_repository = Repository::new();
6244

6345
handle_event(
64-
Event::Tcp4Announce {
46+
Event::TcpAnnounce {
6547
connection: ConnectionContext {
6648
client_ip_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)),
6749
server_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070),
@@ -81,7 +63,7 @@ mod tests {
8163
let stats_repository = Repository::new();
8264

8365
handle_event(
84-
Event::Tcp4Scrape {
66+
Event::TcpScrape {
8567
connection: ConnectionContext {
8668
client_ip_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)),
8769
server_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070),
@@ -101,7 +83,7 @@ mod tests {
10183
let stats_repository = Repository::new();
10284

10385
handle_event(
104-
Event::Tcp6Announce {
86+
Event::TcpAnnounce {
10587
connection: ConnectionContext {
10688
client_ip_addr: IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
10789
server_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070),
@@ -121,7 +103,7 @@ mod tests {
121103
let stats_repository = Repository::new();
122104

123105
handle_event(
124-
Event::Tcp6Scrape {
106+
Event::TcpScrape {
125107
connection: ConnectionContext {
126108
client_ip_addr: IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
127109
server_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070),

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ pub mod sender;
66

77
/// An statistics event. It is used to collect tracker metrics.
88
///
9-
/// - `Tcp` prefix means the event was triggered by the HTTP tracker
10-
/// - `4` or `6` prefixes means the IP version used by the peer
11-
/// - Finally the event suffix is the type of request: `announce` or `scrape`
9+
/// - `Tcp` prefix means the event was triggered by the HTTP tracker.
10+
/// - The event suffix is the type of request: `announce` or `scrape`.
1211
#[derive(Debug, PartialEq, Eq)]
1312
pub enum Event {
14-
Tcp4Announce { connection: ConnectionContext },
15-
Tcp4Scrape { connection: ConnectionContext },
16-
Tcp6Announce { connection: ConnectionContext },
17-
Tcp6Scrape { connection: ConnectionContext },
13+
TcpAnnounce { connection: ConnectionContext },
14+
TcpScrape { connection: ConnectionContext },
1815
}
1916

2017
#[derive(Debug, PartialEq, Eq)]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mod tests {
7373
let event_sender = stats_tracker.run_event_listener();
7474

7575
let result = event_sender
76-
.send_event(Event::Tcp4Announce {
76+
.send_event(Event::TcpAnnounce {
7777
connection: ConnectionContext {
7878
client_ip_addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)),
7979
server_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070),

0 commit comments

Comments
 (0)