Skip to main content

cloudpub_common/
data.rs

1use crate::constants::DEFAULT_CLIENT_DATA_CHANNEL_CAPACITY;
2use crate::protocol::Data;
3use std::fmt::{self, Debug, Formatter};
4use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
5use tokio::sync::{mpsc, Notify};
6use tokio::time::Instant;
7use tracing::{trace, warn};
8
9pub struct DataChannel {
10    pub id: u32,
11    pub closed: AtomicBool,
12    pub data_tx: mpsc::Sender<Data>,
13    pub capacity: AtomicU32,
14    pub notify: Notify,
15}
16
17impl DataChannel {
18    pub fn new_client(id: u32, data_tx: mpsc::Sender<Data>) -> DataChannel {
19        Self {
20            id,
21            closed: AtomicBool::new(false),
22            data_tx,
23            capacity: AtomicU32::new(DEFAULT_CLIENT_DATA_CHANNEL_CAPACITY),
24            notify: Notify::new(),
25        }
26    }
27
28    pub fn add_capacity(&self, amount: u32) {
29        trace!("{:?} acked, consumed {} bytes", self, amount);
30        // Late acks after reset_capacity never raise capacity past full - CLOUDPUB-1601.
31        let _ = self
32            .capacity
33            .fetch_update(Ordering::SeqCst, Ordering::SeqCst, |current| {
34                Some(
35                    current
36                        .saturating_add(amount)
37                        .min(DEFAULT_CLIENT_DATA_CHANNEL_CAPACITY),
38                )
39            });
40        self.notify.notify_waiters();
41    }
42
43    /// Resets capacity to full minus `consumed`, giving up on acks in flight - CLOUDPUB-1601.
44    pub fn reset_capacity(&self, consumed: u32) {
45        self.capacity.store(
46            DEFAULT_CLIENT_DATA_CHANNEL_CAPACITY.saturating_sub(consumed),
47            Ordering::SeqCst,
48        );
49    }
50
51    /// wait_for_capacity, but with `reset_at` the wait ends then with a capacity reset:
52    /// an agent before CLOUDPUB-1601 may never ack datagrams it lost.
53    pub async fn wait_for_capacity_or_reset(
54        &self,
55        required: u32,
56        reset_at: Option<Instant>,
57    ) -> Result<(), mpsc::error::SendError<()>> {
58        let Some(reset_at) = reset_at else {
59            return self.wait_for_capacity(required).await;
60        };
61        match tokio::time::timeout_at(reset_at, self.wait_for_capacity(required)).await {
62            Ok(result) => result,
63            Err(_) => {
64                warn!("{:?}: no capacity back in time, resetting it", self);
65                self.reset_capacity(required);
66                Ok(())
67            }
68        }
69    }
70
71    pub async fn wait_for_capacity(&self, required: u32) -> Result<(), mpsc::error::SendError<()>> {
72        loop {
73            if self.closed.load(Ordering::SeqCst) {
74                trace!("{:?} channel is closed, cannot wait for capacity", self);
75                return Err(mpsc::error::SendError(()));
76            }
77            let current = self.capacity.load(Ordering::SeqCst);
78            trace!(
79                "{:?} checking capacity: {} bytes available, required: {} bytes",
80                self,
81                current,
82                required
83            );
84            if current >= required {
85                // Atomically subtract only if capacity hasn't changed
86                match self.capacity.compare_exchange_weak(
87                    current,
88                    current - required,
89                    Ordering::SeqCst,
90                    Ordering::SeqCst,
91                ) {
92                    Ok(_) => {
93                        trace!(
94                            "{:?} has sufficient capacity: {} bytes available, consuming {} bytes",
95                            self,
96                            current,
97                            required
98                        );
99                        return Ok(());
100                    }
101                    Err(_) => continue, // Retry if another thread modified capacity
102                }
103            }
104
105            trace!(
106                "{:?} insufficient capacity: {} bytes available, waiting for {} more",
107                self,
108                current,
109                required
110            );
111
112            tokio::select! {
113                _ = self.notify.notified() => {
114                    trace!("{:?} notified, checking capacity again", self);
115                }
116                _ = self.data_tx.closed() => {
117                    return Err(mpsc::error::SendError(()));
118                }
119            }
120        }
121    }
122
123    pub fn close(&self) {
124        trace!("{:?} closing channel", self);
125        self.closed.store(true, Ordering::SeqCst);
126        self.notify.notify_waiters();
127    }
128
129    pub async fn closed(&self) {
130        trace!("{:?} waiting for channel to close", self);
131        while !self.closed.load(Ordering::SeqCst) {
132            self.notify.notified().await;
133        }
134    }
135}
136
137impl Debug for DataChannel {
138    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
139        f.debug_struct("DataChannel")
140            .field("id", &self.id)
141            .field("capacity", &self.capacity)
142            .finish()
143    }
144}