use crate::constants::DEFAULT_CLIENT_DATA_CHANNEL_CAPACITY;
use crate::protocol::Data;
use std::fmt::{self, Debug, Formatter};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use tokio::sync::{mpsc, Notify};
use tokio::time::Instant;
use tracing::{trace, warn};
pub struct DataChannel {
pub id: u32,
pub closed: AtomicBool,
pub data_tx: mpsc::Sender<Data>,
pub capacity: AtomicU32,
pub notify: Notify,
}
impl DataChannel {
pub fn new_client(id: u32, data_tx: mpsc::Sender<Data>) -> DataChannel {
Self {
id,
closed: AtomicBool::new(false),
data_tx,
capacity: AtomicU32::new(DEFAULT_CLIENT_DATA_CHANNEL_CAPACITY),
notify: Notify::new(),
}
}
pub fn add_capacity(&self, amount: u32) {
trace!("{:?} acked, consumed {} bytes", self, amount);
let _ = self
.capacity
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |current| {
Some(
current
.saturating_add(amount)
.min(DEFAULT_CLIENT_DATA_CHANNEL_CAPACITY),
)
});
self.notify.notify_waiters();
}
pub fn reset_capacity(&self, consumed: u32) {
self.capacity.store(
DEFAULT_CLIENT_DATA_CHANNEL_CAPACITY.saturating_sub(consumed),
Ordering::SeqCst,
);
}
pub async fn wait_for_capacity_or_reset(
&self,
required: u32,
reset_at: Option<Instant>,
) -> Result<(), mpsc::error::SendError<()>> {
let Some(reset_at) = reset_at else {
return self.wait_for_capacity(required).await;
};
match tokio::time::timeout_at(reset_at, self.wait_for_capacity(required)).await {
Ok(result) => result,
Err(_) => {
warn!("{:?}: no capacity back in time, resetting it", self);
self.reset_capacity(required);
Ok(())
}
}
}
pub async fn wait_for_capacity(&self, required: u32) -> Result<(), mpsc::error::SendError<()>> {
loop {
if self.closed.load(Ordering::SeqCst) {
trace!("{:?} channel is closed, cannot wait for capacity", self);
return Err(mpsc::error::SendError(()));
}
let current = self.capacity.load(Ordering::SeqCst);
trace!(
"{:?} checking capacity: {} bytes available, required: {} bytes",
self,
current,
required
);
if current >= required {
match self.capacity.compare_exchange_weak(
current,
current - required,
Ordering::SeqCst,
Ordering::SeqCst,
) {
Ok(_) => {
trace!(
"{:?} has sufficient capacity: {} bytes available, consuming {} bytes",
self,
current,
required
);
return Ok(());
}
Err(_) => continue, }
}
trace!(
"{:?} insufficient capacity: {} bytes available, waiting for {} more",
self,
current,
required
);
tokio::select! {
_ = self.notify.notified() => {
trace!("{:?} notified, checking capacity again", self);
}
_ = self.data_tx.closed() => {
return Err(mpsc::error::SendError(()));
}
}
}
}
pub fn close(&self) {
trace!("{:?} closing channel", self);
self.closed.store(true, Ordering::SeqCst);
self.notify.notify_waiters();
}
pub async fn closed(&self) {
trace!("{:?} waiting for channel to close", self);
while !self.closed.load(Ordering::SeqCst) {
self.notify.notified().await;
}
}
}
impl Debug for DataChannel {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("DataChannel")
.field("id", &self.id)
.field("capacity", &self.capacity)
.finish()
}
}