Skip to content

Commit 8603f8b

Browse files
committed
refactor: [#1380] refactor: [#1373] merge UDP stats events with different IP version
1 parent 2be682e commit 8603f8b

File tree

8 files changed

+44
-87
lines changed

8 files changed

+44
-87
lines changed

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

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//!
88
//! It also sends an [`udp_tracker_core::statistics::event::Event`]
99
//! because events are specific for the HTTP tracker.
10-
use std::net::{IpAddr, SocketAddr};
10+
use std::net::SocketAddr;
1111
use std::ops::Range;
1212
use std::sync::Arc;
1313

@@ -103,16 +103,11 @@ impl AnnounceService {
103103

104104
async fn send_stats_event(&self, client_socket_addr: SocketAddr, server_socket_addr: SocketAddr) {
105105
if let Some(udp_stats_event_sender) = self.opt_udp_core_stats_event_sender.as_deref() {
106-
let event = match client_socket_addr.ip() {
107-
IpAddr::V4(_) => statistics::event::Event::Udp4Announce {
106+
udp_stats_event_sender
107+
.send_event(statistics::event::Event::UdpAnnounce {
108108
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
109-
},
110-
IpAddr::V6(_) => statistics::event::Event::Udp6Announce {
111-
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
112-
},
113-
};
114-
115-
udp_stats_event_sender.send_event(event).await;
109+
})
110+
.await;
116111
}
117112
}
118113
}

packages/udp-tracker-core/src/services/connect.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,11 @@ impl ConnectService {
4141
make(gen_remote_fingerprint(&client_socket_addr), cookie_issue_time).expect("it should be a normal value");
4242

4343
if let Some(udp_stats_event_sender) = self.opt_udp_core_stats_event_sender.as_deref() {
44-
match client_socket_addr {
45-
SocketAddr::V4(_) => {
46-
udp_stats_event_sender
47-
.send_event(statistics::event::Event::Udp4Connect {
48-
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
49-
})
50-
.await;
51-
}
52-
SocketAddr::V6(_) => {
53-
udp_stats_event_sender
54-
.send_event(statistics::event::Event::Udp6Connect {
55-
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
56-
})
57-
.await;
58-
}
59-
}
44+
udp_stats_event_sender
45+
.send_event(statistics::event::Event::UdpConnect {
46+
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
47+
})
48+
.await;
6049
}
6150

6251
connection_id
@@ -149,7 +138,7 @@ mod tests {
149138
let mut udp_stats_event_sender_mock = MockUdpCoreStatsEventSender::new();
150139
udp_stats_event_sender_mock
151140
.expect_send_event()
152-
.with(eq(statistics::event::Event::Udp4Connect {
141+
.with(eq(statistics::event::Event::UdpConnect {
153142
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
154143
}))
155144
.times(1)
@@ -172,7 +161,7 @@ mod tests {
172161
let mut udp_stats_event_sender_mock = MockUdpCoreStatsEventSender::new();
173162
udp_stats_event_sender_mock
174163
.expect_send_event()
175-
.with(eq(statistics::event::Event::Udp6Connect {
164+
.with(eq(statistics::event::Event::UdpConnect {
176165
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
177166
}))
178167
.times(1)

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,11 @@ impl ScrapeService {
8585

8686
async fn send_stats_event(&self, client_socket_addr: SocketAddr, server_socket_addr: SocketAddr) {
8787
if let Some(udp_stats_event_sender) = self.opt_udp_stats_event_sender.as_deref() {
88-
let event = match client_socket_addr {
89-
SocketAddr::V4(_) => statistics::event::Event::Udp4Scrape {
88+
udp_stats_event_sender
89+
.send_event(statistics::event::Event::UdpScrape {
9090
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
91-
},
92-
SocketAddr::V6(_) => statistics::event::Event::Udp6Scrape {
93-
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
94-
},
95-
};
96-
udp_stats_event_sender.send_event(event).await;
91+
})
92+
.await;
9793
}
9894
}
9995
}

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

+11-38
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,25 @@ use crate::statistics::repository::Repository;
66
/// This function panics if the IP version does not match the event type.
77
pub async fn handle_event(event: Event, stats_repository: &Repository) {
88
match event {
9-
// UDP4
10-
Event::Udp4Connect { context } => match context.client_socket_addr.ip() {
9+
Event::UdpConnect { context } => match context.client_socket_addr.ip() {
1110
std::net::IpAddr::V4(_) => {
1211
stats_repository.increase_udp4_connections().await;
1312
}
14-
std::net::IpAddr::V6(_) => {
15-
panic!("IP Version 6 does not match the event type for connect");
16-
}
17-
},
18-
Event::Udp4Announce { context } => match context.client_socket_addr.ip() {
19-
std::net::IpAddr::V4(_) => {
20-
stats_repository.increase_udp4_announces().await;
21-
}
22-
std::net::IpAddr::V6(_) => {
23-
panic!("IP Version 6 does not match the event type for announce");
24-
}
25-
},
26-
Event::Udp4Scrape { context } => match context.client_socket_addr.ip() {
27-
std::net::IpAddr::V4(_) => {
28-
stats_repository.increase_udp4_scrapes().await;
29-
}
30-
std::net::IpAddr::V6(_) => {
31-
panic!("IP Version 6 does not match the event type for scrape");
32-
}
33-
},
34-
35-
// UDP6
36-
Event::Udp6Connect { context } => match context.client_socket_addr.ip() {
37-
std::net::IpAddr::V4(_) => {
38-
panic!("IP Version 4 does not match the event type for connect");
39-
}
4013
std::net::IpAddr::V6(_) => {
4114
stats_repository.increase_udp6_connections().await;
4215
}
4316
},
44-
Event::Udp6Announce { context } => match context.client_socket_addr.ip() {
17+
Event::UdpAnnounce { context } => match context.client_socket_addr.ip() {
4518
std::net::IpAddr::V4(_) => {
46-
panic!("IP Version 4 does not match the event type for announce");
19+
stats_repository.increase_udp4_announces().await;
4720
}
4821
std::net::IpAddr::V6(_) => {
4922
stats_repository.increase_udp6_announces().await;
5023
}
5124
},
52-
Event::Udp6Scrape { context } => match context.client_socket_addr.ip() {
25+
Event::UdpScrape { context } => match context.client_socket_addr.ip() {
5326
std::net::IpAddr::V4(_) => {
54-
panic!("IP Version 4 does not match the event type for scrape");
27+
stats_repository.increase_udp4_scrapes().await;
5528
}
5629
std::net::IpAddr::V6(_) => {
5730
stats_repository.increase_udp6_scrapes().await;
@@ -75,7 +48,7 @@ mod tests {
7548
let stats_repository = Repository::new();
7649

7750
handle_event(
78-
Event::Udp4Connect {
51+
Event::UdpConnect {
7952
context: ConnectionContext::new(
8053
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195)), 8080),
8154
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 196)), 6969),
@@ -95,7 +68,7 @@ mod tests {
9568
let stats_repository = Repository::new();
9669

9770
handle_event(
98-
Event::Udp4Announce {
71+
Event::UdpAnnounce {
9972
context: ConnectionContext::new(
10073
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195)), 8080),
10174
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 196)), 6969),
@@ -115,7 +88,7 @@ mod tests {
11588
let stats_repository = Repository::new();
11689

11790
handle_event(
118-
Event::Udp4Scrape {
91+
Event::UdpScrape {
11992
context: ConnectionContext::new(
12093
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195)), 8080),
12194
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 196)), 6969),
@@ -135,7 +108,7 @@ mod tests {
135108
let stats_repository = Repository::new();
136109

137110
handle_event(
138-
Event::Udp6Connect {
111+
Event::UdpConnect {
139112
context: ConnectionContext::new(
140113
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 195)), 8080),
141114
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 196)), 6969),
@@ -155,7 +128,7 @@ mod tests {
155128
let stats_repository = Repository::new();
156129

157130
handle_event(
158-
Event::Udp6Announce {
131+
Event::UdpAnnounce {
159132
context: ConnectionContext::new(
160133
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 195)), 8080),
161134
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 196)), 6969),
@@ -175,7 +148,7 @@ mod tests {
175148
let stats_repository = Repository::new();
176149

177150
handle_event(
178-
Event::Udp6Scrape {
151+
Event::UdpScrape {
179152
context: ConnectionContext::new(
180153
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 195)), 8080),
181154
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 203, 0, 113, 196)), 6969),

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@ pub mod sender;
66

77
/// An statistics event. It is used to collect tracker metrics.
88
///
9-
/// - `Udp` prefix means the event was triggered by the UDP 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`, `scrape` or `connection`
9+
/// - `Udp` prefix means the event was triggered by the UDP tracker.
10+
/// - The event suffix is the type of request: `announce`, `scrape` or `connection`.
1211
#[derive(Debug, PartialEq, Eq)]
1312
pub enum Event {
14-
Udp4Connect { context: ConnectionContext },
15-
Udp4Announce { context: ConnectionContext },
16-
Udp4Scrape { context: ConnectionContext },
17-
Udp6Connect { context: ConnectionContext },
18-
Udp6Announce { context: ConnectionContext },
19-
Udp6Scrape { context: ConnectionContext },
13+
UdpConnect { context: ConnectionContext },
14+
UdpAnnounce { context: ConnectionContext },
15+
UdpScrape { context: ConnectionContext },
2016
}
2117

2218
#[derive(Debug, PartialEq, Eq)]
@@ -33,4 +29,12 @@ impl ConnectionContext {
3329
server_socket_addr,
3430
}
3531
}
32+
33+
pub fn client_socket_addr(&self) -> SocketAddr {
34+
self.client_socket_addr
35+
}
36+
37+
pub fn server_socket_addr(&self) -> SocketAddr {
38+
self.server_socket_addr
39+
}
3640
}

packages/udp-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::Udp4Connect {
76+
.send_event(Event::UdpConnect {
7777
context: ConnectionContext::new(
7878
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 195)), 8080),
7979
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 196)), 6969),

packages/udp-tracker-server/src/handlers/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ mod tests {
859859
let mut udp_core_stats_event_sender_mock = MockUdpCoreStatsEventSender::new();
860860
udp_core_stats_event_sender_mock
861861
.expect_send_event()
862-
.with(eq(core_statistics::event::Event::Udp6Announce {
862+
.with(eq(core_statistics::event::Event::UdpAnnounce {
863863
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
864864
}))
865865
.times(1)

packages/udp-tracker-server/src/handlers/connect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ mod tests {
203203
let mut udp_core_stats_event_sender_mock = MockUdpCoreStatsEventSender::new();
204204
udp_core_stats_event_sender_mock
205205
.expect_send_event()
206-
.with(eq(core_statistics::event::Event::Udp4Connect {
206+
.with(eq(core_statistics::event::Event::UdpConnect {
207207
context: core_statistics::event::ConnectionContext::new(client_socket_addr, server_socket_addr),
208208
}))
209209
.times(1)
@@ -243,7 +243,7 @@ mod tests {
243243
let mut udp_core_stats_event_sender_mock = MockUdpCoreStatsEventSender::new();
244244
udp_core_stats_event_sender_mock
245245
.expect_send_event()
246-
.with(eq(core_statistics::event::Event::Udp6Connect {
246+
.with(eq(core_statistics::event::Event::UdpConnect {
247247
context: ConnectionContext::new(client_socket_addr, server_socket_addr),
248248
}))
249249
.times(1)

0 commit comments

Comments
 (0)