1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use std::mem;
use tokio::sync::mpsc;
use tokio::sync::mpsc::{Receiver, Sender, UnboundedReceiver, UnboundedSender};
use tokio::sync::oneshot;
use crate::{init_stats_state, ChannelType, StatsEvent};
/// Wrap the inner channel with proxy ends. Returns (outer_tx, outer_rx).
/// All messages pass through the two forwarders.
pub(crate) fn wrap_channel<T: Send + 'static>(
inner: (Sender<T>, Receiver<T>),
channel_id: &'static str,
label: Option<&'static str>,
) -> (Sender<T>, Receiver<T>) {
let (inner_tx, mut inner_rx) = inner;
let type_name = std::any::type_name::<T>();
let capacity = inner_tx.capacity();
let (outer_tx, mut to_inner_rx) = mpsc::channel::<T>(capacity);
let (from_inner_tx, outer_rx) = mpsc::channel::<T>(capacity);
let (stats_tx, _) = init_stats_state();
let _ = stats_tx.send(StatsEvent::Created {
id: channel_id,
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();
// Create a signal channel to notify send-forwarder when outer_rx is closed
let (close_signal_tx, mut close_signal_rx) = oneshot::channel::<()>();
// Forward outer -> inner (proxy the send path)
tokio::spawn(async move {
loop {
tokio::select! {
msg = to_inner_rx.recv() => {
match msg {
Some(msg) => {
if inner_tx.send(msg).await.is_err() {
to_inner_rx.close();
break;
}
let _ = stats_tx_send.send(StatsEvent::MessageSent { id: channel_id });
}
None => break, // Outer sender dropped
}
}
_ = &mut close_signal_rx => {
// Outer receiver was closed/dropped, close our receiver to reject further sends
to_inner_rx.close();
break;
}
}
}
// Channel is closed
let _ = stats_tx_send.send(StatsEvent::Closed { id: channel_id });
});
// Forward inner -> outer (proxy the recv path)
tokio::spawn(async move {
loop {
tokio::select! {
msg = inner_rx.recv() => {
match msg {
Some(msg) => {
if from_inner_tx.send(msg).await.is_ok() {
let _ = stats_tx_recv.send(StatsEvent::MessageReceived { id: channel_id });
} else {
let _ = close_signal_tx.send(());
break;
}
}
None => break, // Inner sender dropped
}
}
_ = from_inner_tx.closed() => {
// Outer receiver was closed/dropped
let _ = close_signal_tx.send(());
break;
}
}
}
// Channel is closed (either inner sender dropped or outer receiver closed)
let _ = stats_tx_recv.send(StatsEvent::Closed { id: channel_id });
});
(outer_tx, outer_rx)
}
/// Wrap an unbounded channel with proxy ends. Returns (outer_tx, outer_rx).
pub(crate) fn wrap_unbounded<T: Send + 'static>(
inner: (UnboundedSender<T>, UnboundedReceiver<T>),
channel_id: &'static str,
label: Option<&'static str>,
) -> (UnboundedSender<T>, UnboundedReceiver<T>) {
let (inner_tx, mut inner_rx) = inner;
let type_name = std::any::type_name::<T>();
let (outer_tx, mut to_inner_rx) = mpsc::unbounded_channel::<T>();
let (from_inner_tx, outer_rx) = mpsc::unbounded_channel::<T>();
let (stats_tx, _) = init_stats_state();
let _ = stats_tx.send(StatsEvent::Created {
id: channel_id,
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();
// Create a signal channel to notify send-forwarder when outer_rx is closed
let (close_signal_tx, mut close_signal_rx) = oneshot::channel::<()>();
// Forward outer -> inner (proxy the send path)
tokio::spawn(async move {
loop {
tokio::select! {
msg = to_inner_rx.recv() => {
match msg {
Some(msg) => {
if inner_tx.send(msg).is_err() {
to_inner_rx.close();
break;
}
let _ = stats_tx_send.send(StatsEvent::MessageSent { id: channel_id });
}
None => break, // Outer sender dropped
}
}
_ = &mut close_signal_rx => {
// Outer receiver was closed/dropped, close our receiver to reject further sends
to_inner_rx.close();
break;
}
}
}
// Channel is closed
let _ = stats_tx_send.send(StatsEvent::Closed { id: channel_id });
});
// Forward inner -> outer (proxy the recv path)
tokio::spawn(async move {
loop {
tokio::select! {
msg = inner_rx.recv() => {
match msg {
Some(msg) => {
if from_inner_tx.send(msg).is_ok() {
let _ = stats_tx_recv.send(StatsEvent::MessageReceived { id: channel_id });
} else {
// Outer receiver was closed
let _ = close_signal_tx.send(());
break;
}
}
None => break, // Inner sender dropped
}
}
_ = from_inner_tx.closed() => {
// Outer receiver was closed/dropped
let _ = close_signal_tx.send(());
break;
}
}
}
// Channel is closed (either inner sender dropped or outer receiver closed)
let _ = stats_tx_recv.send(StatsEvent::Closed { id: channel_id });
});
(outer_tx, outer_rx)
}
/// Wrap a oneshot channel with proxy ends. Returns (outer_tx, outer_rx).
pub(crate) fn wrap_oneshot<T: Send + 'static>(
inner: (oneshot::Sender<T>, oneshot::Receiver<T>),
channel_id: &'static str,
label: Option<&'static str>,
) -> (oneshot::Sender<T>, oneshot::Receiver<T>) {
let (inner_tx, inner_rx) = inner;
let type_name = std::any::type_name::<T>();
let (outer_tx, outer_rx_proxy) = oneshot::channel::<T>();
let (mut inner_tx_proxy, outer_rx) = oneshot::channel::<T>();
let (stats_tx, _) = init_stats_state();
let _ = stats_tx.send(StatsEvent::Created {
id: channel_id,
display_label: label,
channel_type: ChannelType::Oneshot,
type_name,
type_size: mem::size_of::<T>(),
});
let stats_tx_send = stats_tx.clone();
let stats_tx_recv = stats_tx;
// Create a signal channel to notify send-forwarder when outer_rx is closed
let (close_signal_tx, mut close_signal_rx) = oneshot::channel::<()>();
// Monitor outer receiver and drop inner receiver when outer is dropped
tokio::spawn(async move {
let mut inner_rx = Some(inner_rx);
let mut message_received = false;
tokio::select! {
msg = async { inner_rx.take().unwrap().await }, if inner_rx.is_some() => {
// Message received from inner
match msg {
Ok(msg) => {
if inner_tx_proxy.send(msg).is_ok() {
let _ = stats_tx_recv.send(StatsEvent::MessageReceived { id: channel_id });
message_received = true;
}
}
Err(_) => {
// Inner sender was dropped without sending
}
}
}
_ = inner_tx_proxy.closed() => {
// Outer receiver was dropped - drop inner_rx to make sends fail
drop(inner_rx);
let _ = close_signal_tx.send(());
}
}
// Only send Closed if message was not successfully received
if !message_received {
let _ = stats_tx_recv.send(StatsEvent::Closed { id: channel_id });
}
});
// Forward outer -> inner (proxy the send path)
tokio::spawn(async move {
let mut message_sent = false;
tokio::select! {
msg = outer_rx_proxy => {
match msg {
Ok(msg) => {
if inner_tx.send(msg).is_ok() {
let _ = stats_tx_send.send(StatsEvent::MessageSent { id: channel_id });
let _ = stats_tx_send.send(StatsEvent::Notified { id: channel_id });
message_sent = true;
}
}
Err(_) => {
// Outer sender was dropped without sending
}
}
}
_ = &mut close_signal_rx => {
// Outer receiver was closed/dropped before send
}
}
// Only send Closed if message was not successfully sent
if !message_sent {
let _ = stats_tx_send.send(StatsEvent::Closed { id: channel_id });
}
});
(outer_tx, outer_rx)
}