kameo 0.22.2

Fault-tolerant Async Actors Built on Tokio
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
use std::{
    any::Any,
    collections::HashMap,
    fmt, mem,
    ops::{self, ControlFlow},
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    time::{Duration, Instant},
};

use futures::{
    FutureExt, StreamExt,
    future::{BoxFuture, join_all},
    stream::FuturesUnordered,
};
use tokio::sync::Mutex;

use crate::{
    actor::{Actor, ActorId},
    error::ActorStopReason,
    mailbox::{MailboxReceiver, SignalMailbox},
    supervision::RestartPolicy,
};

pub type BoxMailboxReceiver = Box<dyn Any + Send>;
pub type BoxActorRef = Box<dyn Any + Send>;
pub type SpawnFactory =
    Box<dyn Fn(BoxMailboxReceiver) -> BoxFuture<'static, BoxActorRef> + Send + Sync>;
pub type ShutdownFn = Box<dyn Fn() -> BoxFuture<'static, ()> + Send + Sync>;

/// A collection of links to other actors that are notified when the actor dies.
///
/// Links are used for parent-child or sibling relationships, allowing actors to observe each other's lifecycle.
#[derive(Clone, Default)]
#[allow(missing_debug_implementations)]
pub struct Links(Arc<Mutex<LinksInner>>);

impl Links {
    /// Creates links for a supervised child, pre-populated with the restart configuration its
    /// supervisor shares with it: the `restart_policy`, the shared `restart_tracker` (so the child
    /// can predict its own restarts in [`LinksInner::will_restart`]), and the shared
    /// `parent_shutdown` flag. Building the inner value up front avoids locking it right after
    /// construction.
    pub fn supervised(
        restart_policy: RestartPolicy,
        restart_tracker: Arc<RestartTracker>,
        parent_shutdown: Arc<AtomicBool>,
    ) -> Self {
        Links(Arc::new(Mutex::new(LinksInner {
            restart_policy: Some(restart_policy),
            restart_tracker: Some(restart_tracker),
            parent_shutdown,
            ..Default::default()
        })))
    }

    /// Sets the `parent_shutdown` flag on all supervised children.
    ///
    /// This must be called before the parent stops processing its mailbox so that any child
    /// exiting independently after this point will drop its `mailbox_rx` immediately in
    /// `notify_links`, preventing it from being queued in the parent's mailbox where it would
    /// deadlock the `shutdown_children` wait.
    pub async fn set_children_parent_shutdown(&self) {
        let inner = self.lock().await;
        for spec in inner.children.values() {
            spec.parent_shutdown.store(true, Ordering::Release);
        }
    }

    pub async fn send_children_shutdown(&self) {
        let shutdowns: Vec<_> = {
            let inner = self.lock().await;
            inner
                .children
                .values()
                .map(|c| c.shutdown.clone())
                .collect()
        };

        join_all(shutdowns.iter().map(|s| s())).await;
    }

    pub async fn wait_children_closed(&self) {
        let mailboxes: Vec<_> = {
            let inner = self.lock().await;
            inner
                .children
                .values()
                .map(|c| c.signal_mailbox.clone())
                .collect()
        };

        join_all(mailboxes.iter().map(|m| m.closed())).await;
    }
}

#[derive(Default)]
pub struct LinksInner {
    /// Parent actor supervising us.
    pub parent: Option<(ActorId, Link)>,
    /// Sibbling linked actors.
    pub sibblings: HashMap<ActorId, Link>,
    /// Child actors we are supervising.
    pub children: HashMap<ActorId, ErasedChildSpec>,
    /// Set by the parent supervisor (via `ErasedChildSpec`) before sending `SupervisorRestart`
    /// during final shutdown. When `true`, `notify_links` drops `mailbox_rx` immediately
    /// instead of queuing it in the parent's mailbox, which would deadlock the parent's
    /// `shutdown_children` wait.
    pub parent_shutdown: Arc<AtomicBool>,
    /// The restart policy our supervisor will apply to us, `Some` only when supervised. Lets the
    /// actor determine, during its own teardown, whether it will be restarted (see
    /// [`Self::will_restart`]).
    pub restart_policy: Option<RestartPolicy>,
    /// The restart intensity bookkeeping, shared by `Arc` with our supervisor's [`ErasedChildSpec`].
    /// `Some` only when supervised. Lets [`Self::will_restart`] apply the same restart-limit check
    /// the supervisor uses, instead of guessing.
    pub restart_tracker: Option<Arc<RestartTracker>>,
}

impl LinksInner {
    /// Determines, during the actor's own teardown, whether its supervisor will restart it.
    /// Mirrors [`ErasedChildSpec::should_restart`], reading the same shared [`RestartTracker`] so
    /// the restart-limit check matches the supervisor's decision. `run_actor_lifecycle` uses this
    /// both to route a dropped `ask`'s message (`ActorRestarting` vs `ActorNotRunning`) and to
    /// decide whether leftover tells go to `Actor::on_undelivered`, so it must not report a restart
    /// that won't happen.
    ///
    /// The child reads the tracker slightly before the supervisor decides. Because the restart
    /// count only changes on the supervisor's restart path (which hasn't run yet for this death)
    /// and `now - last_restart` is monotonic, the only possible disagreement is at a restart-window
    /// boundary, where the child predicts no restart but the supervisor restarts: the leftover
    /// tells reach `on_undelivered` instead of the new incarnation. No message is dropped either
    /// way. The opposite (predict restart, supervisor declines) cannot occur.
    pub fn will_restart(&self, reason: &ActorStopReason) -> bool {
        let Some(policy) = self.restart_policy else {
            return false; // unsupervised: nobody will restart us
        };
        if self.parent_shutdown.load(Ordering::Acquire) {
            return false; // our supervisor is going away too
        }
        match policy {
            RestartPolicy::Never => false,
            // a coordinator-initiated restart bypasses the intensity check, same as the supervisor
            _ if matches!(reason, ActorStopReason::SupervisorRestart) => true,
            RestartPolicy::Permanent => self.restart_intensity_allows(),
            RestartPolicy::Transient => !reason.is_normal() && self.restart_intensity_allows(),
        }
    }

    /// Whether the shared restart budget still permits a restart. Falls back to `true`
    /// if no tracker is set, which shouldn't happen while supervised.
    fn restart_intensity_allows(&self) -> bool {
        self.restart_tracker
            .as_ref()
            .is_none_or(|tracker| tracker.predict_restart())
    }

    /// Notify parent or sibblings, depending on supervision status.
    pub fn notify_links<A: Actor>(
        &mut self,
        id: ActorId,
        reason: ActorStopReason,
        mailbox_rx: MailboxReceiver<A>,
    ) {
        match self.parent.clone() {
            Some((parent_id, parent_link)) => {
                let sibblings = mem::take(&mut self.sibblings);
                if self.parent_shutdown.load(Ordering::Acquire) {
                    // Parent is in shutdown_children — drop mailbox_rx immediately so the
                    // child's channel closes and the parent's mailbox.closed() wait resolves.
                    // Passing it to the parent's queue would deadlock since the parent is not
                    // processing its mailbox while blocked in shutdown_children.
                    tokio::spawn(parent_link.notify(parent_id, id, reason, None, None));
                } else {
                    // Supervised normal path — pass mailbox_rx so the parent can restart us.
                    tokio::spawn(parent_link.notify(
                        parent_id,
                        id,
                        reason,
                        Some(Box::new(mailbox_rx)),
                        Some(sibblings),
                    ));
                }
            }
            None => {
                // Unsupervised, notify sibblings directly
                self.notify_sibblings(id, &reason);
            }
        }
    }

    /// Notify sibbling links.
    pub fn notify_sibblings(&mut self, id: ActorId, reason: &ActorStopReason) {
        let mut notify_futs: FuturesUnordered<_> = self
            .sibblings
            .drain()
            .map(|(sibbling_actor_id, link)| {
                link.notify(sibbling_actor_id, id, reason.clone(), None, None)
                    .boxed()
            })
            .collect();
        tokio::spawn(async move { while let Some(()) = notify_futs.next().await {} });
    }
}

impl ops::Deref for Links {
    type Target = Mutex<LinksInner>;

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

#[derive(Clone)]
#[allow(missing_debug_implementations)]
pub enum Link {
    Local(Box<dyn SignalMailbox>),
    #[cfg(feature = "remote")]
    Remote(std::borrow::Cow<'static, str>),
}

impl Link {
    #[cfg_attr(not(feature = "remote"), allow(unused_variables))]
    pub async fn notify(
        self,
        link_actor_id: ActorId,
        dead_actor_id: ActorId,
        reason: ActorStopReason,
        mailbox_rx: Option<BoxMailboxReceiver>,
        dead_actor_sibblings: Option<HashMap<ActorId, Link>>,
    ) {
        match self {
            Link::Local(mailbox) => {
                #[cfg_attr(not(feature = "tracing"), allow(unused_variables))]
                if let Err(err) = mailbox
                    .signal_link_died(dead_actor_id, reason, mailbox_rx, dead_actor_sibblings)
                    .await
                {
                    #[cfg(feature = "tracing")]
                    match err {
                        crate::error::SendError::ActorNotRunning(_) => {}
                        _ => {
                            tracing::error!("failed to notify actor a link died: {err}");
                        }
                    }
                }
            }
            #[cfg(feature = "remote")]
            Link::Remote(notified_actor_remote_id) => {
                if let Some(swarm) = crate::remote::ActorSwarm::get() {
                    let res = swarm
                        .signal_link_died(
                            dead_actor_id,
                            link_actor_id,
                            notified_actor_remote_id,
                            reason,
                        )
                        .await;
                    #[cfg_attr(not(feature = "tracing"), allow(unused_variables))]
                    if let Err(err) = res {
                        #[cfg(feature = "tracing")]
                        tracing::error!("failed to notify actor a link died: {err}");
                    }
                }
            }
        }
    }
}

#[derive(Clone)]
pub struct ErasedChildSpec {
    pub factory: Arc<SpawnFactory>,
    pub shutdown: Arc<ShutdownFn>,
    pub signal_mailbox: Box<dyn SignalMailbox>,
    /// Shared with the child's `LinksInner::parent_shutdown`. Set to `true` by
    /// `shutdown_children` before sending `SupervisorRestart`, so the child knows to drop
    /// its `mailbox_rx` immediately rather than forwarding it to the parent's queue.
    pub parent_shutdown: Arc<AtomicBool>,
    pub restart_policy: RestartPolicy,
    /// Restart intensity bookkeeping, shared by `Arc` with the child's
    /// [`LinksInner::restart_tracker`] so the child can predict this same decision.
    pub restart_tracker: Arc<RestartTracker>,
}

impl ErasedChildSpec {
    pub fn should_restart(&self, reason: &ActorStopReason) -> ControlFlow<NoRestartReason> {
        // Never policy takes precedence over everything, including coordinator-initiated restarts
        if matches!(self.restart_policy, RestartPolicy::Never) {
            return ControlFlow::Break(NoRestartReason::NeverPolicy);
        }

        // Always restart if supervisor initiated the shutdown for coordination
        if matches!(reason, ActorStopReason::SupervisorRestart) {
            return ControlFlow::Continue(());
        }

        // Policy check
        match self.restart_policy {
            RestartPolicy::Permanent => {}
            RestartPolicy::Transient if reason.is_normal() => {
                return ControlFlow::Break(NoRestartReason::NormalExitUnderTransientPolicy);
            }
            RestartPolicy::Transient => {}
            RestartPolicy::Never => unreachable!(),
        }

        self.restart_tracker.record_restart()
    }
}

/// Restart intensity bookkeeping for a supervised child, shared by `Arc` between the supervisor's
/// [`ErasedChildSpec`] and the child's [`LinksInner`]. The supervisor mutates it authoritatively
/// via [`Self::record_restart`]; the child reads it via [`Self::predict_restart`] to decide, during
/// its own teardown, whether it is about to be restarted.
#[derive(Debug)]
pub struct RestartTracker {
    max_restarts: u32,
    restart_window: Duration,
    // std Mutex: only ever locked for the short, non-async body of the methods below, and
    // independent of the async `Links` mutexes, so it can't deadlock against them.
    state: std::sync::Mutex<RestartTrackerState>,
}

#[derive(Debug)]
struct RestartTrackerState {
    restart_count: u32,
    last_restart: Instant,
}

impl RestartTracker {
    pub fn new(max_restarts: u32, restart_window: Duration) -> Self {
        RestartTracker {
            max_restarts,
            restart_window,
            state: std::sync::Mutex::new(RestartTrackerState {
                restart_count: 0,
                last_restart: Instant::now(),
            }),
        }
    }

    /// Authoritative check made by the supervisor. Applies the restart-window reset and intensity
    /// limit, recording the restart when it is allowed.
    pub fn record_restart(&self) -> ControlFlow<NoRestartReason> {
        let mut state = self.state.lock().unwrap();

        // Reset count if outside time window
        let now = Instant::now();
        if now.duration_since(state.last_restart) > self.restart_window {
            state.restart_count = 0;
        }

        // Intensity check
        if state.restart_count >= self.max_restarts {
            return ControlFlow::Break(NoRestartReason::MaxRestartsExceeded {
                restart_count: state.restart_count,
                max_restarts: self.max_restarts,
            });
        }

        state.restart_count += 1;
        state.last_restart = now;

        ControlFlow::Continue(())
    }

    /// Read-only mirror of [`Self::record_restart`]'s intensity decision, used by the child to
    /// predict whether the supervisor will restart it. Does not mutate the count.
    pub fn predict_restart(&self) -> bool {
        let state = self.state.lock().unwrap();
        let count = if Instant::now().duration_since(state.last_restart) > self.restart_window {
            0
        } else {
            state.restart_count
        };
        count < self.max_restarts
    }

    #[cfg(feature = "console")]
    pub fn max_restarts(&self) -> u32 {
        self.max_restarts
    }

    #[cfg(feature = "console")]
    pub fn restart_window(&self) -> Duration {
        self.restart_window
    }

    #[cfg(any(feature = "tracing", feature = "console"))]
    pub fn current_count(&self) -> u32 {
        self.state.lock().unwrap().restart_count
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NoRestartReason {
    /// Transient policy but stop was normal
    NormalExitUnderTransientPolicy,
    /// Restart intensity threshold exceeded
    MaxRestartsExceeded {
        restart_count: u32,
        max_restarts: u32,
    },
    /// Never policy — this child is configured to never restart
    NeverPolicy,
}

impl fmt::Display for NoRestartReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            NoRestartReason::NormalExitUnderTransientPolicy => {
                write!(f, "normal exit under transient policy")
            }
            NoRestartReason::MaxRestartsExceeded {
                restart_count,
                max_restarts,
            } => {
                write!(
                    f,
                    "max restarts exceeded ({restart_count} >= {max_restarts})"
                )
            }
            NoRestartReason::NeverPolicy => write!(f, "never restart policy"),
        }
    }
}