forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_reporter_service.rs
55 lines (50 loc) · 1.4 KB
/
stats_reporter_service.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
51
52
53
54
55
use {
crossbeam_channel::{Receiver, RecvTimeoutError},
std::{
result::Result,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread::{self, Builder, JoinHandle},
time::Duration,
},
};
pub struct StatsReporterService {
thread_hdl: JoinHandle<()>,
}
impl StatsReporterService {
pub fn new(
reporting_receiver: Receiver<Box<dyn FnOnce() + Send>>,
exit: &Arc<AtomicBool>,
) -> Self {
let exit = exit.clone();
let thread_hdl = Builder::new()
.name("solana-stats-reporter".to_owned())
.spawn(move || loop {
if exit.load(Ordering::Relaxed) {
return;
}
if let Err(e) = Self::receive_reporting_func(&reporting_receiver) {
match e {
RecvTimeoutError::Disconnected => break,
RecvTimeoutError::Timeout => (),
}
}
})
.unwrap();
Self { thread_hdl }
}
pub fn join(self) -> thread::Result<()> {
self.thread_hdl.join()?;
Ok(())
}
fn receive_reporting_func(
r: &Receiver<Box<dyn FnOnce() + Send>>,
) -> Result<(), RecvTimeoutError> {
let timer = Duration::new(1, 0);
let func = r.recv_timeout(timer)?;
func();
Ok(())
}
}