remoc 0.19.1

🦑 Remote multiplexed objects, channels, observable collections and RPC making remote interactions seamless. Provides multiple remote channels and RPC over TCP, TLS or any other transport.
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Multi producer single customer remote channel.
//!
//! The sender and receiver can both be sent to remote endpoints.
//! The channel also works if both halves are local.
//! Forwarding over multiple connections is supported.
//!
//! This has similar functionality as [tokio::sync::mpsc] with the additional
//! ability to work over remote connections.
//!
//! # Example
//!
//! In the following example the client sends a number and an MPSC channel sender to the server.
//! The server counts to the number and sends each value to the client over the MPSC channel.
//!
//! ```
//! use remoc::prelude::*;
//!
//! #[derive(Debug, serde::Serialize, serde::Deserialize)]
//! struct CountReq {
//!     up_to: u32,
//!     seq_tx: rch::mpsc::Sender<u32>,
//! }
//!
//! // This would be run on the client.
//! async fn client(mut tx: rch::base::Sender<CountReq>) {
//!     let (seq_tx, mut seq_rx) = rch::mpsc::channel(1);
//!     tx.send(CountReq { up_to: 4, seq_tx }).await.unwrap();
//!
//!     assert_eq!(seq_rx.recv().await.unwrap(), Some(0));
//!     assert_eq!(seq_rx.recv().await.unwrap(), Some(1));
//!     assert_eq!(seq_rx.recv().await.unwrap(), Some(2));
//!     assert_eq!(seq_rx.recv().await.unwrap(), Some(3));
//!     assert_eq!(seq_rx.recv().await.unwrap(), None);
//! }
//!
//! // This would be run on the server.
//! async fn server(mut rx: rch::base::Receiver<CountReq>) {
//!     while let Some(CountReq { up_to, seq_tx }) = rx.recv().await.unwrap() {
//!         for i in 0..up_to {
//!             seq_tx.send(i).await.unwrap();
//!         }
//!     }
//! }
//! # tokio_test::block_on(remoc::doctest::client_server(client, server));
//! ```
//!

use bytes::Buf;
use futures::{FutureExt, future::BoxFuture};
use std::{
    fmt,
    future::Future,
    mem,
    pin::Pin,
    task::{Context, Poll, ready},
};

use super::{ClosedReason, RemoteSendError, Sending, base};
use crate::{
    RemoteSend, chmux,
    codec::{self, AnySend, ErasedDeserializer, ErasedSerializer},
    exec,
    rch::{BACKCHANNEL_MSG_CLOSE, BACKCHANNEL_MSG_ERROR},
};

mod distributor;
mod receiver;
mod sender;

pub use distributor::{DistributedReceiverHandle, Distributor};
pub use receiver::{Receiver, RecvError, TryRecvError};
pub use sender::{Permit, SendError, Sender, SenderSink, TrySendError};

/// Creates a bounded channel for communicating between asynchronous tasks with back pressure.
///
/// The sender and receiver may be sent to remote endpoints via channels.
pub fn channel<T, Codec>(local_buffer: usize) -> (Sender<T, Codec>, Receiver<T, Codec>)
where
    T: RemoteSend,
{
    assert!(local_buffer > 0, "local_buffer must not be zero");

    let (tx, rx) = tokio::sync::mpsc::channel(local_buffer);
    let (closed_tx, closed_rx) = tokio::sync::watch::channel(None);
    let (remote_send_err_tx, remote_send_err_rx) = tokio::sync::watch::channel(None);

    let sender = Sender::new(tx, closed_rx, remote_send_err_rx);
    let receiver = Receiver::new(rx, closed_tx, false, remote_send_err_tx, None);
    (sender, receiver)
}

/// Makes a local mpsc receiver forwardable to remote endpoints.
///
/// The returned [`Forwarding`] future resolves once forwarding has completed or an error occurs.
/// The returned receiver may be sent to remote endpoints via channels.
pub fn forward<T, Codec>(mut local_rx: tokio::sync::mpsc::Receiver<T>) -> (Forwarding, Receiver<T, Codec>)
where
    T: RemoteSend,
    Codec: codec::Codec,
{
    let (tx, rx) = channel(1);

    let hnd = exec::spawn(async move {
        loop {
            let permit = match tx.reserve().await {
                Ok(permit) => permit,
                Err(err) if err.is_closed() => break,
                Err(err) => return Err(err),
            };
            match local_rx.recv().await {
                Some(v) => {
                    permit.send(v);
                }
                None => break,
            }
        }

        Ok(())
    });

    (Forwarding(hnd), rx)
}

/// Handle to obtain the result of forwarding a local receiver remotely by [`forward`].
///
/// Await this to obtain the result of the forwarding operation.
/// The operation is assumed to have finished successfully if either the local or remote
/// channel is closed or dropped.
///
/// Dropping this *does not* stop forwarding.
pub struct Forwarding(exec::task::JoinHandle<Result<(), SendError<()>>>);

impl fmt::Debug for Forwarding {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Forwarding").finish()
    }
}

impl Future for Forwarding {
    type Output = Result<(), SendError<()>>;
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        match ready!(self.0.poll_unpin(cx)) {
            Ok(res) => Poll::Ready(res),
            Err(_) => Poll::Ready(Err(SendError::Closed(()))),
        }
    }
}

impl Forwarding {
    /// Stops forwarding.
    ///
    /// The remote sending half and local receiving half of the mpsc channels are dropped.
    pub fn stop(self) {
        self.0.abort();
    }
}

/// Extensions for MPSC channels.
pub trait MpscExt<T, Codec, const BUFFER: usize, const MAX_ITEM_SIZE: usize> {
    /// Sets the buffer size that will be used when sending the channel's sender and receiver
    /// to a remote endpoint.
    fn with_buffer<const NEW_BUFFER: usize>(
        self,
    ) -> (Sender<T, Codec, NEW_BUFFER>, Receiver<T, Codec, NEW_BUFFER, MAX_ITEM_SIZE>);

    /// Sets the maximum item size for the channel.
    fn with_max_item_size<const NEW_MAX_ITEM_SIZE: usize>(
        self,
    ) -> (Sender<T, Codec, BUFFER>, Receiver<T, Codec, BUFFER, NEW_MAX_ITEM_SIZE>);
}

impl<T, Codec, const BUFFER: usize, const MAX_ITEM_SIZE: usize> MpscExt<T, Codec, BUFFER, MAX_ITEM_SIZE>
    for (Sender<T, Codec, BUFFER>, Receiver<T, Codec, BUFFER, MAX_ITEM_SIZE>)
where
    T: Send + 'static,
{
    fn with_buffer<const NEW_BUFFER: usize>(
        self,
    ) -> (Sender<T, Codec, NEW_BUFFER>, Receiver<T, Codec, NEW_BUFFER, MAX_ITEM_SIZE>) {
        let (tx, rx) = self;
        let tx = tx.set_buffer();
        let rx = rx.set_buffer();
        (tx, rx)
    }

    fn with_max_item_size<const NEW_MAX_ITEM_SIZE: usize>(
        self,
    ) -> (Sender<T, Codec, BUFFER>, Receiver<T, Codec, BUFFER, NEW_MAX_ITEM_SIZE>) {
        let (mut tx, rx) = self;
        tx.set_max_item_size(NEW_MAX_ITEM_SIZE);
        let rx = rx.set_max_item_size();
        (tx, rx)
    }
}

/// Request to send data.
pub(crate) struct SendReq<T> {
    /// Value to send.
    pub value: Result<T, RecvError>,
    /// Channel for reporting result of sending.
    ///
    /// Present only if the sender awaits the send result via [`Sending`].
    pub result_tx: Option<tokio::sync::oneshot::Sender<Result<(), base::SendError<T>>>>,
}

impl<T> SendReq<T> {
    /// Creates a send request without result reporting.
    fn new(value: Result<T, RecvError>) -> Self {
        Self { value, result_tx: None }
    }

    /// Acknowledge reception and return contained value.
    fn ack(self) -> Result<T, RecvError> {
        let Self { value, result_tx } = self;
        if let Some(result_tx) = result_tx {
            let _ = result_tx.send(Ok(()));
        }
        value
    }
}

/// Type-erased access to [SendReq].
pub(crate) trait ErasedSendReq {
    /// Take the value out, replacing it with a dummy value.
    fn take_value(&mut self) -> AnySend;
    /// Report successful sending.
    fn result_ok(&mut self);
    /// Report a send error, returning it back if nobody listens on the result channel.
    fn result_err(&mut self, err: base::SendError<AnySend>) -> Result<(), base::SendError<AnySend>>;
}

impl<T> ErasedSendReq for SendReq<T>
where
    T: Send + 'static,
{
    fn take_value(&mut self) -> AnySend {
        let value = mem::replace(&mut self.value, Err(RecvError::RemoteConnect(chmux::ConnectError::Rejected)));
        Box::new(value)
    }

    fn result_ok(&mut self) {
        if let Some(result_tx) = self.result_tx.take() {
            let _ = result_tx.send(Ok(()));
        }
    }

    fn result_err(&mut self, err: base::SendError<AnySend>) -> Result<(), base::SendError<AnySend>> {
        let item: Result<T, RecvError> = *err.item.downcast().expect("type mismatch in SendReq");
        let Ok(item) = item else { return Ok(()) };
        let err = base::SendError { kind: err.kind, item };

        // Report the error to the caller if nobody is awaiting the send result.
        let err = match self.result_tx.take() {
            Some(result_tx) => match result_tx.send(Err(err)) {
                Ok(()) => return Ok(()),
                Err(res) => res.expect_err("sent item was error"),
            },
            None => err,
        };
        Err(base::SendError { kind: err.kind, item: Box::new(err.item) as AnySend })
    }
}

/// Create a send request and corresponding [Sending] instance for receiving result of send operation.
pub(crate) fn send_req<T>(value: Result<T, RecvError>) -> (SendReq<T>, Sending<T>) {
    let (result_tx, result_rx) = tokio::sync::oneshot::channel();
    let this = SendReq { value, result_tx: Some(result_tx) };
    let sent = Sending(result_rx);
    (this, sent)
}

trait ErasedMpscRx {
    fn recv_erased(&'_ mut self) -> BoxFuture<'_, Option<Box<dyn ErasedSendReq + Send>>>;
}

impl<T> ErasedMpscRx for tokio::sync::mpsc::Receiver<SendReq<T>>
where
    T: Send + 'static,
{
    fn recv_erased(&'_ mut self) -> BoxFuture<'_, Option<Box<dyn ErasedSendReq + Send>>> {
        async { self.recv().await.map(|send_req| Box::new(send_req) as Box<dyn ErasedSendReq + Send>) }.boxed()
    }
}

/// Send implementation for deserializer of Sender and serializer of Receiver.
async fn send_impl(
    erased_serializer: ErasedSerializer, mut rx: Box<dyn ErasedMpscRx + Send>, raw_tx: chmux::Sender,
    mut raw_rx: chmux::Receiver, remote_send_err_tx: tokio::sync::watch::Sender<Option<RemoteSendError>>,
    closed_tx: tokio::sync::watch::Sender<Option<ClosedReason>>, max_item_size: usize,
) {
    // Encode data using remote sender.
    let mut remote_tx = base::ErasedSender::new(erased_serializer, raw_tx);
    remote_tx.set_max_item_size(max_item_size);

    // Process events.
    loop {
        tokio::select! {
            biased;

            // Back channel message from remote endpoint.
            backchannel_msg = raw_rx.recv() => {
                match backchannel_msg {
                    Ok(Some(mut msg)) if msg.remaining() >= 1 => {
                        match msg.get_u8() {
                            BACKCHANNEL_MSG_CLOSE => {
                                let _ = remote_send_err_tx.send(Some(RemoteSendError::Closed));
                                let _ = closed_tx.send(Some(ClosedReason::Closed));
                                break;
                            }
                            BACKCHANNEL_MSG_ERROR => {
                                let _ = remote_send_err_tx.send(Some(RemoteSendError::Forward));
                                let _ = closed_tx.send(Some(ClosedReason::Failed));
                                break;
                            }
                            _ => (),
                        }
                    },
                    Ok(Some(_)) => (),
                    Ok(None) => {
                        let _ = remote_send_err_tx.send(Some(RemoteSendError::Send(
                            base::SendErrorKind::Send(chmux::SendError::Closed { gracefully: false })
                        )));
                        let _ = closed_tx.send(Some(ClosedReason::Dropped));
                        break;
                    }
                    _ => {
                        let _ = remote_send_err_tx.send(Some(RemoteSendError::Send(
                            base::SendErrorKind::Send(chmux::SendError::ChMux)
                        )));
                        let _ = closed_tx.send(Some(ClosedReason::Failed));
                        break;
                    },
                }
            }

            // Data to send to remote endpoint.
            send_req_opt = rx.recv_erased() => {
                let Some(mut send_req) = send_req_opt else { break };
                match remote_tx.send_erased(send_req.take_value()).await {
                    Ok(()) => send_req.result_ok(),
                    Err(err) => {
                        let _ = remote_send_err_tx.send(Some(RemoteSendError::Send(err.kind.clone())));
                        let _ = closed_tx.send(Some(ClosedReason::Failed));
                        if let Err(err) = send_req.result_err(err) && err.is_item_specific() {
                            tracing::warn!(%err, "sending over remote channel failed");
                        }
                    }
                }
            }
        }
    }
}

trait ErasedMpscTx {
    fn send(&'_ self, value: AnySend) -> BoxFuture<'_, Result<(), ()>>;
    fn send_err(&'_ self, err: RecvError) -> BoxFuture<'_, Result<(), ()>>;
}

impl<T> ErasedMpscTx for tokio::sync::mpsc::Sender<SendReq<T>>
where
    T: Send + 'static,
{
    fn send(&'_ self, value: AnySend) -> BoxFuture<'_, Result<(), ()>> {
        let value: Result<T, RecvError> = *value.downcast().expect("type mismatch in mpsc receiver");
        async { self.send(SendReq::new(value)).await.map_err(|_| ()) }.boxed()
    }

    fn send_err(&'_ self, err: RecvError) -> BoxFuture<'_, Result<(), ()>> {
        async { self.send(SendReq::new(Err(err))).await.map_err(|_| ()) }.boxed()
    }
}

/// Receive implementation for serializer of Sender and deserializer of Receiver.
async fn recv_impl(
    erased_deserializer: ErasedDeserializer, tx: &(dyn ErasedMpscTx + Send + Sync), mut raw_tx: chmux::Sender,
    raw_rx: chmux::Receiver, mut remote_send_err_rx: tokio::sync::watch::Receiver<Option<RemoteSendError>>,
    mut closed_rx: tokio::sync::watch::Receiver<Option<ClosedReason>>, max_item_size: usize,
) {
    // Decode raw received data using remote receiver.
    let mut remote_rx = base::ErasedReceiver::new(erased_deserializer, raw_rx);
    remote_rx.set_max_item_size(max_item_size);

    // Process events.
    loop {
        tokio::select! {
            biased;

            // Channel closure requested locally.
            res = closed_rx.changed() => {
                match res {
                    Ok(()) => {
                        let reason = closed_rx.borrow().clone();
                        match reason {
                            Some(ClosedReason::Closed) => {
                                let _ = raw_tx.send(vec![BACKCHANNEL_MSG_CLOSE].into()).await;
                            }
                            Some(ClosedReason::Dropped) => break,
                            Some(ClosedReason::Failed) => {
                                let _ = raw_tx.send(vec![BACKCHANNEL_MSG_ERROR].into()).await;
                            }
                            None => (),
                        }
                    },
                    Err(_) => break,
                }
            }

            // Notify remote endpoint of error.
            Ok(()) = remote_send_err_rx.changed() => {
                if remote_send_err_rx.borrow().as_ref().is_some() {
                    let _ = raw_tx.send(vec![BACKCHANNEL_MSG_ERROR].into()).await;
                }
            }

            // Data received from remote endpoint.
            res = remote_rx.recv_erased() => {
                match res {
                    Ok(Some(value)) => {
                        if tx.send(value).await.is_err() {
                            break
                        }
                    }
                    Ok(None) => break,
                    Err(err) => {
                        let is_final_err = err.is_final();
                        if tx.send_err(RecvError::RemoteReceive(err)).await.is_err() || is_final_err {
                            break
                        }
                    }
                }
            }
        }
    }
}