solana_core/
stats_reporter_service.rs1use {
2 crossbeam_channel::{Receiver, RecvTimeoutError},
3 std::{
4 result::Result,
5 sync::{
6 Arc,
7 atomic::{AtomicBool, Ordering},
8 },
9 thread::{self, Builder, JoinHandle},
10 time::Duration,
11 },
12};
13
14pub struct StatsReporterService {
15 thread_hdl: JoinHandle<()>,
16}
17
18impl StatsReporterService {
19 pub fn new(
20 reporting_receiver: Receiver<Box<dyn FnOnce() + Send>>,
21 exit: Arc<AtomicBool>,
22 ) -> Self {
23 let thread_hdl = Builder::new()
24 .name("solStatsReport".to_owned())
25 .spawn(move || {
26 loop {
27 if exit.load(Ordering::Relaxed) {
28 return;
29 }
30 if let Err(e) = Self::receive_reporting_func(&reporting_receiver) {
31 match e {
32 RecvTimeoutError::Disconnected => break,
33 RecvTimeoutError::Timeout => (),
34 }
35 }
36 }
37 })
38 .unwrap();
39
40 Self { thread_hdl }
41 }
42
43 pub fn join(self) -> thread::Result<()> {
44 self.thread_hdl.join()?;
45 Ok(())
46 }
47
48 fn receive_reporting_func(
49 r: &Receiver<Box<dyn FnOnce() + Send>>,
50 ) -> Result<(), RecvTimeoutError> {
51 let timer = Duration::new(1, 0);
52 let func = r.recv_timeout(timer)?;
53 func();
54 Ok(())
55 }
56}