ractor 0.15.12

A actor framework 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright (c) Sean Lawlor
//
// This source code is licensed under both the MIT license found in the
// LICENSE-MIT file in the root directory of this source tree.

//! The thread-local module provides support for managing tasks which are not [Send]
//! safe, and must remain pinned to the same thread for their lifetime.
//!
//! IMPORTANT: This ONLY works currently with Tokio's `rt` feature, specifically due to the usage
//! of [tokio::task::LocalSet]

use std::future::Future;

use crate::concurrency::JoinHandle;
use crate::Actor as SendActor;
use crate::ActorCell;
use crate::ActorName;
use crate::ActorProcessingErr;
use crate::ActorRef;
use crate::Message;
use crate::RpcReplyPort;
use crate::SpawnErr;
use crate::State;
use crate::SupervisionEvent;

mod inner;
#[cfg(test)]
mod supervision_tests;
#[cfg(test)]
mod tests;

/// [ThreadLocalActor] defines the behavior of an Actor. It specifies the
/// Message type, State type, and all processing logic for the actor
///
/// NOTE: All of the implemented trait functions
///
/// * `pre_start`
/// * `post_start`
/// * `post_stop`
/// * `handle`
/// * `handle_serialized` (Available with `cluster` feature only)
/// * `handle_supervisor_evt`
///
/// return a [Result<_, ActorProcessingError>] where the error type is an
/// alias of [Box<dyn std::error::Error + Send + Sync + 'static>]. This is treated
/// as an "unhandled" error and will terminate the actor + execute necessary supervision
/// patterns. Panics are also captured from the inner functions and wrapped into an Error
/// type, however should an [Err(_)] result from any of these functions the **actor will
/// terminate** and cleanup.
///
/// # Example
///
/// ```
/// use ractor::thread_local::ThreadLocalActor;
/// use ractor::thread_local::ThreadLocalActorSpawner;
/// use ractor::ActorProcessingErr;
/// use ractor::ActorRef;
///
/// #[derive(Default)]
/// struct TheActor;
///
/// impl ThreadLocalActor for TheActor {
///     type Msg = ();
///     type Arguments = String;
///     type State = String;
///
///     async fn pre_start(
///         &self,
///         _myself: ActorRef<Self::Msg>,
///         args: Self::Arguments,
///     ) -> Result<Self::State, ActorProcessingErr> {
///         Ok(args)
///     }
///
///     async fn handle(
///         &self,
///         _myself: ActorRef<Self::Msg>,
///         _msg: (),
///         state: &mut Self::State,
///     ) -> Result<(), ActorProcessingErr> {
///         println!("Message! {state}");
///         Ok(())
///     }
/// }
///
/// #[tokio::main]
/// async fn main() {
///     // Create the thread-local spawner
///     let spawner = ThreadLocalActorSpawner::new();
///     // spawn the actor
///     let (who, handle) =
///         ractor::spawn_local::<TheActor>("Something".to_string(), spawner.clone())
///             .await
///             .expect("Failed to spawn thread-local actor!");
///
///     // send messages to the actor
///     who.cast(()).expect("Failed to send");
///     who.cast(()).expect("Failed to send");
///
///     // Tell the actor to drain then stop
///     who.drain();
///
///     // wait for the termination
///     handle.await.unwrap();
/// }
/// ```
pub trait ThreadLocalActor: Default + Sized + 'static {
    /// The message type for this actor
    type Msg: Message;

    /// The type of state this actor manages internally. This type
    /// has no bound requirements, and needs to neither be [Send] nor
    /// [Sync] when used in a [ThreadLocalActor] context.
    type State;

    /// Initialization arguments. These must be [Send] as they are
    /// sent to the pinned thread in order to startup the actor.
    /// However the actor's local [ThreadLocalActor::State] does
    /// NOT need to be [Send] and neither does the actor instance.
    type Arguments: State;

    /// Invoked when an actor is being started by the system.
    ///
    /// Any initialization inherent to the actor's role should be
    /// performed here hence why it returns the initial state.
    ///
    /// Panics in `pre_start` do not invoke the
    /// supervision strategy and the actor won't be started. [ThreadLocalActor]::`spawn`
    /// will return an error to the caller
    ///
    /// * `myself` - A handle to the [ActorCell] representing this actor
    /// * `args` - Arguments that are passed in the spawning of the actor which might
    ///   be necessary to construct the initial state
    ///
    /// Returns an initial [ThreadLocalActor::State] to bootstrap the actor
    fn pre_start(
        &self,
        myself: ActorRef<Self::Msg>,
        args: Self::Arguments,
    ) -> impl Future<Output = Result<Self::State, ActorProcessingErr>>;

    /// Invoked after an actor has started.
    ///
    /// Any post initialization can be performed here, such as writing
    /// to a log file, emitting metrics.
    ///
    /// Panics in `post_start` follow the supervision strategy.
    ///
    /// * `myself` - A handle to the [ActorCell] representing this actor
    /// * `state` - A mutable reference to the internal actor's state
    #[allow(unused_variables)]
    fn post_start(
        &self,
        myself: ActorRef<Self::Msg>,
        state: &mut Self::State,
    ) -> impl Future<Output = Result<(), ActorProcessingErr>> {
        async { Ok(()) }
    }

    /// Invoked after an actor has been stopped to perform final cleanup. In the
    /// event the actor is terminated with `Signal::Kill` or has self-panicked,
    /// `post_stop` won't be called.
    ///
    /// Panics in `post_stop` follow the supervision strategy.
    ///
    /// * `myself` - A handle to the [ActorCell] representing this actor
    /// * `state` - A mutable reference to the internal actor's last known state
    #[allow(unused_variables)]
    fn post_stop(
        &self,
        myself: ActorRef<Self::Msg>,
        state: &mut Self::State,
    ) -> impl Future<Output = Result<(), ActorProcessingErr>> {
        async { Ok(()) }
    }

    /// Handle the incoming message from the event processing loop. Unhandled panickes will be
    /// captured and sent to the supervisor(s)
    ///
    /// * `myself` - A handle to the [ActorCell] representing this actor
    /// * `message` - The message to process
    /// * `state` - A mutable reference to the internal actor's state
    #[allow(unused_variables)]
    fn handle(
        &self,
        myself: ActorRef<Self::Msg>,
        message: Self::Msg,
        state: &mut Self::State,
    ) -> impl Future<Output = Result<(), ActorProcessingErr>> {
        async { Ok(()) }
    }

    /// Handle the remote incoming message from the event processing loop. Unhandled panickes will be
    /// captured and sent to the supervisor(s)
    ///
    /// * `myself` - A handle to the [ActorCell] representing this actor
    /// * `message` - The serialized message to handle
    /// * `state` - A mutable reference to the internal actor's state
    #[allow(unused_variables)]
    #[cfg(feature = "cluster")]
    fn handle_serialized(
        &self,
        myself: ActorRef<Self::Msg>,
        message: crate::message::SerializedMessage,
        state: &mut Self::State,
    ) -> impl Future<Output = Result<(), ActorProcessingErr>> {
        async { Ok(()) }
    }

    /// Handle the incoming supervision event. Unhandled panics will be captured and
    /// sent the the supervisor(s). The default supervision behavior is to exit the
    /// supervisor on any child exit. To override this behavior, implement this function.
    ///
    /// * `myself` - A handle to the [ActorCell] representing this actor
    /// * `message` - The message to process
    /// * `state` - A mutable reference to the internal actor's state
    #[allow(unused_variables)]
    fn handle_supervisor_evt(
        &self,
        myself: ActorRef<Self::Msg>,
        message: SupervisionEvent,
        state: &mut Self::State,
    ) -> impl Future<Output = Result<(), ActorProcessingErr>> {
        async move {
            match message {
                SupervisionEvent::ActorTerminated(who, _, _)
                | SupervisionEvent::ActorFailed(who, _) => {
                    myself.stop(None);
                }
                _ => {}
            }
            Ok(())
        }
    }

    /// Spawn an actor of this type, which is unsupervised, automatically starting
    ///
    /// * `name`: A name to give the actor. Useful for global referencing or debug printing
    /// * `startup_args`: Arguments passed to the `pre_start` call of the [ThreadLocalActor] to facilitate startup and
    ///   initial state creation
    /// * `spawner`: The [ThreadLocalActorSpawner] which will control starting this actor on a specific
    ///   thread
    ///
    /// Returns a [Ok((ActorRef, JoinHandle<()>))] upon successful start, denoting the actor reference
    /// along with the join handle which will complete when the actor terminates. Returns [Err(SpawnErr)] if
    /// the actor failed to start
    fn spawn(
        name: Option<ActorName>,
        startup_args: Self::Arguments,
        spawner: ThreadLocalActorSpawner,
    ) -> impl Future<Output = Result<(ActorRef<Self::Msg>, JoinHandle<()>), SpawnErr>> {
        inner::ThreadLocalActorRuntime::<Self>::spawn(name, startup_args, spawner)
    }
    /// Spawn an actor instantly, not waiting on the actor's `pre_start` routine.
    ///
    /// Returns a [Ok((ActorRef, JoinHandle<Result<JoinHandle<()>, SpawnErr>>))] upon successful creation of the
    /// message queues, so you can begin sending messages. However the associated [JoinHandle] contains the inner
    /// information around if the actor successfully started or not in it's `pre_start` routine. Returns [Err(SpawnErr)] if
    /// the actor name is already allocated
    #[allow(clippy::type_complexity)]
    fn spawn_instant(
        name: Option<ActorName>,
        startup_args: Self::Arguments,
        spawner: ThreadLocalActorSpawner,
    ) -> Result<
        (
            ActorRef<Self::Msg>,
            JoinHandle<Result<JoinHandle<()>, SpawnErr>>,
        ),
        SpawnErr,
    > {
        inner::ThreadLocalActorRuntime::<Self>::spawn_instant(name, startup_args, spawner)
    }
    /// Spawn an actor of this type with a supervisor, automatically starting the actor
    ///
    /// * `name`: A name to give the actor. Useful for global referencing or debug printing
    /// * `startup_args`: Arguments passed to the `pre_start` call of the [ThreadLocalActor] to
    ///   facilitate startup and initial state creation
    /// * `supervisor`: The [ActorCell] which is to become the supervisor (parent) of this actor
    /// * `spawner`: The [ThreadLocalActorSpawner] which will control starting this actor on a specific
    ///   thread
    ///
    /// Returns a [Ok((ActorRef, JoinHandle<()>))] upon successful start, denoting the actor reference
    /// along with the join handle which will complete when the actor terminates. Returns [Err(SpawnErr)] if
    /// the actor failed to start
    fn spawn_linked(
        name: Option<ActorName>,
        startup_args: Self::Arguments,
        supervisor: ActorCell,
        spawner: ThreadLocalActorSpawner,
    ) -> impl Future<Output = Result<(ActorRef<Self::Msg>, JoinHandle<()>), SpawnErr>> {
        inner::ThreadLocalActorRuntime::<Self>::spawn_linked(
            name,
            startup_args,
            spawner,
            supervisor,
        )
    }
    /// Spawn an actor instantly with a supervisor, not waiting on the actor's `pre_start` routine.
    #[allow(clippy::type_complexity)]
    fn spawn_linked_instant(
        name: Option<ActorName>,
        startup_args: Self::Arguments,
        supervisor: ActorCell,
        spawner: ThreadLocalActorSpawner,
    ) -> Result<
        (
            ActorRef<Self::Msg>,
            JoinHandle<Result<JoinHandle<()>, SpawnErr>>,
        ),
        SpawnErr,
    > {
        inner::ThreadLocalActorRuntime::<Self>::spawn_linked_instant(
            name,
            startup_args,
            spawner,
            supervisor,
        )
    }
}

impl<T> ThreadLocalActor for T
where
    T: SendActor + Default,
{
    type Msg = <T as SendActor>::Msg;
    type State = <T as SendActor>::State;
    type Arguments = <T as SendActor>::Arguments;

    async fn pre_start(
        &self,
        myself: ActorRef<Self::Msg>,
        args: Self::Arguments,
    ) -> Result<Self::State, ActorProcessingErr> {
        <Self as SendActor>::pre_start(self, myself, args).await
    }

    async fn post_start(
        &self,
        myself: ActorRef<Self::Msg>,
        state: &mut Self::State,
    ) -> Result<(), ActorProcessingErr> {
        <Self as SendActor>::post_start(self, myself, state).await
    }

    async fn handle(
        &self,
        myself: ActorRef<Self::Msg>,
        message: Self::Msg,
        state: &mut Self::State,
    ) -> Result<(), ActorProcessingErr> {
        <Self as SendActor>::handle(self, myself, message, state).await
    }

    #[cfg(feature = "cluster")]
    async fn handle_serialized(
        &self,
        myself: ActorRef<Self::Msg>,
        message: crate::message::SerializedMessage,
        state: &mut Self::State,
    ) -> Result<(), ActorProcessingErr> {
        <Self as SendActor>::handle_serialized(self, myself, message, state).await
    }

    async fn handle_supervisor_evt(
        &self,
        myself: ActorRef<Self::Msg>,
        message: SupervisionEvent,
        state: &mut Self::State,
    ) -> Result<(), ActorProcessingErr> {
        <Self as SendActor>::handle_supervisor_evt(self, myself, message, state).await
    }
}

#[allow(clippy::type_complexity)]
struct SpawnArgs {
    builder: Box<
        dyn FnOnce() -> std::pin::Pin<Box<dyn Future<Output = Result<JoinHandle<()>, SpawnErr>>>>
            + Send,
    >,
    reply: RpcReplyPort<JoinHandle<Result<JoinHandle<()>, SpawnErr>>>,
    name: Option<String>,
}

/// The [ThreadLocalActorSpawner] is responsible for spawning [ThreadLocalActor] instances
/// which do not require [Send] restrictions and will be pinned to the current thread. You can
/// make multiple of these to "load balance" actors across threads and can spawn multiple actors
/// on a single one to be shared on a single thread.
#[derive(Clone)]
pub struct ThreadLocalActorSpawner {
    send: crate::concurrency::MpscUnboundedSender<SpawnArgs>,
}

impl std::fmt::Debug for ThreadLocalActorSpawner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ThreadLocalActorSpawner")
    }
}

impl Default for ThreadLocalActorSpawner {
    fn default() -> Self {
        Self::new()
    }
}

// note: it's not great that we have multiple implementations depending on the async backend in here
// - ideally this would be moved to ./concurrency/*
impl ThreadLocalActorSpawner {
    #[cfg(all(not(feature = "async-std"), not(target_arch = "wasm32")))]
    /// Create a new [ThreadLocalActorSpawner] on the current thread.
    pub fn new() -> Self {
        let (send, mut recv) = crate::concurrency::mpsc_unbounded();

        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();

        std::thread::spawn(move || {
            let local = tokio::task::LocalSet::new();

            // TODO (seanlawlor): Support named spawn
            local.spawn_local(async move {
                while let Some(SpawnArgs {
                    builder,
                    reply,
                    name,
                }) = recv.recv().await
                {
                    let fut = builder();
                    #[cfg(tokio_unstable)]
                    {
                        let handle = tokio::task::Builder::new()
                            .name(name.unwrap_or_default().as_str())
                            .spawn_local(fut)
                            .expect("Tokio task spawn failed");
                        _ = reply.send(handle);
                    }
                    #[cfg(not(tokio_unstable))]
                    {
                        _ = name;
                        let handle = crate::concurrency::spawn_local(fut);
                        _ = reply.send(handle);
                    }
                }
                // If the while loop returns, then all the LocalSpawner
                // objects have been dropped.
            });

            // This will return once all senders are dropped and all
            // spawned tasks have returned.
            rt.block_on(local);
        });

        Self { send }
    }

    #[cfg(all(feature = "async-std", not(target_arch = "wasm32")))]
    /// Create a new [ThreadLocalActorSpawner] on the current thread.
    pub fn new() -> Self {
        let (send, mut recv) = crate::concurrency::mpsc_unbounded();

        std::thread::spawn(move || {
            // TODO (seanlawlor): Support named spawn
            async_std::task::block_on(async_std::task::spawn_local(async move {
                while let Some(SpawnArgs {
                    builder,
                    reply,
                    name,
                }) = recv.recv().await
                {
                    let fut = builder();
                    _ = name;
                    let handle = crate::concurrency::spawn_local(fut);
                    _ = reply.send(handle);
                }
            }));
        });

        Self { send }
    }

    #[cfg(target_arch = "wasm32")]
    /// Create a new [ThreadLocalActorSpawner] on the current thread.
    pub fn new() -> Self {
        let (send, mut recv) = crate::concurrency::mpsc_unbounded();

        crate::concurrency::spawn_local(async move {
            while let Some(SpawnArgs {
                builder,
                reply,
                name: _name,
            }) = recv.recv().await
            {
                let fut = builder();
                let handle = crate::concurrency::spawn_local(fut);
                _ = reply.send(handle);
            }
        });

        Self { send }
    }

    #[allow(clippy::type_complexity)]
    async fn spawn(
        &self,
        builder: Box<
            dyn FnOnce()
                    -> std::pin::Pin<Box<dyn Future<Output = Result<JoinHandle<()>, SpawnErr>>>>
                + Send,
        >,
        name: Option<String>,
    ) -> Result<JoinHandle<()>, SpawnErr> {
        let (tx, rx) = crate::concurrency::oneshot();
        let args = SpawnArgs {
            builder,
            reply: tx.into(),
            name,
        };

        if self.send.send(args).is_err() {
            return Err(SpawnErr::StartupFailed("Spawner dead".into()));
        }
        let rx_result = rx
            .await
            .map_err(|inner| SpawnErr::StartupFailed(inner.into()))?
            .await;

        #[cfg(not(feature = "async-std"))]
        {
            rx_result.map_err(|joinerr| SpawnErr::StartupFailed(joinerr.into()))?
        }

        #[cfg(feature = "async-std")]
        {
            rx_result.map_err(|()| SpawnErr::StartupFailed("receive failed".into()))?
        }
    }
}

impl ActorCell {
    /// Spawn an actor of the given type as a thread-local child of this actor, automatically starting the actor.
    /// This [ActorCell] becomes the supervisor of the child actor.
    ///
    /// * `name`: A name to give the actor. Useful for global referencing or debug printing
    /// * `handler` The implementation of Self
    /// * `startup_args`: Arguments passed to the `pre_start` call of the [ThreadLocalActor] to facilitate startup and
    ///   initial state creation
    ///
    /// Returns a [Ok((ActorRef, JoinHandle<()>))] upon successful start, denoting the actor reference
    /// along with the join handle which will complete when the actor terminates. Returns [Err(SpawnErr)] if
    /// the actor failed to start
    pub async fn spawn_local_linked<T: ThreadLocalActor>(
        &self,
        name: Option<String>,
        startup_args: T::Arguments,
        spawner: ThreadLocalActorSpawner,
    ) -> Result<(ActorRef<T::Msg>, JoinHandle<()>), SpawnErr> {
        T::spawn_linked(name, startup_args, self.clone(), spawner).await
    }
}