Skip to content

Commit 74ffa4c

Browse files
committed
refactor: [torrust#1382] error request kind in UDP req does not make sense
1 parent df507a8 commit 74ffa4c

File tree

7 files changed

+77
-60
lines changed

7 files changed

+77
-60
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ impl ConnectionContext {
3030
}
3131
}
3232

33+
#[must_use]
3334
pub fn client_socket_addr(&self) -> SocketAddr {
3435
self.client_socket_addr
3536
}
3637

38+
#[must_use]
3739
pub fn server_socket_addr(&self) -> SocketAddr {
3840
self.server_socket_addr
3941
}

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use zerocopy::network_endian::I32;
1616

1717
use crate::error::Error;
1818
use crate::statistics as server_statistics;
19-
use crate::statistics::event::UdpResponseKind;
19+
use crate::statistics::event::UdpRequestKind;
2020

2121
/// It handles the `Announce` request.
2222
///
@@ -45,14 +45,14 @@ pub async fn handle_announce(
4545
IpAddr::V4(_) => {
4646
udp_server_stats_event_sender
4747
.send_event(server_statistics::event::Event::Udp4Request {
48-
kind: UdpResponseKind::Announce,
48+
kind: UdpRequestKind::Announce,
4949
})
5050
.await;
5151
}
5252
IpAddr::V6(_) => {
5353
udp_server_stats_event_sender
5454
.send_event(server_statistics::event::Event::Udp6Request {
55-
kind: UdpResponseKind::Announce,
55+
kind: UdpRequestKind::Announce,
5656
})
5757
.await;
5858
}
@@ -226,7 +226,7 @@ mod tests {
226226
TorrentPeerBuilder,
227227
};
228228
use crate::statistics as server_statistics;
229-
use crate::statistics::event::UdpResponseKind;
229+
use crate::statistics::event::UdpRequestKind;
230230

231231
#[tokio::test]
232232
async fn an_announced_peer_should_be_added_to_the_tracker() {
@@ -433,7 +433,7 @@ mod tests {
433433
udp_server_stats_event_sender_mock
434434
.expect_send_event()
435435
.with(eq(server_statistics::event::Event::Udp4Request {
436-
kind: UdpResponseKind::Announce,
436+
kind: UdpRequestKind::Announce,
437437
}))
438438
.times(1)
439439
.returning(|_| Box::pin(future::ready(Some(Ok(())))));
@@ -549,7 +549,7 @@ mod tests {
549549
sample_issue_time, MockUdpServerStatsEventSender, TorrentPeerBuilder,
550550
};
551551
use crate::statistics as server_statistics;
552-
use crate::statistics::event::UdpResponseKind;
552+
use crate::statistics::event::UdpRequestKind;
553553

554554
#[tokio::test]
555555
async fn an_announced_peer_should_be_added_to_the_tracker() {
@@ -775,7 +775,7 @@ mod tests {
775775
udp_server_stats_event_sender_mock
776776
.expect_send_event()
777777
.with(eq(server_statistics::event::Event::Udp6Request {
778-
kind: UdpResponseKind::Announce,
778+
kind: UdpRequestKind::Announce,
779779
}))
780780
.times(1)
781781
.returning(|_| Box::pin(future::ready(Some(Ok(())))));
@@ -830,7 +830,7 @@ mod tests {
830830
TrackerConfigurationBuilder,
831831
};
832832
use crate::statistics as server_statistics;
833-
use crate::statistics::event::UdpResponseKind;
833+
use crate::statistics::event::UdpRequestKind;
834834

835835
#[tokio::test]
836836
async fn the_peer_ip_should_be_changed_to_the_external_ip_in_the_tracker_configuration() {
@@ -871,7 +871,7 @@ mod tests {
871871
udp_server_stats_event_sender_mock
872872
.expect_send_event()
873873
.with(eq(server_statistics::event::Event::Udp6Request {
874-
kind: UdpResponseKind::Announce,
874+
kind: UdpRequestKind::Announce,
875875
}))
876876
.times(1)
877877
.returning(|_| Box::pin(future::ready(Some(Ok(())))));

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bittorrent_udp_tracker_core::services::connect::ConnectService;
77
use tracing::{instrument, Level};
88

99
use crate::statistics as server_statistics;
10-
use crate::statistics::event::UdpResponseKind;
10+
use crate::statistics::event::UdpRequestKind;
1111

1212
/// It handles the `Connect` request.
1313
#[instrument(fields(transaction_id), skip(connect_service, opt_udp_server_stats_event_sender), ret(level = Level::TRACE))]
@@ -27,14 +27,14 @@ pub async fn handle_connect(
2727
IpAddr::V4(_) => {
2828
udp_server_stats_event_sender
2929
.send_event(server_statistics::event::Event::Udp4Request {
30-
kind: UdpResponseKind::Connect,
30+
kind: UdpRequestKind::Connect,
3131
})
3232
.await;
3333
}
3434
IpAddr::V6(_) => {
3535
udp_server_stats_event_sender
3636
.send_event(server_statistics::event::Event::Udp6Request {
37-
kind: UdpResponseKind::Connect,
37+
kind: UdpRequestKind::Connect,
3838
})
3939
.await;
4040
}
@@ -79,7 +79,7 @@ mod tests {
7979
sample_ipv6_remote_addr_fingerprint, sample_issue_time, MockUdpCoreStatsEventSender, MockUdpServerStatsEventSender,
8080
};
8181
use crate::statistics as server_statistics;
82-
use crate::statistics::event::UdpResponseKind;
82+
use crate::statistics::event::UdpRequestKind;
8383

8484
fn sample_connect_request() -> ConnectRequest {
8585
ConnectRequest {
@@ -215,7 +215,7 @@ mod tests {
215215
udp_server_stats_event_sender_mock
216216
.expect_send_event()
217217
.with(eq(server_statistics::event::Event::Udp4Request {
218-
kind: UdpResponseKind::Connect,
218+
kind: UdpRequestKind::Connect,
219219
}))
220220
.times(1)
221221
.returning(|_| Box::pin(future::ready(Some(Ok(())))));
@@ -255,7 +255,7 @@ mod tests {
255255
udp_server_stats_event_sender_mock
256256
.expect_send_event()
257257
.with(eq(server_statistics::event::Event::Udp6Request {
258-
kind: UdpResponseKind::Connect,
258+
kind: UdpRequestKind::Connect,
259259
}))
260260
.times(1)
261261
.returning(|_| Box::pin(future::ready(Some(Ok(())))));

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use zerocopy::network_endian::I32;
1414

1515
use crate::error::Error;
1616
use crate::statistics as server_statistics;
17-
use crate::statistics::event::UdpResponseKind;
17+
use crate::statistics::event::UdpRequestKind;
1818

1919
/// It handles the `Scrape` request.
2020
///
@@ -41,14 +41,14 @@ pub async fn handle_scrape(
4141
IpAddr::V4(_) => {
4242
udp_server_stats_event_sender
4343
.send_event(server_statistics::event::Event::Udp4Request {
44-
kind: UdpResponseKind::Scrape,
44+
kind: UdpRequestKind::Scrape,
4545
})
4646
.await;
4747
}
4848
IpAddr::V6(_) => {
4949
udp_server_stats_event_sender
5050
.send_event(server_statistics::event::Event::Udp6Request {
51-
kind: UdpResponseKind::Scrape,
51+
kind: UdpRequestKind::Scrape,
5252
})
5353
.await;
5454
}
@@ -375,7 +375,7 @@ mod tests {
375375
udp_server_stats_event_sender_mock
376376
.expect_send_event()
377377
.with(eq(server_statistics::event::Event::Udp4Request {
378-
kind: server_statistics::event::UdpResponseKind::Scrape,
378+
kind: server_statistics::event::UdpRequestKind::Scrape,
379379
}))
380380
.times(1)
381381
.returning(|_| Box::pin(future::ready(Some(Ok(())))));
@@ -422,7 +422,7 @@ mod tests {
422422
udp_server_stats_event_sender_mock
423423
.expect_send_event()
424424
.with(eq(server_statistics::event::Event::Udp6Request {
425-
kind: server_statistics::event::UdpResponseKind::Scrape,
425+
kind: server_statistics::event::UdpRequestKind::Scrape,
426426
}))
427427
.times(1)
428428
.returning(|_| Box::pin(future::ready(Some(Ok(())))));

packages/udp-tracker-server/src/server/processor.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ impl Processor {
6969
};
7070

7171
let udp_response_kind = match &response {
72-
Response::Connect(_) => statistics::event::UdpResponseKind::Connect,
73-
Response::AnnounceIpv4(_) | Response::AnnounceIpv6(_) => statistics::event::UdpResponseKind::Announce,
74-
Response::Scrape(_) => statistics::event::UdpResponseKind::Scrape,
72+
Response::Connect(_) => statistics::event::UdpResponseKind::Ok {
73+
req_kind: statistics::event::UdpRequestKind::Connect,
74+
},
75+
Response::AnnounceIpv4(_) | Response::AnnounceIpv6(_) => statistics::event::UdpResponseKind::Ok {
76+
req_kind: statistics::event::UdpRequestKind::Announce,
77+
},
78+
Response::Scrape(_) => statistics::event::UdpResponseKind::Ok {
79+
req_kind: statistics::event::UdpRequestKind::Scrape,
80+
},
7581
Response::Error(_e) => statistics::event::UdpResponseKind::Error,
7682
};
7783

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

+37-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::statistics::event::{Event, UdpResponseKind};
1+
use crate::statistics::event::{Event, UdpRequestKind, UdpResponseKind};
22
use crate::statistics::repository::Repository;
33

44
pub async fn handle_event(event: Event, stats_repository: &Repository) {
@@ -16,16 +16,15 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
1616
stats_repository.increase_udp4_requests().await;
1717
}
1818
Event::Udp4Request { kind } => match kind {
19-
UdpResponseKind::Connect => {
19+
UdpRequestKind::Connect => {
2020
stats_repository.increase_udp4_connections().await;
2121
}
22-
UdpResponseKind::Announce => {
22+
UdpRequestKind::Announce => {
2323
stats_repository.increase_udp4_announces().await;
2424
}
25-
UdpResponseKind::Scrape => {
25+
UdpRequestKind::Scrape => {
2626
stats_repository.increase_udp4_scrapes().await;
2727
}
28-
UdpResponseKind::Error => {}
2928
},
3029
Event::Udp4Response {
3130
kind,
@@ -34,21 +33,23 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
3433
stats_repository.increase_udp4_responses().await;
3534

3635
match kind {
37-
UdpResponseKind::Connect => {
38-
stats_repository
39-
.recalculate_udp_avg_connect_processing_time_ns(req_processing_time)
40-
.await;
41-
}
42-
UdpResponseKind::Announce => {
43-
stats_repository
44-
.recalculate_udp_avg_announce_processing_time_ns(req_processing_time)
45-
.await;
46-
}
47-
UdpResponseKind::Scrape => {
48-
stats_repository
49-
.recalculate_udp_avg_scrape_processing_time_ns(req_processing_time)
50-
.await;
51-
}
36+
UdpResponseKind::Ok { req_kind } => match req_kind {
37+
UdpRequestKind::Connect => {
38+
stats_repository
39+
.recalculate_udp_avg_connect_processing_time_ns(req_processing_time)
40+
.await;
41+
}
42+
UdpRequestKind::Announce => {
43+
stats_repository
44+
.recalculate_udp_avg_announce_processing_time_ns(req_processing_time)
45+
.await;
46+
}
47+
UdpRequestKind::Scrape => {
48+
stats_repository
49+
.recalculate_udp_avg_scrape_processing_time_ns(req_processing_time)
50+
.await;
51+
}
52+
},
5253
UdpResponseKind::Error => {}
5354
}
5455
}
@@ -61,16 +62,15 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
6162
stats_repository.increase_udp6_requests().await;
6263
}
6364
Event::Udp6Request { kind } => match kind {
64-
UdpResponseKind::Connect => {
65+
UdpRequestKind::Connect => {
6566
stats_repository.increase_udp6_connections().await;
6667
}
67-
UdpResponseKind::Announce => {
68+
UdpRequestKind::Announce => {
6869
stats_repository.increase_udp6_announces().await;
6970
}
70-
UdpResponseKind::Scrape => {
71+
UdpRequestKind::Scrape => {
7172
stats_repository.increase_udp6_scrapes().await;
7273
}
73-
UdpResponseKind::Error => {}
7474
},
7575
Event::Udp6Response {
7676
kind: _,
@@ -89,7 +89,7 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
8989
#[cfg(test)]
9090
mod tests {
9191
use crate::statistics::event::handler::handle_event;
92-
use crate::statistics::event::Event;
92+
use crate::statistics::event::{Event, UdpRequestKind};
9393
use crate::statistics::repository::Repository;
9494

9595
#[tokio::test]
@@ -148,7 +148,7 @@ mod tests {
148148

149149
handle_event(
150150
Event::Udp4Request {
151-
kind: crate::statistics::event::UdpResponseKind::Connect,
151+
kind: crate::statistics::event::UdpRequestKind::Connect,
152152
},
153153
&stats_repository,
154154
)
@@ -165,7 +165,7 @@ mod tests {
165165

166166
handle_event(
167167
Event::Udp4Request {
168-
kind: crate::statistics::event::UdpResponseKind::Announce,
168+
kind: crate::statistics::event::UdpRequestKind::Announce,
169169
},
170170
&stats_repository,
171171
)
@@ -182,7 +182,7 @@ mod tests {
182182

183183
handle_event(
184184
Event::Udp4Request {
185-
kind: crate::statistics::event::UdpResponseKind::Scrape,
185+
kind: crate::statistics::event::UdpRequestKind::Scrape,
186186
},
187187
&stats_repository,
188188
)
@@ -199,7 +199,9 @@ mod tests {
199199

200200
handle_event(
201201
Event::Udp4Response {
202-
kind: crate::statistics::event::UdpResponseKind::Announce,
202+
kind: crate::statistics::event::UdpResponseKind::Ok {
203+
req_kind: UdpRequestKind::Announce,
204+
},
203205
req_processing_time: std::time::Duration::from_secs(1),
204206
},
205207
&stats_repository,
@@ -228,7 +230,7 @@ mod tests {
228230

229231
handle_event(
230232
Event::Udp6Request {
231-
kind: crate::statistics::event::UdpResponseKind::Connect,
233+
kind: crate::statistics::event::UdpRequestKind::Connect,
232234
},
233235
&stats_repository,
234236
)
@@ -245,7 +247,7 @@ mod tests {
245247

246248
handle_event(
247249
Event::Udp6Request {
248-
kind: crate::statistics::event::UdpResponseKind::Announce,
250+
kind: crate::statistics::event::UdpRequestKind::Announce,
249251
},
250252
&stats_repository,
251253
)
@@ -262,7 +264,7 @@ mod tests {
262264

263265
handle_event(
264266
Event::Udp6Request {
265-
kind: crate::statistics::event::UdpResponseKind::Scrape,
267+
kind: crate::statistics::event::UdpRequestKind::Scrape,
266268
},
267269
&stats_repository,
268270
)
@@ -279,7 +281,9 @@ mod tests {
279281

280282
handle_event(
281283
Event::Udp6Response {
282-
kind: crate::statistics::event::UdpResponseKind::Announce,
284+
kind: crate::statistics::event::UdpResponseKind::Ok {
285+
req_kind: UdpRequestKind::Announce,
286+
},
283287
req_processing_time: std::time::Duration::from_secs(1),
284288
},
285289
&stats_repository,

0 commit comments

Comments
 (0)