viser_encoding/
progress.rs1use std::sync::atomic::{AtomicI64, Ordering};
2use tokio::sync::mpsc;
3use tracing::debug;
4
5pub struct ProgressSender<T> {
7 tx: Option<mpsc::Sender<T>>,
8 dropped: AtomicI64,
9}
10
11impl<T> ProgressSender<T> {
12 pub fn new(tx: Option<mpsc::Sender<T>>) -> Self {
14 Self { tx, dropped: AtomicI64::new(0) }
15 }
16
17 pub fn send(&self, value: T) {
19 let Some(ref tx) = self.tx else { return };
20 match tx.try_send(value) {
21 Ok(()) => {}
22 Err(mpsc::error::TrySendError::Full(_)) => {
23 let count = self.dropped.fetch_add(1, Ordering::Relaxed) + 1;
24 if count == 1 || count % 100 == 0 {
25 debug!("progress update dropped (channel full), total_dropped={count}");
26 }
27 }
28 Err(mpsc::error::TrySendError::Closed(_)) => {}
29 }
30 }
31
32 pub fn dropped(&self) -> i64 {
34 self.dropped.load(Ordering::Relaxed)
35 }
36}