use std::sync::mpsc::{self, Receiver, Sender, SyncSender};
use crate::channels::{
register_channel, send_channel_event, ChannelEvent, ChannelType, Instant, RegisteredChannel,
};
fn wrap_sync_channel_impl<T, F>(
inner: (SyncSender<T>, Receiver<T>),
source: &'static str,
label: Option<String>,
capacity: usize,
mut log_on_send: F,
) -> (SyncSender<T>, Receiver<T>)
where
T: Send + 'static,
F: FnMut(&T) -> Option<String> + Send + 'static,
{
let (inner_tx, inner_rx) = inner;
let (proxy_tx, proxy_rx) = mpsc::sync_channel::<T>(1);
let RegisteredChannel { id, stats_tx } =
register_channel::<T>(source, label, ChannelType::Bounded(capacity));
std::thread::spawn(move || {
while let Ok(msg) = inner_rx.recv() {
let log = log_on_send(&msg);
send_channel_event(
&stats_tx,
ChannelEvent::MessageSent {
id,
log,
timestamp: Instant::now(),
},
);
if proxy_tx.send(msg).is_ok() {
send_channel_event(
&stats_tx,
ChannelEvent::MessageReceived {
id,
timestamp: Instant::now(),
},
);
} else {
break;
}
}
send_channel_event(&stats_tx, ChannelEvent::Closed { id });
});
(inner_tx, proxy_rx)
}
pub(crate) fn wrap_sync_channel<T: Send + 'static>(
inner: (SyncSender<T>, Receiver<T>),
source: &'static str,
label: Option<String>,
capacity: usize,
) -> (SyncSender<T>, Receiver<T>) {
wrap_sync_channel_impl(inner, source, label, capacity, |_| None)
}
pub(crate) fn wrap_sync_channel_log<T: Send + std::fmt::Debug + 'static>(
inner: (SyncSender<T>, Receiver<T>),
source: &'static str,
label: Option<String>,
capacity: usize,
) -> (SyncSender<T>, Receiver<T>) {
wrap_sync_channel_impl(inner, source, label, capacity, |msg| {
Some(crate::output::format_debug_truncated(msg))
})
}
fn wrap_channel_impl<T, F>(
inner: (Sender<T>, Receiver<T>),
source: &'static str,
label: Option<String>,
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 (proxy_tx, proxy_rx) = mpsc::channel::<T>();
let RegisteredChannel { id, stats_tx } =
register_channel::<T>(source, label, ChannelType::Unbounded);
std::thread::spawn(move || {
while let Ok(msg) = inner_rx.recv() {
let log = log_on_send(&msg);
send_channel_event(
&stats_tx,
ChannelEvent::MessageSent {
id,
log,
timestamp: Instant::now(),
},
);
if proxy_tx.send(msg).is_ok() {
send_channel_event(
&stats_tx,
ChannelEvent::MessageReceived {
id,
timestamp: Instant::now(),
},
);
} else {
break;
}
}
send_channel_event(&stats_tx, ChannelEvent::Closed { id });
});
(inner_tx, proxy_rx)
}
pub(crate) fn wrap_channel<T: Send + 'static>(
inner: (Sender<T>, Receiver<T>),
source: &'static str,
label: Option<String>,
) -> (Sender<T>, Receiver<T>) {
wrap_channel_impl(inner, source, label, |_| None)
}
pub(crate) fn wrap_channel_log<T: Send + std::fmt::Debug + 'static>(
inner: (Sender<T>, Receiver<T>),
source: &'static str,
label: Option<String>,
) -> (Sender<T>, Receiver<T>) {
wrap_channel_impl(inner, source, label, |msg| {
Some(crate::output::format_debug_truncated(msg))
})
}
use crate::channels::InstrumentChannel;
impl<T: Send + 'static> InstrumentChannel
for (std::sync::mpsc::Sender<T>, std::sync::mpsc::Receiver<T>)
{
type Output = (std::sync::mpsc::Sender<T>, std::sync::mpsc::Receiver<T>);
fn instrument(
self,
source: &'static str,
label: Option<String>,
_capacity: Option<usize>,
) -> Self::Output {
wrap_channel(self, source, label)
}
}
impl<T: Send + 'static> InstrumentChannel
for (std::sync::mpsc::SyncSender<T>, std::sync::mpsc::Receiver<T>)
{
type Output = (std::sync::mpsc::SyncSender<T>, std::sync::mpsc::Receiver<T>);
fn instrument(
self,
source: &'static str,
label: Option<String>,
capacity: Option<usize>,
) -> Self::Output {
if capacity.is_none() {
panic!("Capacity is required for bounded std channels, because they don't expose their capacity in a public API");
}
wrap_sync_channel(self, source, label, capacity.unwrap())
}
}
use crate::channels::InstrumentChannelLog;
impl<T: Send + std::fmt::Debug + 'static> InstrumentChannelLog
for (std::sync::mpsc::Sender<T>, std::sync::mpsc::Receiver<T>)
{
type Output = (std::sync::mpsc::Sender<T>, std::sync::mpsc::Receiver<T>);
fn instrument_log(
self,
source: &'static str,
label: Option<String>,
_capacity: Option<usize>,
) -> Self::Output {
wrap_channel_log(self, source, label)
}
}
impl<T: Send + std::fmt::Debug + 'static> InstrumentChannelLog
for (std::sync::mpsc::SyncSender<T>, std::sync::mpsc::Receiver<T>)
{
type Output = (std::sync::mpsc::SyncSender<T>, std::sync::mpsc::Receiver<T>);
fn instrument_log(
self,
source: &'static str,
label: Option<String>,
capacity: Option<usize>,
) -> Self::Output {
if capacity.is_none() {
panic!("Capacity is required for bounded std channels, because they don't expose their capacity in a public API");
}
wrap_sync_channel_log(self, source, label, capacity.unwrap())
}
}