mesh_tracing/
bounded.rs

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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Bounded channel for sending requests to the tracing backend.
//!
//! In the future, this should be generalized and move to `mesh_channel`.

use futures::Stream;
use mesh::Message;
use mesh::OwnedMessage;
use mesh::RecvError;
use mesh::local_node::HandleMessageError;
use mesh::local_node::HandlePortEvent;
use mesh::local_node::NodeError;
use mesh::local_node::Port;
use mesh::local_node::PortControl;
use mesh::local_node::PortField;
use mesh::local_node::PortWithHandler;
use mesh::message::MeshField;
use mesh::payload::Protobuf;
use mesh::resource::Resource;
use std::collections::VecDeque;
use std::future::poll_fn;
use std::marker::PhantomData;
use std::task::Context;
use std::task::Poll;
use std::task::Waker;
use std::task::ready;

pub struct BoundedReceiver<T> {
    port: PortWithHandler<ReceiverState>,
    quota: u32,
    _phantom: PhantomData<fn() -> T>,
}

/// Creates a new unidirectional bounded pipe, returning a reader and writer.
///
/// The resulting pipe has backpressure, so that if the writer tries to write
/// too many bytes before the reader reads them, then calls to
/// [`AsyncWriteExt::write`](futures::AsyncWriteExt::write) will block, and
/// calls to [`AsyncWrite::poll_write`] will return [`Poll::Pending`].
pub fn bounded<T: MeshField>(quota: u32) -> (BoundedSender<T>, BoundedReceiver<T>) {
    let (read, write) = Port::new_pair();
    let receiver = BoundedReceiver {
        port: read.set_handler(ReceiverState {
            data: VecDeque::new(),
            consumed_messages: 0,
            closed: false,
            failed: None,
            waker: None,
        }),
        quota,
        _phantom: PhantomData,
    };
    let sender = BoundedSender {
        port: write.set_handler(SenderState {
            remaining_quota: quota,
            closed: false,
            waker: None,
        }),
        _phantom: PhantomData,
    };
    (sender, receiver)
}

struct ReceiverState {
    data: VecDeque<OwnedMessage>,
    consumed_messages: u32,
    closed: bool,
    failed: Option<NodeError>,
    waker: Option<Waker>,
}

impl<T: 'static + MeshField + Send> BoundedReceiver<T> {
    fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Result<T, RecvError>> {
        let mut old_waker = None;
        self.port.with_port_and_handler(|port, state| {
            let Some(message) = state.data.pop_front() else {
                if let Some(err) = state.failed.take() {
                    state.closed = true;
                    return Err(RecvError::Error(err.into())).into();
                } else if state.closed {
                    return Err(RecvError::Closed).into();
                }
                old_waker = state.waker.replace(cx.waker().clone());
                return Poll::Pending;
            };
            state.consumed_messages += 1;
            if state.consumed_messages >= self.quota / 2 {
                port.respond(Message::new(QuotaMessage {
                    messages: state.consumed_messages,
                }));
                state.consumed_messages = 0;
            }
            match message.parse() {
                Ok((message,)) => Ok(message).into(),
                Err(err) => Err(RecvError::Error(err.into())).into(),
            }
        })
    }
}

impl<T: 'static + MeshField + Send> Stream for BoundedReceiver<T> {
    type Item = T;

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        match ready!(self.poll_recv(cx)) {
            Ok(message) => Some(message).into(),
            Err(RecvError::Closed) => None.into(),
            Err(RecvError::Error(err)) => {
                tracing::error!(
                    error = &err as &dyn std::error::Error,
                    "bounded channel error"
                );
                None.into()
            }
        }
    }
}

impl HandlePortEvent for ReceiverState {
    fn message(
        &mut self,
        control: &mut PortControl<'_, '_>,
        message: Message<'_>,
    ) -> Result<(), HandleMessageError> {
        if let Some(err) = &self.failed {
            return Err(HandleMessageError::new(err.clone()));
        }
        self.data.push_back(message.into_owned());
        if let Some(waker) = self.waker.take() {
            control.wake(waker);
        }
        Ok(())
    }

    fn close(&mut self, control: &mut PortControl<'_, '_>) {
        self.closed = true;
        if let Some(waker) = self.waker.take() {
            control.wake(waker);
        }
    }

    fn fail(&mut self, control: &mut PortControl<'_, '_>, err: NodeError) {
        self.failed = Some(err);
        if let Some(waker) = self.waker.take() {
            control.wake(waker);
        }
    }

    fn drain(&mut self) -> Vec<OwnedMessage> {
        std::mem::take(&mut self.data).into()
    }
}

#[derive(Protobuf, Debug)]
#[mesh(resource = "Resource")]
pub struct BoundedSender<T> {
    #[mesh(encoding = "PortField")]
    port: PortWithHandler<SenderState>,
    _phantom: PhantomData<fn(T)>,
}

#[derive(Default, Debug)]
struct SenderState {
    remaining_quota: u32,
    closed: bool,
    waker: Option<Waker>,
}

#[derive(Debug)]
pub enum TrySendError {
    Full,
    Closed,
}

impl<T: 'static + MeshField + Send> BoundedSender<T> {
    fn poll_send(&mut self, cx: &mut Context<'_>, message: &mut Option<T>) -> Poll<()> {
        let mut old_waker = None;
        self.port.with_port_and_handler(|port, state| {
            if state.closed {
                ().into()
            } else if state.remaining_quota > 0 {
                state.remaining_quota -= 1;
                port.respond(Message::new((message.take().unwrap(),)));
                ().into()
            } else {
                old_waker = state.waker.replace(cx.waker().clone());
                Poll::Pending
            }
        })
    }

    pub async fn send(&mut self, message: T) {
        let mut message = Some(message);
        poll_fn(|cx| self.poll_send(cx, &mut message)).await
    }

    pub fn try_send(&self, message: T) -> Result<(), TrySendError> {
        self.port.with_port_and_handler(|port, state| {
            if state.closed {
                Err(TrySendError::Closed)
            } else if state.remaining_quota == 0 {
                Err(TrySendError::Full)
            } else {
                state.remaining_quota -= 1;
                port.respond(Message::new((message,)));
                Ok(())
            }
        })
    }
}

impl HandlePortEvent for SenderState {
    fn message(
        &mut self,
        control: &mut PortControl<'_, '_>,
        message: Message<'_>,
    ) -> Result<(), HandleMessageError> {
        let message = message.parse::<QuotaMessage>().map_err(|err| {
            self.closed = true;
            HandleMessageError::new(err)
        })?;
        self.remaining_quota += message.messages;
        if let Some(waker) = self.waker.take() {
            control.wake(waker);
        }
        Ok(())
    }

    fn close(&mut self, control: &mut PortControl<'_, '_>) {
        self.closed = true;
        if let Some(waker) = self.waker.take() {
            control.wake(waker);
        }
    }

    fn fail(&mut self, control: &mut PortControl<'_, '_>, _err: NodeError) {
        self.closed = true;
        if let Some(waker) = self.waker.take() {
            control.wake(waker);
        }
    }

    fn drain(&mut self) -> Vec<OwnedMessage> {
        // Send remaining quota as a message to avoid having to synchronize
        // during encoding.
        vec![OwnedMessage::new(QuotaMessage {
            messages: self.remaining_quota,
        })]
    }
}

#[derive(Protobuf)]
struct QuotaMessage {
    messages: u32,
}

mod encoding {
    use super::BoundedReceiver;
    use super::ReceiverState;
    use mesh::local_node::Port;
    use mesh::message::MeshField;
    use mesh::payload::DefaultEncoding;
    use mesh::payload::MessageDecode;
    use mesh::payload::MessageEncode;
    use mesh::payload::Protobuf;
    use mesh::payload::encoding::MessageEncoding;
    use mesh::payload::inplace_none;
    use mesh::resource::Resource;
    use std::collections::VecDeque;
    use std::marker::PhantomData;

    pub struct BoundedReceiverEncoder;

    impl<T: MeshField> DefaultEncoding for BoundedReceiver<T> {
        type Encoding = MessageEncoding<BoundedReceiverEncoder>;
    }

    #[derive(Protobuf)]
    #[mesh(resource = "Resource")]
    struct SerializedBoundedReceiver {
        port: Port,
        quota: u32,
    }

    impl<T: MeshField> From<SerializedBoundedReceiver> for BoundedReceiver<T> {
        fn from(value: SerializedBoundedReceiver) -> Self {
            let SerializedBoundedReceiver { port, quota } = value;
            Self {
                port: port.set_handler(ReceiverState {
                    data: VecDeque::new(),
                    consumed_messages: 0,
                    closed: false,
                    failed: None,
                    waker: None,
                }),
                quota,
                _phantom: PhantomData,
            }
        }
    }

    impl<T: MeshField> From<BoundedReceiver<T>> for SerializedBoundedReceiver {
        fn from(value: BoundedReceiver<T>) -> Self {
            Self {
                port: value.port.remove_handler().0,
                quota: value.quota,
            }
        }
    }

    impl<T: MeshField> MessageEncode<BoundedReceiver<T>, Resource> for BoundedReceiverEncoder {
        fn write_message(
            item: BoundedReceiver<T>,
            writer: mesh::payload::protobuf::MessageWriter<'_, '_, Resource>,
        ) {
            <SerializedBoundedReceiver as DefaultEncoding>::Encoding::write_message(
                SerializedBoundedReceiver::from(item),
                writer,
            )
        }

        fn compute_message_size(
            item: &mut BoundedReceiver<T>,
            mut sizer: mesh::payload::protobuf::MessageSizer<'_>,
        ) {
            sizer.field(1).resource();
            sizer.field(2).varint(item.quota.into());
        }
    }

    impl<T: MeshField> MessageDecode<'_, BoundedReceiver<T>, Resource> for BoundedReceiverEncoder {
        fn read_message(
            item: &mut mesh::payload::inplace::InplaceOption<'_, BoundedReceiver<T>>,
            reader: mesh::payload::protobuf::MessageReader<'_, '_, Resource>,
        ) -> mesh::payload::Result<()> {
            inplace_none!(inner: SerializedBoundedReceiver);
            <SerializedBoundedReceiver as DefaultEncoding>::Encoding::read_message(
                &mut inner, reader,
            )?;
            item.set(inner.take().unwrap().into());
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::bounded;
    use futures::StreamExt;
    use pal_async::async_test;

    #[async_test]
    async fn test_bounded_send_recv() {
        let (send, mut recv) = bounded::<()>(16);
        // Ensure we can send 16 times without blocking (via try_send).
        for _ in 0..16 {
            send.try_send(()).unwrap();
        }

        // Ensure we can no longer send.
        assert!(send.try_send(()).is_err());

        // Receive some messages, and then make sure we can send again, up to the expected quota.
        for _ in 0..8 {
            recv.next().await.unwrap();
        }
        for _ in 0..8 {
            send.try_send(()).unwrap();
        }
        assert!(send.try_send(()).is_err());
    }
}