use crossbeam_channel::{self, Receiver, Sender};
use std::mem;
use std::sync::atomic::Ordering;
use crate::{init_stats_state, ChannelType, StatsEvent, CHANNEL_ID_COUNTER};
fn wrap_bounded_impl<T, F>(
inner: (Sender<T>, Receiver<T>),
source: &'static str,
label: Option<&'static str>,
capacity: usize,
mut log_on_send: F,
) -> (Sender<T>, Receiver<T>)
where
T: Send + 'static,
F: FnMut(&T) -> Option<String> + Send + 'static,
{
let (inner_tx, inner_rx) = inner;
let type_name = std::any::type_name::<T>();
let (outer_tx, to_inner_rx) = crossbeam_channel::bounded::<T>(capacity);
let (from_inner_tx, outer_rx) = crossbeam_channel::bounded::<T>(capacity);
let (stats_tx, _) = init_stats_state();
let id = CHANNEL_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
let _ = stats_tx.send(StatsEvent::Created {
id,
source,
display_label: label,
channel_type: ChannelType::Bounded(capacity),
type_name,
type_size: mem::size_of::<T>(),
});
let stats_tx_send = stats_tx.clone();
let stats_tx_recv = stats_tx.clone();
let (close_signal_tx, close_signal_rx) = crossbeam_channel::bounded::<()>(1);
std::thread::spawn(move || {
loop {
match close_signal_rx.try_recv() {
Ok(_) => {
break;
}
Err(crossbeam_channel::TryRecvError::Disconnected) => {
break;
}
Err(crossbeam_channel::TryRecvError::Empty) => {
}
}
match to_inner_rx.recv_timeout(std::time::Duration::from_millis(10)) {
Ok(msg) => {
let log = log_on_send(&msg);
if inner_tx.send(msg).is_err() {
break;
}
let _ = stats_tx_send.send(StatsEvent::MessageSent {
id,
log,
timestamp: std::time::Instant::now(),
});
}
Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
continue;
}
Err(crossbeam_channel::RecvTimeoutError::Disconnected) => {
break;
}
}
}
let _ = stats_tx_send.send(StatsEvent::Closed { id });
});
std::thread::spawn(move || {
while let Ok(msg) = inner_rx.recv() {
if from_inner_tx.send(msg).is_err() {
let _ = close_signal_tx.send(());
break;
}
let _ = stats_tx_recv.send(StatsEvent::MessageReceived {
id,
timestamp: std::time::Instant::now(),
});
}
let _ = stats_tx_recv.send(StatsEvent::Closed { id });
});
(outer_tx, outer_rx)
}
pub(crate) fn wrap_bounded<T: Send + 'static>(
inner: (Sender<T>, Receiver<T>),
source: &'static str,
label: Option<&'static str>,
capacity: usize,
) -> (Sender<T>, Receiver<T>) {
wrap_bounded_impl(inner, source, label, capacity, |_| None)
}
pub(crate) fn wrap_bounded_log<T: Send + std::fmt::Debug + 'static>(
inner: (Sender<T>, Receiver<T>),
source: &'static str,
label: Option<&'static str>,
capacity: usize,
) -> (Sender<T>, Receiver<T>) {
wrap_bounded_impl(inner, source, label, capacity, |msg| {
Some(format!("{:?}", msg))
})
}
fn wrap_unbounded_impl<T, F>(
inner: (Sender<T>, Receiver<T>),
source: &'static str,
label: Option<&'static str>,
mut log_on_send: F,
) -> (Sender<T>, Receiver<T>)
where
T: Send + 'static,
F: FnMut(&T) -> Option<String> + Send + 'static,
{
let (inner_tx, inner_rx) = inner;
let type_name = std::any::type_name::<T>();
let (outer_tx, to_inner_rx) = crossbeam_channel::unbounded::<T>();
let (from_inner_tx, outer_rx) = crossbeam_channel::unbounded::<T>();
let (stats_tx, _) = init_stats_state();
let id = CHANNEL_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
let _ = stats_tx.send(StatsEvent::Created {
id,
source,
display_label: label,
channel_type: ChannelType::Unbounded,
type_name,
type_size: mem::size_of::<T>(),
});
let stats_tx_send = stats_tx.clone();
let stats_tx_recv = stats_tx.clone();
let (close_signal_tx, close_signal_rx) = crossbeam_channel::bounded::<()>(1);
std::thread::spawn(move || {
loop {
match close_signal_rx.try_recv() {
Ok(_) => {
break;
}
Err(crossbeam_channel::TryRecvError::Disconnected) => {
break;
}
Err(crossbeam_channel::TryRecvError::Empty) => {
}
}
match to_inner_rx.recv_timeout(std::time::Duration::from_millis(10)) {
Ok(msg) => {
let log = log_on_send(&msg);
if inner_tx.send(msg).is_err() {
break;
}
let _ = stats_tx_send.send(StatsEvent::MessageSent {
id,
log,
timestamp: std::time::Instant::now(),
});
}
Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
continue;
}
Err(crossbeam_channel::RecvTimeoutError::Disconnected) => {
break;
}
}
}
let _ = stats_tx_send.send(StatsEvent::Closed { id });
});
std::thread::spawn(move || {
while let Ok(msg) = inner_rx.recv() {
if from_inner_tx.send(msg).is_err() {
let _ = close_signal_tx.send(());
break;
}
let _ = stats_tx_recv.send(StatsEvent::MessageReceived {
id,
timestamp: std::time::Instant::now(),
});
}
let _ = stats_tx_recv.send(StatsEvent::Closed { id });
});
(outer_tx, outer_rx)
}
pub(crate) fn wrap_unbounded<T: Send + 'static>(
inner: (Sender<T>, Receiver<T>),
source: &'static str,
label: Option<&'static str>,
) -> (Sender<T>, Receiver<T>) {
wrap_unbounded_impl(inner, source, label, |_| None)
}
pub(crate) fn wrap_unbounded_log<T: Send + std::fmt::Debug + 'static>(
inner: (Sender<T>, Receiver<T>),
source: &'static str,
label: Option<&'static str>,
) -> (Sender<T>, Receiver<T>) {
wrap_unbounded_impl(inner, source, label, |msg| Some(format!("{:?}", msg)))
}
use crate::Instrument;
impl<T: Send + 'static> Instrument
for (crossbeam_channel::Sender<T>, crossbeam_channel::Receiver<T>)
{
type Output = (crossbeam_channel::Sender<T>, crossbeam_channel::Receiver<T>);
fn instrument(
self,
source: &'static str,
label: Option<&'static str>,
_capacity: Option<usize>,
) -> Self::Output {
match self.0.capacity() {
Some(capacity) => wrap_bounded(self, source, label, capacity),
None => wrap_unbounded(self, source, label),
}
}
}
use crate::InstrumentLog;
impl<T: Send + std::fmt::Debug + 'static> InstrumentLog
for (crossbeam_channel::Sender<T>, crossbeam_channel::Receiver<T>)
{
type Output = (crossbeam_channel::Sender<T>, crossbeam_channel::Receiver<T>);
fn instrument_log(
self,
source: &'static str,
label: Option<&'static str>,
_capacity: Option<usize>,
) -> Self::Output {
match self.0.capacity() {
Some(capacity) => wrap_bounded_log(self, source, label, capacity),
None => wrap_unbounded_log(self, source, label),
}
}
}