forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.rs
More file actions
136 lines (121 loc) · 4.3 KB
/
statistics.rs
File metadata and controls
136 lines (121 loc) · 4.3 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::sync::Arc;
use tokio::sync::{mpsc, RwLock, RwLockReadGuard};
use tokio::sync::mpsc::Sender;
use tokio::sync::mpsc::error::SendError;
const CHANNEL_BUFFER_SIZE: usize = 65_535;
#[derive(Debug)]
pub enum TrackerStatisticsEvent {
Tcp4Announce,
Tcp4Scrape,
Tcp6Announce,
Tcp6Scrape,
Udp4Connect,
Udp4Announce,
Udp4Scrape,
Udp6Connect,
Udp6Announce,
Udp6Scrape,
}
#[derive(Debug)]
pub struct TrackerStatistics {
pub tcp4_connections_handled: u64,
pub tcp4_announces_handled: u64,
pub tcp4_scrapes_handled: u64,
pub tcp6_connections_handled: u64,
pub tcp6_announces_handled: u64,
pub tcp6_scrapes_handled: u64,
pub udp4_connections_handled: u64,
pub udp4_announces_handled: u64,
pub udp4_scrapes_handled: u64,
pub udp6_connections_handled: u64,
pub udp6_announces_handled: u64,
pub udp6_scrapes_handled: u64,
}
impl TrackerStatistics {
pub fn new() -> Self {
Self {
tcp4_connections_handled: 0,
tcp4_announces_handled: 0,
tcp4_scrapes_handled: 0,
tcp6_connections_handled: 0,
tcp6_announces_handled: 0,
tcp6_scrapes_handled: 0,
udp4_connections_handled: 0,
udp4_announces_handled: 0,
udp4_scrapes_handled: 0,
udp6_connections_handled: 0,
udp6_announces_handled: 0,
udp6_scrapes_handled: 0,
}
}
}
pub struct StatsTracker {
channel_sender: Option<Sender<TrackerStatisticsEvent>>,
pub stats: Arc<RwLock<TrackerStatistics>>,
}
impl StatsTracker {
pub fn new() -> Self {
Self {
channel_sender: None,
stats: Arc::new(RwLock::new(TrackerStatistics::new())),
}
}
pub async fn get_stats(&self) -> RwLockReadGuard<'_, TrackerStatistics> {
self.stats.read().await
}
pub async fn send_event(&self, event: TrackerStatisticsEvent) -> Option<Result<(), SendError<TrackerStatisticsEvent>>> {
if let Some(tx) = &self.channel_sender {
Some(tx.send(event).await)
} else {
None
}
}
pub fn run_worker(&mut self) {
let (tx, mut rx) = mpsc::channel::<TrackerStatisticsEvent>(CHANNEL_BUFFER_SIZE);
// set send channel on stats_tracker
self.channel_sender = Some(tx);
let stats = self.stats.clone();
tokio::spawn(async move {
while let Some(event) = rx.recv().await {
let mut stats_lock = stats.write().await;
match event {
TrackerStatisticsEvent::Tcp4Announce => {
stats_lock.tcp4_announces_handled += 1;
stats_lock.tcp4_connections_handled += 1;
}
TrackerStatisticsEvent::Tcp4Scrape => {
stats_lock.tcp4_scrapes_handled += 1;
stats_lock.tcp4_connections_handled += 1;
}
TrackerStatisticsEvent::Tcp6Announce => {
stats_lock.tcp6_announces_handled += 1;
stats_lock.tcp6_connections_handled += 1;
}
TrackerStatisticsEvent::Tcp6Scrape => {
stats_lock.tcp6_scrapes_handled += 1;
stats_lock.tcp6_connections_handled += 1;
}
TrackerStatisticsEvent::Udp4Connect => {
stats_lock.udp4_connections_handled += 1;
}
TrackerStatisticsEvent::Udp4Announce => {
stats_lock.udp4_announces_handled += 1;
}
TrackerStatisticsEvent::Udp4Scrape => {
stats_lock.udp4_scrapes_handled += 1;
}
TrackerStatisticsEvent::Udp6Connect => {
stats_lock.udp6_connections_handled += 1;
}
TrackerStatisticsEvent::Udp6Announce => {
stats_lock.udp6_announces_handled += 1;
}
TrackerStatisticsEvent::Udp6Scrape => {
stats_lock.udp6_scrapes_handled += 1;
}
}
drop(stats_lock);
}
});
}
}