forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.rs
More file actions
47 lines (41 loc) · 1.4 KB
/
handler.rs
File metadata and controls
47 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::sync::Arc;
use bittorrent_udp_tracker_core::services::banning::BanService;
use tokio::sync::RwLock;
use torrust_tracker_metrics::label::LabelSet;
use torrust_tracker_metrics::metric_name;
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use crate::event::{ErrorKind, Event};
use crate::statistics::repository::Repository;
use crate::statistics::UDP_TRACKER_SERVER_IPS_BANNED_TOTAL;
pub async fn handle_event(
event: Event,
ban_service: &Arc<RwLock<BanService>>,
repository: &Repository,
now: DurationSinceUnixEpoch,
) {
if let Event::UdpError {
context,
kind: _,
error: ErrorKind::ConnectionCookie(_msg),
} = event
{
let mut ban_service = ban_service.write().await;
ban_service.increase_counter(&context.client_socket_addr().ip());
update_metric_for_banned_ips_total(repository, ban_service.get_banned_ips_total(), now).await;
}
}
#[allow(clippy::cast_precision_loss)]
async fn update_metric_for_banned_ips_total(repository: &Repository, ips_banned_total: usize, now: DurationSinceUnixEpoch) {
match repository
.set_gauge(
&metric_name!(UDP_TRACKER_SERVER_IPS_BANNED_TOTAL),
&LabelSet::default(),
ips_banned_total as f64,
now,
)
.await
{
Ok(()) => {}
Err(err) => tracing::error!("Failed to increase the counter: {}", err),
}
}