acto 0.8.0

light-weight Actor library for Rust
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
use crate::{
    runtime::MailboxSize, ActoAborted, ActoHandle, ActoId, ActoRuntime, PanicInfo, PanicOrAbort,
    Receiver, Sender,
};
use parking_lot::RwLock;
use smol_str::SmolStr;
use std::{
    any::{type_name, Any},
    future::Future,
    ops::Deref,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};
use tokio::{
    runtime::{Builder, Handle, Runtime},
    sync::mpsc::{self, error::TrySendError},
    task::JoinError,
};

/// Owned handle to an [`AcTokioRuntime`].
///
/// Dropping this handle will abort all actors spawned by this runtime.
pub struct AcTokio(AcTokioRuntime);

impl AcTokio {
    /// Create a new [`AcTokio`] runtime with the given name and number of threads.
    ///
    /// ```
    /// # use acto::{AcTokio, ActoInput, ActoRuntime, ActoHandle};
    /// let rt = AcTokio::new("test", 1).unwrap();
    ///
    /// let actor = rt.spawn_actor("test", |mut ctx| async move {
    ///     let ActoInput::<String, ()>::Message(name) = ctx.recv().await else { return };
    ///     println!("Hello, {}!", name);
    /// });
    ///
    /// // Send a message to the actor, after which it will terminate.
    /// actor.me.send("world".to_owned());
    /// // Wait for the actor to terminate.
    /// rt.with_rt(|rt| rt.block_on(actor.handle.join())).unwrap();
    /// ```
    ///
    /// In the above example the last step is crucial, without it nothing may be printed.
    /// The following demonstrates that this object owns the tokio runtime:
    ///
    /// ```
    /// # use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
    /// use acto::{AcTokio, ActoRuntime, ActoCell};
    ///
    /// let flag = Arc::new(AtomicBool::new(false));
    ///
    /// // This serves as a drop detector so we know the actor was killed.
    /// struct X(Arc<AtomicBool>);
    /// impl Drop for X {
    ///     fn drop(&mut self) {
    ///         self.0.store(true, Ordering::Relaxed);
    ///     }
    /// }
    /// let x = X(flag.clone());
    ///
    /// let tokio = AcTokio::new("test", 1).unwrap();
    /// tokio.spawn_actor("test", move |mut ctx: ActoCell<(), _>| async move {
    ///     // make sure to move the drop detector inside this scope
    ///     let _y = x;
    ///     // wait forever
    ///     loop { ctx.recv().await; }
    /// });
    ///
    /// // this will synchronously await termination of all actors and of the threads that run them
    /// drop(tokio);
    ///
    /// // verify that indeed the `_y` was dropped inside the actor
    /// assert!(flag.load(Ordering::Relaxed));
    /// ```
    pub fn new(name: impl Into<String>, num_threads: usize) -> std::io::Result<Self> {
        Ok(Self(AcTokioRuntime::new(name, num_threads)?))
    }

    /// Create a new [`AcTokio`] runtime from an existing [`tokio::runtime::Handle`].
    ///
    /// This is useful if you want to use an existing tokio runtime, for example if you want to use [`acto`] in a library.
    /// Dropping this value will not abort the actors spawned by this runtime, but it will prevent new actors from
    /// being spawned, so make sure to keep it around long enough!
    pub fn from_handle(name: impl Into<String>, handle: Handle) -> Self {
        Self(AcTokioRuntime::from_handle(name, handle))
    }
}

impl Deref for AcTokio {
    type Target = AcTokioRuntime;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Drop for AcTokio {
    fn drop(&mut self) {
        self.0.inner.rt.write().take();
    }
}

/// An [`ActoRuntime`] based on [`tokio::runtime`] and [`tokio::sync::mpsc`] queues.
///
/// In order to create it, see [`AcTokio::new()`].
#[derive(Clone)]
pub struct AcTokioRuntime {
    inner: Arc<Inner>,
    mailbox_size: usize,
}

impl AcTokioRuntime {
    fn new(name: impl Into<String>, num_threads: usize) -> std::io::Result<Self> {
        let name = name.into();
        tracing::debug!(%name, ?num_threads, "creating");
        let rt = Builder::new_multi_thread()
            .thread_name(&name)
            .worker_threads(num_threads)
            .enable_all()
            .build()?;
        Ok(Self {
            inner: Arc::new(Inner {
                name,
                rt: RwLock::new(Some(RuntimeOrHandle::Runtime(rt))),
            }),
            mailbox_size: 128,
        })
    }

    fn from_handle(name: impl Into<String>, handle: Handle) -> Self {
        let name = name.into();
        tracing::debug!(%name, "creating");
        Self {
            inner: Arc::new(Inner {
                name,
                rt: RwLock::new(Some(RuntimeOrHandle::Handle(handle))),
            }),
            mailbox_size: 128,
        }
    }

    /// Perform a task using the underlying runtime.
    ///
    /// Beware that while this function is running, dropping the [`AcTokio`] handle will block until the function is finished.
    /// Returns `None` if the runtime has been dropped.
    pub fn with_rt<U>(&self, f: impl FnOnce(&Handle) -> U) -> Option<U> {
        let _span = tracing::debug_span!("with_rt").entered();
        self.inner.rt.read().as_ref().map(|rt| f(&*rt))
    }
}

enum RuntimeOrHandle {
    Runtime(Runtime),
    Handle(Handle),
}

impl Deref for RuntimeOrHandle {
    type Target = Handle;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Runtime(rt) => rt.handle(),
            Self::Handle(h) => h,
        }
    }
}

struct Inner {
    name: String,
    rt: RwLock<Option<RuntimeOrHandle>>,
}

impl ActoRuntime for AcTokioRuntime {
    type ActoHandle<O: Send + 'static> = TokioJoinHandle<O>;
    type Sender<M: Send + 'static> = TokioSender<M>;
    type Receiver<M: Send + 'static> = TokioReceiver<M>;

    fn name(&self) -> &str {
        &self.inner.name
    }

    fn mailbox<M: Send + 'static>(&self) -> (Self::Sender<M>, Self::Receiver<M>) {
        let (tx, rx) = mpsc::channel(self.mailbox_size);
        (TokioSender(tx), TokioReceiver(rx))
    }

    fn spawn_task<T>(&self, id: ActoId, name: SmolStr, task: T) -> Self::ActoHandle<T::Output>
    where
        T: std::future::Future + Send + 'static,
        T::Output: Send + 'static,
    {
        let _span = tracing::debug_span!("spawn_task").entered();
        TokioJoinHandle(
            id,
            name,
            self.inner.rt.read().as_ref().map(|rt| rt.spawn(task)),
        )
    }
}

impl MailboxSize for AcTokioRuntime {
    type Output = Self;

    fn with_mailbox_size(&self, mailbox_size: usize) -> Self::Output {
        Self {
            inner: self.inner.clone(),
            mailbox_size,
        }
    }
}

pub struct TokioSender<M>(mpsc::Sender<M>);
impl<M: Send + 'static> Sender<M> for TokioSender<M> {
    fn send(&self, msg: M) -> bool {
        if let Err(e) = self.0.try_send(msg) {
            match e {
                TrySendError::Full(_) => {
                    tracing::debug!(
                        msg = type_name::<M>(),
                        "dropping message due to full mailbox"
                    );
                }
                TrySendError::Closed(_) => {
                    tracing::debug!(
                        msg = type_name::<M>(),
                        "dropping message due to closed mailbox"
                    );
                }
            }
            return false;
        }
        true
    }
    fn send_wait(&self, msg: M) -> Pin<Box<dyn Future<Output = bool> + Send + 'static>> {
        let tx = self.0.clone();
        Box::pin(async move {
            if let Err(_) = tx.send(msg).await {
                tracing::debug!(
                    msg = type_name::<M>(),
                    "dropping message due to closed mailbox"
                );
                return false;
            }
            true
        })
    }
}

pub struct TokioReceiver<M>(mpsc::Receiver<M>);
impl<M: Send + 'static> Receiver<M> for TokioReceiver<M> {
    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<M> {
        self.0.poll_recv(cx).map(Option::unwrap)
    }
}

pub struct TokioJoinHandle<O>(ActoId, SmolStr, Option<tokio::task::JoinHandle<O>>);

impl<O: Send + 'static> ActoHandle for TokioJoinHandle<O> {
    type Output = O;

    fn id(&self) -> ActoId {
        self.0
    }

    fn name(&self) -> &str {
        &self.1
    }

    fn abort_pinned(self: Pin<&mut Self>) {
        tracing::debug!(name = ?self.0, handle = ?self.2.is_some(), "aborting");
        let handle = self.get_mut().2.take();
        if let Some(handle) = handle {
            handle.abort();
        }
    }

    fn is_finished(&self) -> bool {
        self.2.as_ref().map(|h| h.is_finished()).unwrap_or(true)
    }

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<O, PanicOrAbort>> {
        if let Some(handle) = &mut self.as_mut().get_mut().2 {
            Pin::new(handle).poll(cx).map(|r| {
                r.map_err(|err| {
                    tracing::debug!(?err, "actor aborted");
                    PanicOrAbort::Panic(Box::new(TokioPanic::from(err)))
                })
            })
        } else {
            tracing::debug!("actor aborted");
            Poll::Ready(Err(PanicOrAbort::Abort(ActoAborted::new(
                self.as_ref().1.as_str(),
            ))))
        }
    }
}

#[derive(Debug)]
enum TokioPanic {
    Join(Box<dyn Any + Send + 'static>),
    Cancelled,
}

impl PanicInfo for TokioPanic {
    fn is_cancelled(&self) -> bool {
        matches!(self, Self::Cancelled)
    }

    fn payload(&self) -> Option<&(dyn Any + Send + 'static)> {
        match self {
            TokioPanic::Join(j) => Some(j.as_ref()),
            TokioPanic::Cancelled => None,
        }
    }

    fn cause(&self) -> String {
        match self {
            TokioPanic::Join(j) => j
                .downcast_ref::<&'static str>()
                .map(|s| *s)
                .or_else(|| j.downcast_ref::<String>().map(|s| &**s))
                .unwrap_or("opaque panic")
                .to_owned(),
            TokioPanic::Cancelled => "cancelled via Tokio".to_owned(),
        }
    }
}

impl From<JoinError> for TokioPanic {
    fn from(err: JoinError) -> Self {
        if err.is_cancelled() {
            Self::Cancelled
        } else {
            Self::Join(err.into_panic())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::AcTokio;
    use crate::{ActoCell, ActoHandle, ActoId, ActoInput, ActoRuntime, SupervisionRef};
    use std::{
        collections::BTreeMap,
        sync::{
            atomic::{AtomicBool, Ordering},
            Arc,
        },
        time::Duration,
    };
    use tokio::sync::oneshot;

    #[test]
    fn run() {
        let sys = AcTokio::new("test", 1).unwrap();
        let flag = Arc::new(AtomicBool::new(false));
        let flag2 = flag.clone();
        let SupervisionRef { me: r, handle: j } =
            sys.spawn_actor("super", |mut ctx: ActoCell<_, _>| async move {
                flag2.store(true, Ordering::Relaxed);
                ctx.recv().await;
                flag2.store(false, Ordering::Relaxed);
                42
            });
        std::thread::sleep(Duration::from_millis(100));
        assert!(flag.load(Ordering::Relaxed));
        r.send(());
        std::thread::sleep(Duration::from_millis(100));
        assert!(!flag.load(Ordering::Relaxed));
        let ret = sys.with_rt(|rt| rt.block_on(j.join())).unwrap();
        assert_eq!(ret.unwrap(), 42);
    }

    #[test]
    fn child() {
        let sys = AcTokio::new("test", 2).unwrap();
        let SupervisionRef { me: r, handle: j } = sys.spawn_actor(
            "super",
            |mut ctx: ActoCell<_, _, Result<i32, i32>>| async move {
                let mut v: Vec<i32> = vec![];
                let mut running = BTreeMap::<ActoId, (i32, oneshot::Sender<()>)>::new();
                loop {
                    match ctx.recv().await {
                        ActoInput::NoMoreSenders => break,
                        ActoInput::Supervision { id, result, .. } => {
                            let (arg, tx) = running.remove(&id).unwrap();
                            v.push(arg);
                            v.push(result.unwrap().unwrap_or_else(|x| x));
                            tx.send(()).unwrap();
                        }
                        ActoInput::Message((x, tx)) => {
                            v.push(x);
                            let r = ctx.spawn_supervised(
                                "child",
                                |mut ctx: ActoCell<_, _>| async move {
                                    if let ActoInput::Message(x) = ctx.recv().await {
                                        Ok(2 * x)
                                    } else {
                                        Err(5)
                                    }
                                },
                            );
                            r.send(x);
                            running.insert(r.id(), (x, tx));
                        }
                    }
                }
                v
            },
        );
        let (tx, rx) = oneshot::channel();
        r.send((1, tx));
        sys.with_rt(|rt| rt.block_on(rx)).unwrap().unwrap();
        let (tx, rx) = oneshot::channel();
        r.send((2, tx));
        sys.with_rt(|rt| rt.block_on(rx)).unwrap().unwrap();
        drop(r);
        let v = sys.with_rt(|rt| rt.block_on(j.join())).unwrap().unwrap();
        assert_eq!(v, vec![1, 1, 2, 2, 2, 4]);
    }
}