forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeeper.rs
50 lines (39 loc) · 1.24 KB
/
keeper.rs
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
use tokio::sync::broadcast::Receiver;
use super::event::listener::dispatch_events;
use super::event::Event;
use super::repository::Repository;
/// The service responsible for keeping tracker metrics (listening to statistics events and handle them).
///
/// It actively listen to new statistics events. When it receives a new event
/// it accordingly increases the counters.
pub struct Keeper {
pub repository: Repository,
}
impl Default for Keeper {
fn default() -> Self {
Self::new()
}
}
impl Keeper {
#[must_use]
pub fn new() -> Self {
Self {
repository: Repository::new(),
}
}
pub fn run_event_listener(&mut self, receiver: Receiver<Event>) {
let stats_repository = self.repository.clone();
tokio::spawn(async move { dispatch_events(receiver, stats_repository).await });
}
}
#[cfg(test)]
mod tests {
use crate::statistics::keeper::Keeper;
use crate::statistics::metrics::Metrics;
#[tokio::test]
async fn should_contain_the_tracker_statistics() {
let stats_tracker = Keeper::new();
let stats = stats_tracker.repository.get_stats().await;
assert_eq!(stats.tcp4_announces_handled, Metrics::default().tcp4_announces_handled);
}
}