speare 0.4.3

actor-like thin abstraction over tokio::task and flume channels
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
use flume::{Receiver, Sender};
use futures_core::Stream;
use std::any::Any;
use std::{
    cmp,
    collections::HashMap,
    future::Future,
    sync::{Arc, RwLock},
    time::Duration,
};
use tokio::{
    task::{self, JoinSet},
    time,
};

pub mod mini;

mod exit;
mod node;
mod pubsub;
mod req_res;
mod streams;
mod watch;

pub use exit::*;
pub use node::*;
pub use pubsub::PubSubError;
pub use req_res::*;
pub use streams::{SourceSet, Sources};

use crate::pubsub::PubSub;
use crate::watch::{NoWatch, OnErrTerminate, WatchFn};

/// A thin abstraction over tokio tasks and flume channels, allowing for easy message passing
/// with a supervision tree to handle failures.
///
/// ## Example
/// ```
/// use speare::{Ctx, Actor};
/// use derive_more::From;
///
/// struct Counter {
///     count: u32,
/// }
///
/// struct CounterProps {
///     initial_count: u32,
///     max_count: u32,
/// }
///
/// #[derive(From)]
/// enum CounterMsg {
///     Inc(u32),
/// }
///
/// enum CounterErr {
///     MaxCountExceeded,
/// }
///
/// impl Actor for Counter {
///     type Props = CounterProps;
///     type Msg = CounterMsg;
///     type Err = CounterErr;
///
///     async fn init(ctx: &mut Ctx<Self>) -> Result<Self, Self::Err> {
///         Ok(Counter {
///             count: ctx.props().initial_count,
///         })
///     }
///
///     async fn handle(&mut self, msg: Self::Msg, ctx: &mut Ctx<Self>) -> Result<(), Self::Err> {
///         match msg {
///             CounterMsg::Inc(x) => {
///                 self.count += x;
///
///                 if self.count > ctx.props().max_count {
///                     return Err(CounterErr::MaxCountExceeded);
///                 }
///             }
///         }
///
///         Ok(())
///     }
/// }
/// ```
#[allow(unused_variables)]
pub trait Actor: Sized + Send + 'static {
    type Props: Send + 'static;
    type Msg: Send + 'static;
    type Err: Send + Sync + 'static;

    /// Constructs the actor. Called on initial spawn and on every restart.
    ///
    /// # Example
    /// ```ignore
    /// async fn init(ctx: &mut Ctx<Self>) -> Result<Self, Self::Err> {
    ///     Ok(MyActor { count: ctx.props().initial })
    /// }
    /// ```
    fn init(ctx: &mut Ctx<Self>) -> impl Future<Output = Result<Self, Self::Err>> + Send;

    /// Cleanup hook called when the actor stops, restarts, or fails to init.
    /// `this` is `None` if init failed.
    ///
    /// # Example
    /// ```ignore
    /// async fn exit(this: Option<Self>, reason: ExitReason<Self>, ctx: &mut Ctx<Self>) {
    ///     if let ExitReason::Err(e) = reason {
    ///         eprintln!("actor failed: {e:?}");
    ///     }
    /// }
    /// ```
    fn exit(
        this: Option<Self>,
        reason: ExitReason<Self>,
        ctx: &mut Ctx<Self>,
    ) -> impl Future<Output = ()> + Send {
        async {}
    }

    /// Sets up message sources (streams, intervals) after init.
    ///
    /// Sources added earlier in the [`SourceSet`] chain have higher polling priority.
    /// If an earlier source is consistently ready, later sources may be starved.
    ///
    /// # Example
    /// ```ignore
    /// async fn sources(&self, ctx: &Ctx<Self>) -> Result<impl Sources<Self>, Self::Err> {
    ///     Ok(SourceSet::new()
    ///         .interval(time::interval(Duration::from_millis(100)), || Msg::Tick)
    ///         .stream(my_stream))
    /// }
    /// ```
    fn sources(
        &self,
        ctx: &Ctx<Self>,
    ) -> impl Future<Output = Result<impl Sources<Self>, Self::Err>> + Send {
        async { Ok(SourceSet::new()) }
    }

    /// Called everytime your [`Actor`] receives a message.
    ///
    /// # Example
    /// ```ignore
    /// async fn handle(&mut self, msg: Self::Msg, ctx: &mut Ctx<Self>) -> Result<(), Self::Err> {
    ///     match msg {
    ///         Msg::Inc(n) => self.count += n,
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    fn handle(
        &mut self,
        msg: Self::Msg,
        ctx: &mut Ctx<Self>,
    ) -> impl Future<Output = Result<(), Self::Err>> + Send {
        async { Ok(()) }
    }
}

/// A handle to send messages to or stop an [`Actor`].
pub struct Handle<Msg> {
    msg_tx: Sender<Msg>,
    proc_msg_tx: Sender<ProcMsg>,
}

impl<Msg> Clone for Handle<Msg> {
    fn clone(&self) -> Self {
        Self {
            msg_tx: self.msg_tx.clone(),
            proc_msg_tx: self.proc_msg_tx.clone(),
        }
    }
}

impl<Msg> Handle<Msg> {
    /// Stops the [`Actor`] associated with this handle. Does not wait for the actor to finish.
    ///
    /// # Example
    /// ```ignore
    /// handle.stop();
    /// ```
    pub fn stop(&self) {
        let (tx, _) = flume::unbounded();
        let _ = self
            .proc_msg_tx
            .send(ProcMsg::FromHandle(ProcAction::Stop(tx)));
    }

    /// Restarts the [`Actor`] by re-running [`Actor::init`] and [`Actor::sources`]. Does not wait for the actor to finish.
    ///
    /// # Example
    /// ```ignore
    /// handle.restart();
    /// ```
    pub fn restart(&self) {
        let _ = self
            .proc_msg_tx
            .send(ProcMsg::FromHandle(ProcAction::Restart));
    }

    /// Returns `true` if the [`Actor`] is still running.
    ///
    /// # Example
    /// ```ignore
    /// if handle.is_alive() {
    ///     handle.send(Msg::Ping);
    /// }
    /// ```
    pub fn is_alive(&self) -> bool {
        !self.msg_tx.is_disconnected()
    }

    /// Sends a message to the [`Actor`], returning `true` if the message was delivered
    /// or `false` if the actor is no longer running.
    /// Takes advantage of `From<_>` implementations on the message type.
    ///
    /// # Example
    /// ```ignore
    /// // Given `#[derive(From)] enum Msg { Inc(u32) }`:
    /// handle.send(Msg::Inc(1));
    /// handle.send(1u32); // works via From<u32>
    /// ```
    pub fn send<M: Into<Msg>>(&self, msg: M) -> bool {
        self.msg_tx.send(msg.into()).is_ok()
    }

    /// Sends a message to the [`Actor`] after the given duration, failing silently if it is no longer running.
    ///
    /// # Example
    /// ```ignore
    /// handle.send_in(Msg::Timeout, Duration::from_secs(5));
    /// ```
    pub fn send_in<M>(&self, msg: M, duration: Duration)
    where
        Msg: 'static + Send,
        M: 'static + Send + Into<Msg>,
    {
        let msg_tx = self.msg_tx.clone();

        task::spawn(async move {
            time::sleep(duration).await;
            let _ = msg_tx.send(msg.into());
        });
    }

    /// Sends a request and awaits a response. Requires `Msg: From<Request<Req, Res>>`.
    ///
    /// # Example
    /// ```ignore
    /// #[derive(From)]
    /// enum Msg {
    ///     GetCount(Request<(), u32>),
    /// }
    ///
    /// // sender side:
    /// let count: u32 = handle.req(()).await?;
    ///
    /// // receiver side, inside handle():
    /// Msg::GetCount(req) => req.reply(self.count),
    /// ```
    pub async fn req<Req, Res>(&self, req: Req) -> Result<Res, ReqErr>
    where
        Msg: From<Request<Req, Res>>,
    {
        let (req, res) = req_res(req);
        self.send(req);
        res.recv().await
    }

    /// Like [`Handle::req`], but uses a wrapper function to convert the [`Request`] into the message type.
    /// Useful when the message variant can't implement `From<Request<Req, Res>>`.
    ///
    /// # Example
    /// ```ignore
    /// enum Msg {
    ///     GetCount(Request<(), u32>),
    /// }
    ///
    /// let count: u32 = handle.reqw(Msg::GetCount, ()).await?;
    /// ```
    pub async fn reqw<F, Req, Res>(&self, to_req: F, req: Req) -> Result<Res, ReqErr>
    where
        F: Fn(Request<Req, Res>) -> Msg,
    {
        let (req, res) = req_res(req);
        let msg = to_req(req);
        self.send(msg);
        res.recv().await
    }

    /// Like [`Handle::req`], but fails with [`ReqErr::Timeout`] if no response within the given [`Duration`].
    ///
    /// # Example
    /// ```ignore
    /// let count: u32 = handle.req_timeout((), Duration::from_secs(1)).await?;
    /// ```
    pub async fn req_timeout<Req, Res>(&self, req: Req, timeout: Duration) -> Result<Res, ReqErr>
    where
        Msg: From<Request<Req, Res>>,
    {
        let (req, res) = req_res(req);
        self.send(req);
        res.recv_timeout(timeout).await
    }

    /// Like [`Handle::reqw`], but fails with [`ReqErr::Timeout`] if no response within the given [`Duration`].
    ///
    /// # Example
    /// ```ignore
    /// let count: u32 = handle.reqw_timeout(Msg::GetCount, (), Duration::from_secs(1)).await?;
    /// ```
    pub async fn reqw_timeout<F, Req, Res>(
        &self,
        to_req: F,
        req: Req,
        timeout: Duration,
    ) -> Result<Res, ReqErr>
    where
        F: Fn(Request<Req, Res>) -> Msg,
    {
        let (req, res) = req_res(req);
        let msg = to_req(req);
        self.send(msg);
        res.recv_timeout(timeout).await
    }
}

/// The context surrounding the current `Actor`.
///
/// Provides a collection of methods that allow you to:
/// - spawn other actors as children of the current actor
/// - access the `Handle<_>` for the currrent actor
/// - access this actor's props
/// - clear this actor's mailbox
pub struct Ctx<P>
where
    P: Actor,
{
    id: u64,
    props: P::Props,
    handle: Handle<P::Msg>,
    msg_rx: Receiver<P::Msg>,
    parent_proc_msg_tx: Option<Sender<ProcMsg>>,
    proc_msg_rx: Receiver<ProcMsg>,
    children_proc_msg_tx: HashMap<u64, Sender<ProcMsg>>,
    supervision: Supervision,
    total_children: u64,
    tasks: JoinSet<Result<P::Msg, P::Err>>,
    restarts: u64,
    registry_key: Option<String>,
    registry: Arc<RwLock<HashMap<String, Box<dyn Any + Send + Sync>>>>,
    pubsub: Arc<RwLock<PubSub>>,
    subscription_ids: Vec<(String, u64)>,
}

impl<P> Ctx<P>
where
    P: Actor,
{
    /// Returns a reference to this [`Actor`]'s props. Props are set once at spawn time
    /// and remain immutable for the lifetime of the actor, including across restarts.
    ///
    /// # Example
    /// ```ignore
    /// async fn init(ctx: &mut Ctx<Self>) -> Result<Self, Self::Err> {
    ///     Ok(MyActor { count: ctx.props().initial_count })
    /// }
    /// ```
    pub fn props(&self) -> &P::Props {
        &self.props
    }

    /// Returns a [`Handle`] to the current [`Actor`], allowing it to send messages to itself
    /// or pass its handle to child actors.
    ///
    /// # Example
    /// ```ignore
    /// // schedule a message to self
    /// ctx.this().send_in(Msg::Tick, Duration::from_secs(1));
    /// ```
    pub fn this(&self) -> &Handle<P::Msg> {
        &self.handle
    }

    /// Drains all pending messages from this [`Actor`]'s mailbox. Useful during
    /// restarts to discard stale messages via [`Actor::init`].
    ///
    /// # Example
    /// ```ignore
    /// async fn init(ctx: &mut Ctx<Self>) -> Result<Self, Self::Err> {
    ///     ctx.clear_mailbox();
    ///     Ok(MyActor::default())
    /// }
    /// ```
    pub fn clear_mailbox(&self) {
        self.msg_rx.drain();
    }

    /// Creates a [`SpawnBuilder`] for spawning a child [`Actor`]. The actor type is passed
    /// as a generic parameter and its props as the argument. The child is supervised
    /// by the current actor and will be stopped when the parent stops.
    ///
    /// # Example
    /// ```ignore
    /// let handle = ctx.actor::<Worker>(WorkerProps { id: 1 })
    ///     .supervision(Supervision::Restart {
    ///         max: Limit::Amount(3),
    ///         backoff: Backoff::None,
    ///     })
    ///     .spawn();
    /// ```
    pub fn actor<'a, Child>(&'a mut self, props: Child::Props) -> SpawnBuilder<'a, P, Child>
    where
        Child: Actor,
    {
        SpawnBuilder::new(self, props)
    }

    /// Restarts all child actors immediately, bypassing their supervision strategy.
    /// Each child will re-run its [`Actor::init`] with the same props.
    ///
    /// This is fire-and-forget: it does not wait for children to finish restarting.
    pub fn restart_children(&self) {
        for child in self.children_proc_msg_tx.values() {
            let _ = child.send(ProcMsg::FromParent(ProcAction::Restart));
        }
    }

    /// Stops all child actors and waits for each to fully terminate before returning.
    pub async fn stop_children(&mut self) {
        let mut acks = Vec::with_capacity(self.total_children as usize);
        for child in self.children_proc_msg_tx.values() {
            let (ack_tx, ack_rx) = flume::unbounded();
            let _ = child.send(ProcMsg::FromParent(ProcAction::Stop(ack_tx)));
            acks.push(ack_rx);
        }

        for ack in acks {
            let _ = ack.recv_async().await;
        }

        self.total_children = 0;
        self.children_proc_msg_tx.clear();
    }

    /// Spawns a background async task. On completion, its `Ok` value is delivered
    /// as a message to this [`Actor`]; its `Err` triggers the supervision strategy
    /// that this actor's parent has set for it.
    ///
    /// Tasks are aborted when the actor stops, but **survive restarts**. If the
    /// actor is restarted (via supervision or [`Ctx::restart_children`]), in-flight
    /// tasks from the previous incarnation will continue running and their results
    /// will still be delivered to the restarted actor's `handle()`.
    ///
    /// # Example
    /// ```ignore
    /// ctx.task(async {
    ///     let data = reqwest::get("https://example.com").await?.text().await?;
    ///     Ok(Msg::Fetched(data))
    /// });
    /// ```
    pub fn task<F>(&mut self, f: F)
    where
        F: Future<Output = Result<P::Msg, P::Err>> + Send + 'static,
    {
        self.tasks.spawn(f);
    }

    /// Looks up a registered [`Actor`]'s [`Handle`] by its type. The actor must have been
    /// spawned with [`SpawnBuilder::spawn_registered`].
    ///
    /// # Example
    /// ```ignore
    /// let logger = ctx.get_handle_for::<Logger>()?;
    /// logger.send(LogMsg::Info("hello".into()));
    /// ```
    pub fn get_handle_for<A: Actor>(&self) -> Result<Handle<A::Msg>, RegistryError> {
        let key = std::any::type_name::<A>();
        let reg = self.registry.read().map_err(|_| RegistryError::PoisonErr)?;
        reg.get(key)
            .and_then(|h| h.downcast_ref::<Handle<A::Msg>>())
            .cloned()
            .ok_or_else(|| RegistryError::NotFound(key.to_string()))
    }

    /// Looks up a registered [`Actor`]'s [`Handle`] by name. The actor must have been
    /// spawned with [`SpawnBuilder::spawn_named`].
    ///
    /// # Example
    /// ```ignore
    /// let worker = ctx.get_handle::<WorkerMsg>("worker-1")?;
    /// worker.send(WorkerMsg::Start);
    /// ```
    pub fn get_handle<Msg: Send + 'static>(
        &self,
        name: &str,
    ) -> Result<Handle<Msg>, RegistryError> {
        let reg = self.registry.read().map_err(|_| RegistryError::PoisonErr)?;
        reg.get(name)
            .and_then(|h| h.downcast_ref::<Handle<Msg>>())
            .cloned()
            .ok_or_else(|| RegistryError::NotFound(name.to_string()))
    }

    /// Sends a message to a registered [`Actor`] looked up by type.
    ///
    /// # Example
    /// ```ignore
    /// // Assuming MetricsCollector was spawned with spawn_registered():
    /// // ctx.actor::<MetricsCollector>(props).spawn_registered()?;
    ///
    /// // Any actor in the system can then send to it by type:
    /// ctx.send::<MetricsCollector>(MetricsMsg::RecordLatency(42))?;
    /// ```
    pub fn send<A: Actor>(&self, msg: impl Into<A::Msg>) -> Result<(), RegistryError> {
        let key = std::any::type_name::<A>();
        let reg = self.registry.read().map_err(|_| RegistryError::PoisonErr)?;
        match reg
            .get(key)
            .and_then(|h| h.downcast_ref::<Handle<A::Msg>>())
        {
            Some(handle) => {
                handle.send(msg);
                Ok(())
            }
            None => Err(RegistryError::NotFound(key.to_string())),
        }
    }

    /// Sends a message to a registered [`Actor`] looked up by name.
    ///
    /// # Example
    /// ```ignore
    /// // Assuming a Worker was spawned with spawn_named():
    /// // ctx.actor::<Worker>(props).spawn_named("worker-1")?;
    ///
    /// // Any actor in the system can then send to it by name:
    /// ctx.send_to("worker-1", WorkerMsg::Start)?;
    /// ```
    pub fn send_to<Msg: Send + 'static>(
        &self,
        name: &str,
        msg: impl Into<Msg>,
    ) -> Result<(), RegistryError> {
        let reg = self.registry.read().map_err(|_| RegistryError::PoisonErr)?;
        match reg.get(name).and_then(|h| h.downcast_ref::<Handle<Msg>>()) {
            Some(handle) => {
                handle.send(msg);
                Ok(())
            }
            None => Err(RegistryError::NotFound(name.to_string())),
        }
    }
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug)]
enum ProcMsg {
    /// Sent from child once it terminates
    ChildTerminated {
        child_id: u64,
    },
    FromParent(ProcAction),
    FromHandle(ProcAction),
}

#[derive(Debug)]
enum ProcAction {
    Restart,
    Stop(Sender<()>),
}

fn spawn<Child, W>(mut ctx: Ctx<Child>, delay: Option<Duration>, watch: W)
where
    Child: Actor,
    W: OnErrTerminate<Child::Err>,
{
    tokio::spawn(async move {
        if let Some(d) = delay.filter(|d| !d.is_zero()) {
            time::sleep(d).await;
        }

        // restart is Some whenever we should restart
        let mut restart = Restart::No;
        let mut exit_reason = None;
        let mut actor_created = None;
        let mut stop_ack_tx = None;

        match Child::init(&mut ctx).await {
            Err(e) => {
                exit_reason = Some(ExitReason::Err(e));
                restart = Restart::from_supervision(ctx.supervision, ctx.restarts);
            }

            Ok(mut actor) => match actor.sources(&ctx).await {
                Err(e) => {
                    exit_reason = Some(ExitReason::Err(e));
                    restart = Restart::from_supervision(ctx.supervision, ctx.restarts);
                    actor_created = Some(actor);
                }

                Ok(mut sources) => {
                    macro_rules! on_err {
                        ($e:expr) => {
                            if let Supervision::Resume = ctx.supervision {
                                continue;
                            }

                            restart = Restart::from_supervision(ctx.supervision, ctx.restarts);
                            exit_reason = Some(ExitReason::Err($e));
                            actor_created = Some(actor);
                            break;
                        };
                    }

                    loop {
                        tokio::select! {
                            biased;

                            proc_msg = ctx.proc_msg_rx.recv_async() => {
                                match proc_msg {
                                    Err(_) => break,

                                    Ok(ProcMsg::FromHandle(ProcAction::Stop(tx)) ) => {
                                        exit_reason = Some(ExitReason::Handle);
                                        stop_ack_tx = Some(tx);
                                        break
                                    },

                                    Ok(ProcMsg::FromParent(ProcAction::Stop(tx))) => {
                                        exit_reason = exit_reason.or(Some(ExitReason::Parent));
                                        stop_ack_tx = Some(tx);
                                        break
                                    },

                                    Ok(ProcMsg::FromParent(ProcAction::Restart)) => {
                                        exit_reason = exit_reason.or(Some(ExitReason::Parent));
                                        restart = Restart::In(Duration::ZERO);
                                        break;
                                    }


                                    Ok(ProcMsg::FromHandle(ProcAction::Restart)) => {
                                        exit_reason = exit_reason.or(Some(ExitReason::Handle));
                                        restart = Restart::In(Duration::ZERO);
                                        break;
                                    }

                                    Ok(ProcMsg::ChildTerminated { child_id, }) => {
                                        if ctx.children_proc_msg_tx.remove(&child_id).is_some() {
                                            ctx.total_children -= 1;
                                        }
                                    }
                                }
                            }

                            recvd = ctx.msg_rx.recv_async() => {
                                match recvd {
                                    Err(_) => break,

                                    Ok(msg) => {
                                        if let Err(e) = actor.handle(msg, &mut ctx).await {
                                            on_err!(e);
                                        };
                                    }
                                }
                            }

                            Some(Ok(msg)) = ctx.tasks.join_next() => {
                                match msg {
                                    Err(e) => {
                                        on_err!(e);
                                    }

                                    Ok(msg) => {
                                        if let Err(e) = actor.handle(msg, &mut ctx).await {
                                            on_err!(e);
                                        };
                                    }
                                }

                            }

                            Some(msg) = std::future::poll_fn(|cx| Pin::new(&mut sources).poll_next(cx)) => {
                                if let Err(e) = actor.handle(msg, &mut ctx).await {
                                    on_err!(e);
                                };
                            }
                        }
                    }
                }
            },
        }

        ctx.stop_children().await;
        let exit_reason = exit_reason.unwrap_or(ExitReason::Handle);

        if let ExitReason::Err(_) = &exit_reason {
            ctx.restarts += 1;
        }

        if let (Restart::No, ExitReason::Err(ref e)) = (&restart, &exit_reason) {
            watch.on_err_terminate(e);
        }

        Child::exit(actor_created, exit_reason, &mut ctx).await;

        // Clean up pub/sub subscriptions (runs on both stop and restart)
        if !ctx.subscription_ids.is_empty() {
            if let Ok(mut bus) = ctx.pubsub.write() {
                for (topic, sub_id) in ctx.subscription_ids.drain(..) {
                    if let Some(entry) = bus.topics.get_mut(&topic) {
                        entry.subscribers.retain(|s| s.id != sub_id);
                        if entry.subscribers.is_empty() {
                            bus.topics.remove(&topic);
                        }
                    }
                }
            }
        }

        let _ = stop_ack_tx.map(|tx| tx.send(()));

        if let Restart::In(duration) = restart {
            spawn::<Child, W>(ctx, Some(duration), watch)
        } else if let Some(parent_tx) = ctx.parent_proc_msg_tx {
            if let Some(key) = ctx.registry_key.take() {
                if let Ok(mut reg) = ctx.registry.write() {
                    reg.remove(&key);
                }
            }

            let _ = parent_tx.send(ProcMsg::ChildTerminated { child_id: ctx.id });
        }
    });
}

/// Defines how a parent reacts when a child actor fails.
///
/// # Example
/// ```ignore
/// let supervision = Supervision::Restart {
///     max: Limit::Amount(5),
///     backoff: Backoff::Static(Duration::from_secs(1)),
/// };
/// ```
#[derive(Debug, Clone, Copy)]
pub enum Supervision {
    /// Actor terminates on error.
    Stop,
    /// Actor continues processing the next message after an error.
    Resume,
    /// Actor is restarted on error, up to `max` times with optional `backoff`.
    Restart { max: Limit, backoff: Backoff },
}

/// Delay strategy between restart attempts.
///
/// # Example
/// ```ignore
/// let backoff = Backoff::Incremental {
///     min: Duration::from_millis(100),
///     max: Duration::from_secs(5),
///     step: Duration::from_millis(500),
/// };
/// ```
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Backoff {
    /// Restart immediately with no delay.
    None,
    /// Wait a fixed duration between restarts.
    Static(Duration),
    /// Linearly increase delay from `min` to `max` by `step` per restart.
    Incremental {
        min: Duration,
        max: Duration,
        step: Duration,
    },
}

/// Maximum number of restarts allowed.
///
/// # Example
/// ```ignore
/// let limit = Limit::Amount(3);
/// ```
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Limit {
    /// No limit on restarts.
    None,
    /// Restart at most this many times.
    Amount(u64),
}

/// **Note**: `0` maps to [`Limit::None`] (unlimited), not zero restarts.
/// If you want zero restarts (i.e., never restart), use [`Supervision::Stop`] instead.
impl From<u64> for Limit {
    fn from(value: u64) -> Self {
        match value {
            0 => Limit::None,
            v => Limit::Amount(v),
        }
    }
}

impl PartialEq<u64> for Limit {
    fn eq(&self, other: &u64) -> bool {
        match self {
            Limit::None => false,
            Limit::Amount(n) => n == other,
        }
    }
}

#[derive(Debug, Clone)]
pub enum RegistryError {
    NameTaken(String),
    NotFound(String),
    PoisonErr,
}

impl std::fmt::Display for RegistryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RegistryError::NameTaken(name) => write!(f, "registry name already taken: {name}"),
            RegistryError::NotFound(name) => write!(f, "no actor registered under: {name}"),
            RegistryError::PoisonErr => write!(f, "registry lock poisoned"),
        }
    }
}

impl std::error::Error for RegistryError {}

/// Builder for configuring and spawning a child [`Actor`]. Created via [`Ctx::actor`].
pub struct SpawnBuilder<'a, Parent, Child, W = NoWatch>
where
    Parent: Actor,
    Child: Actor,
{
    ctx: &'a mut Ctx<Parent>,
    props: Child::Props,
    supervision: Supervision,
    /// Only kicks in if child is stopped or reaches maximum number of restarts.
    watch: W,
    registry_key: Option<String>,
}

impl<'a, Parent, Child> SpawnBuilder<'a, Parent, Child, NoWatch>
where
    Parent: Actor,
    Child: Actor,
{
    fn new(ctx: &'a mut Ctx<Parent>, props: Child::Props) -> Self {
        Self {
            ctx,
            props,
            supervision: Supervision::Restart {
                max: Limit::None,
                backoff: Backoff::None,
            },
            watch: NoWatch,
            registry_key: None,
        }
    }
}

impl<'a, Parent, Child, W> SpawnBuilder<'a, Parent, Child, W>
where
    Parent: Actor,
    Child: Actor,
    W: OnErrTerminate<Child::Err>,
{
    /// Sets the [`Supervision`] strategy the parent will use for this child.
    /// Defaults to [`Supervision::Restart`] with unlimited restarts and no backoff.
    ///
    /// # Example
    /// ```ignore
    /// ctx.actor::<Worker>(props)
    ///     .supervision(Supervision::Restart {
    ///         max: Limit::Amount(3),
    ///         backoff: Backoff::Static(Duration::from_secs(1)),
    ///     })
    ///     .spawn();
    /// ```
    pub fn supervision(mut self, supervision: Supervision) -> Self {
        self.supervision = supervision;
        self
    }

    /// Registers a callback that fires when the child terminates due to an error.
    /// This happens when the supervision strategy is [`Supervision::Stop`], or when
    /// [`Supervision::Restart`] has exhausted all allowed restarts. The callback maps
    /// the child's error into a message for the parent.
    ///
    /// # Example
    /// ```ignore
    /// ctx.actor::<Worker>(props)
    ///     .supervision(Supervision::Restart {
    ///         max: Limit::Amount(3),
    ///         backoff: Backoff::None,
    ///     })
    ///     .watch(|err| ParentMsg::WorkerDied(format!("{err:?}")))
    ///     .spawn();
    /// ```
    pub fn watch<F>(self, f: F) -> SpawnBuilder<'a, Parent, Child, WatchFn<F, Parent::Msg>>
    where
        F: Fn(&Child::Err) -> Parent::Msg + Send + 'static,
    {
        let parent_msg_tx = self.ctx.handle.msg_tx.clone();
        SpawnBuilder {
            ctx: self.ctx,
            props: self.props,
            supervision: self.supervision,
            watch: WatchFn { f, parent_msg_tx },
            registry_key: self.registry_key,
        }
    }

    /// Spawns the child [`Actor`] and returns a [`Handle`] to it.
    pub fn spawn(self) -> Handle<Child::Msg> {
        let (msg_tx, msg_rx) = flume::unbounded(); // child
        let (proc_msg_tx, proc_msg_rx) = flume::unbounded(); // child

        let handle = Handle {
            msg_tx,
            proc_msg_tx,
        };

        self.ctx.total_children += 1;
        let id = self.ctx.total_children;

        let ctx: Ctx<Child> = Ctx {
            id,
            props: self.props,
            handle: handle.clone(),
            msg_rx,
            parent_proc_msg_tx: Some(self.ctx.handle.proc_msg_tx.clone()),
            proc_msg_rx,
            children_proc_msg_tx: HashMap::new(),
            total_children: 0,
            supervision: self.supervision,
            restarts: 0,
            tasks: JoinSet::new(),
            registry_key: self.registry_key,
            registry: self.ctx.registry.clone(),
            pubsub: self.ctx.pubsub.clone(),
            subscription_ids: Vec::new(),
        };

        spawn::<Child, W>(ctx, None, self.watch);

        self.ctx
            .children_proc_msg_tx
            .insert(self.ctx.total_children, handle.proc_msg_tx.clone());

        handle
    }

    /// Spawns the child and registers it in the global registry under its type name.
    /// Other actors can then look it up via [`Ctx::get_handle_for`] or [`Ctx::send`].
    /// Returns [`RegistryError::NameTaken`] if already registered.
    pub fn spawn_registered(self) -> Result<Handle<Child::Msg>, RegistryError> {
        let key = std::any::type_name::<Child>();
        self.spawn_named(key)
    }

    /// Spawns the child and registers it in the global registry under the given name.
    /// Other actors can then look it up via [`Ctx::get_handle`] or [`Ctx::send_to`].
    /// Returns [`RegistryError::NameTaken`] if the name is already taken.
    ///
    /// # Example
    /// ```ignore
    /// let h = ctx.actor::<Worker>(props).spawn_named("worker-1")?;
    /// ```
    pub fn spawn_named(
        mut self,
        name: impl Into<String>,
    ) -> Result<Handle<Child::Msg>, RegistryError> {
        let name = name.into();
        let registry = self.ctx.registry.clone();
        let mut reg = registry.write().map_err(|_| RegistryError::PoisonErr)?;

        if reg.contains_key(&name) {
            return Err(RegistryError::NameTaken(name.clone()));
        }

        self.registry_key = Some(name.clone());
        let handle = self.spawn();
        reg.insert(name, Box::new(handle.clone()));

        Ok(handle)
    }
}

#[derive(Debug)]
enum Restart {
    No,
    In(Duration),
}

impl Restart {
    fn from_supervision(supervision: Supervision, current_restarts: u64) -> Self {
        match supervision {
            Supervision::Stop => Restart::No,
            Supervision::Resume => Restart::No,
            Supervision::Restart { max, .. } if max == current_restarts + 1 => Restart::No,
            Supervision::Restart { backoff, .. } => {
                let wait = match backoff {
                    Backoff::None => Duration::ZERO,
                    Backoff::Static(duration) => duration,
                    Backoff::Incremental { min, max, step } => {
                        let wait = step.mul_f64((current_restarts + 1) as f64);
                        let wait = cmp::min(max, wait);
                        cmp::max(min, wait)
                    }
                };

                Restart::In(wait)
            }
        }
    }
}