wlan_telemetry/util/
sender.rs1use fuchsia_sync::Mutex;
6use futures::channel::mpsc;
7use log::{info, warn};
8use std::sync::atomic::{AtomicBool, Ordering};
9use std::sync::Arc;
10
11pub const TELEMETRY_EVENT_BUFFER_SIZE: usize = 100;
14
15#[derive(Clone, Debug)]
16pub struct TelemetrySender {
17 sender: Arc<Mutex<mpsc::Sender<crate::TelemetryEvent>>>,
18 sender_is_blocked: Arc<AtomicBool>,
19}
20
21impl TelemetrySender {
22 pub fn new(sender: mpsc::Sender<crate::TelemetryEvent>) -> Self {
23 Self {
24 sender: Arc::new(Mutex::new(sender)),
25 sender_is_blocked: Arc::new(AtomicBool::new(false)),
26 }
27 }
28
29 pub fn send(&self, event: crate::TelemetryEvent) {
31 match self.sender.lock().try_send(event) {
32 Ok(_) => {
33 if self
35 .sender_is_blocked
36 .compare_exchange(true, false, Ordering::SeqCst, Ordering::SeqCst)
37 .is_ok()
38 {
39 info!("TelemetrySender recovered and resumed sending");
40 }
41 }
42 Err(_) => {
43 if self
45 .sender_is_blocked
46 .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
47 .is_ok()
48 {
49 warn!("TelemetrySender dropped a msg: either buffer is full or no receiver is waiting");
50 }
51 }
52 }
53 }
54}