beam/actor.rs
1#![allow(clippy::mutable_key_type)] // Addr hashes by id field, not interior-mutable sender
2
3//! Actor framework — a lightweight actor model built on [`Mailbox`].
4//!
5//! This module provides a minimal actor system where actors communicate via
6//! [`Arc<Message>`] over pre-allocated ring-buffer mailboxes. The mailbox
7//! replaces tokio's `mpsc` channels, eliminating per-message allocation and
8//! reducing scheduler overhead through batch draining.
9//!
10//! # Architecture
11//!
12//! - [`Actor`] trait — defines the message handling interface
13//! - [`ActorContext`] — per-actor context with peer ID, router address, and
14//! child actor management
15//! - [`Addr`] — a clonable, hashable address for sending messages to an actor
16//!
17//! # Mailbox
18//!
19//! All actors use [`crate::mailbox::mailbox`] — a bounded `VecDeque` protected
20//! by `parking_lot::Mutex` with `tokio::sync::Notify` for wakeups. Messages
21//! are wrapped in [`Arc<Message>`] so fanout is a refcount bump (~2 ns),
22//! not a deep clone.
23//!
24//! - **Default capacity** (65536) — [`ActorContext::start_actor`] creates an
25//! actor with a generous mailbox. Backpressure only under extreme load.
26//! - **Bounded** — [`ActorContext::start_actor_bounded`] creates an actor with
27//! a smaller capacity. When full, [`Addr::send`] returns `Err(())`,
28//! applying backpressure. Used for storage write actors.
29//!
30//! # Message Flow
31//!
32//! ```text
33//! Sender → Addr.send(msg) → Mailbox (bounded VecDeque<Arc<Message>>)
34//! ↓
35//! Actor.handle(Arc<Message>, ctx)
36//! ↓
37//! Actor can:
38//! - spawn child actors
39//! - send to router
40//! - spawn child tasks
41//! ```
42//!
43//! # `Arc<Message>` Semantics
44//!
45//! [`Addr::send`] accepts `impl Into<Arc<Message>>`, so existing call sites
46//! that pass `Message::Put(put)` still compile — the message is wrapped in
47//! `Arc` inside `send`. For fanout paths (e.g. relay), callers can pass
48//! `Arc::clone(&msg)` to skip the allocation entirely — all subscribers
49//! share the same `Arc`.
50//!
51//! # Shutdown
52//!
53//! Actors are stopped via a stop signal channel. When the context's `stop()`
54//! method is called, all child tasks are aborted and stop signals are sent
55//! to all child actors.
56
57use crate::Node;
58use crate::mailbox::{self, MailboxReceiver, MailboxSender};
59use crate::message::Message;
60use crate::metrics::Metrics;
61use crate::tokio_spawn::JoinHandle;
62use crate::utils::random_string;
63use async_trait::async_trait;
64use futures_util::Future;
65use parking_lot::RwLock;
66use std::collections::HashMap;
67use std::fmt;
68use std::hash::{Hash, Hasher};
69use std::marker::Send;
70use std::sync::Arc;
71use tokio::sync::mpsc::{Receiver, Sender, channel};
72use tokio::sync::watch;
73
74/// Default mailbox capacity for unbounded actors.
75const DEFAULT_MAILBOX_CAPACITY: usize = 65536;
76
77/// The core actor trait.
78///
79/// Implementors define how to handle [`Message`] values and optionally
80/// configure lifecycle hooks (`pre_start`, `stopping`).
81///
82/// # Lifecycle
83///
84/// 1. `pre_start` — called once before the actor begins processing messages
85/// 2. `handle` — called for each message received
86/// 3. `stopping` — called once after the actor's message loop exits
87///
88/// # Example
89///
90/// ```no_run
91/// use beam::actor::{Actor, ActorContext};
92/// use beam::message::Message;
93/// use async_trait::async_trait;
94/// use std::sync::Arc;
95///
96/// struct EchoActor;
97///
98/// #[async_trait]
99/// impl Actor for EchoActor {
100/// async fn handle(&mut self, msg: Arc<Message>, _ctx: &ActorContext) {
101/// // Process message — &*msg gives &Message
102/// }
103/// }
104/// ```
105#[async_trait]
106pub trait Actor: Send + Sync + 'static {
107 /// Handle an incoming message.
108 ///
109 /// Messages are wrapped in [`Arc<Message>`] so that fanout paths
110 /// (e.g. relay) can share a single allocation across all subscribers.
111 /// Use `&*msg` or `msg.as_ref()` to access the inner [`Message`].
112 async fn handle(&mut self, message: Arc<Message>, context: &ActorContext);
113
114 /// Handle a batch of messages drained from the mailbox.
115 ///
116 /// Override to process multiple messages in a single call, enabling
117 /// batch optimizations like coalescing WebSocket writes. The default
118 /// implementation calls [`handle`](Actor::handle) for each message.
119 ///
120 /// Implementors that override this **must** drain all messages from
121 /// `batch` (e.g. via `batch.drain(..)` or `batch.clear()`).
122 async fn handle_batch(&mut self, batch: &mut Vec<Arc<Message>>, context: &ActorContext) {
123 for msg in batch.drain(..) {
124 self.handle(msg, context).await;
125 }
126 }
127
128 /// Called once before the actor starts processing messages.
129 ///
130 /// Override to initialize state, spawn child actors, or establish
131 /// connections. Defaults to a no-op.
132 async fn pre_start(&mut self, _context: &ActorContext) {}
133
134 /// Called once after the actor's message loop exits.
135 ///
136 /// Override for cleanup logic. Defaults to a no-op.
137 async fn stopping(&mut self, _context: &ActorContext) {}
138
139 /// Whether this actor wants to receive all messages (not just addressed
140 /// to it). Used by the Multicast adapter.
141 ///
142 /// Defaults to `false`.
143 fn subscribe_to_everything(&self) -> bool {
144 false
145 }
146
147 /// Attempts to produce a clone of this actor for storage read/write
148 /// splitting.
149 ///
150 /// Storage adapters override this to return a boxed clone, enabling the
151 /// [`crate::router::Router`] to start separate read and write actors that
152 /// share the same underlying database. Non-storage actors return `None`
153 /// (the default).
154 ///
155 /// When the Router receives `Some`, it starts two actors: one registered
156 /// in `read_adapters` (receives only `Get`), one in `write_adapters`
157 /// (receives `Put`, `BatchPut`, `Flush`). Both share the same underlying
158 /// data store via `Arc`, so reads see committed writes immediately.
159 fn try_clone_storage(&self) -> Option<Box<dyn Actor>> {
160 None
161 }
162}
163
164impl dyn Actor {
165 /// Internal run loop — receives messages until the stop signal fires
166 /// or the mailbox is closed.
167 ///
168 /// Uses [`MailboxReceiver::recv_batch`] to drain up to 64 messages per
169 /// wakeup, amortizing scheduler overhead across batches. This is the
170 /// key performance difference from tokio's `mpsc::recv` which wakes
171 /// per message.
172 async fn run(
173 &mut self,
174 mut receiver: MailboxReceiver,
175 mut stop_receiver: Receiver<()>,
176 context: ActorContext,
177 ) {
178 self.pre_start(&context).await;
179 let mut batch: Vec<Arc<Message>> = Vec::with_capacity(64);
180 loop {
181 tokio::select! {
182 _v = stop_receiver.recv() => {
183 context.stop();
184 break;
185 },
186 count = receiver.recv_batch(&mut batch, 64) => {
187 if count == 0 {
188 // Mailbox closed.
189 break;
190 }
191 self.handle_batch(&mut batch, &context).await;
192 }
193 }
194 }
195 self.stopping(&context).await;
196 }
197}
198
199/// Per-actor context providing access to runtime services.
200///
201/// Each actor receives an `ActorContext` in `pre_start`, `handle`, and
202/// `stopping`. The context is clonable — clones share the same underlying
203/// state via `Arc`.
204///
205/// # Key Fields
206///
207/// - `peer_id` — this node's peer identifier (shared across all actors)
208/// - `router` — the router actor's address (for forwarding messages)
209/// - `addr` — this actor's own address
210/// - `node` — optional owned [`Node`] (set for the root actor)
211#[derive(Clone)]
212pub struct ActorContext {
213 /// This node's peer ID, shared across all actors.
214 pub peer_id: Arc<RwLock<String>>,
215 /// The router actor's address for message forwarding.
216 /// `Arc<RwLock<…>>` so it can be set after construction (the root node's
217 /// router is created after the node itself).
218 pub router: Arc<RwLock<Addr>>,
219 /// Stop signals for child actors (keyed by child Addr).
220 stop_signals: Arc<RwLock<HashMap<Addr, Sender<()>>>>,
221 /// Join handles for spawned child tasks.
222 task_handles: Arc<RwLock<Vec<JoinHandle<()>>>>,
223 /// This actor's own address.
224 pub addr: Addr,
225 /// Whether this actor has been stopped.
226 pub is_stopped: Arc<RwLock<bool>>,
227 /// Shutdown signal receiver — set to `true` when graceful shutdown begins.
228 /// Long-running child tasks select on this to break their loops.
229 pub shutdown_rx: watch::Receiver<bool>,
230 /// Optional owned Node (set for the root actor).
231 pub node: Arc<RwLock<Option<Node>>>,
232 /// Shared metrics handle — lock-free counters for the relay hot path.
233 ///
234 /// Cloned from the root Node's `Metrics` so all actors in the tree
235 /// observe the same atomic counters. Set in `Node::new_with_config`
236 /// and propagated through `child_context`.
237 pub metrics: Arc<Metrics>,
238}
239
240impl ActorContext {
241 /// Creates a new `ActorContext` with the given peer ID.
242 ///
243 /// The `addr` and `router` fields are initialized to [`Addr::noop()`]
244 /// and should be set before use.
245 pub fn new(peer_id: String) -> Self {
246 Self {
247 addr: Addr::noop(),
248 stop_signals: Arc::new(RwLock::new(HashMap::new())),
249 task_handles: Arc::new(RwLock::new(Vec::new())),
250 peer_id: Arc::new(RwLock::new(peer_id)),
251 router: Arc::new(RwLock::new(Addr::noop())),
252 is_stopped: Arc::new(RwLock::new(false)),
253 shutdown_rx: watch::channel(false).1,
254 node: Arc::new(RwLock::new(None)),
255 metrics: Arc::new(Metrics::new()),
256 }
257 }
258
259 /// Returns the number of child actors spawned by this context.
260 pub fn child_actor_count(&self) -> usize {
261 self.stop_signals.read().len()
262 }
263
264 /// Creates a child context with the given address and stop signal.
265 fn child_context(&self, addr: Addr, stop_signal: Sender<()>) -> Self {
266 let mut stop_signals = HashMap::new();
267 stop_signals.insert(addr.clone(), stop_signal);
268 Self {
269 addr,
270 stop_signals: Arc::new(RwLock::new(stop_signals)),
271 task_handles: Arc::new(RwLock::new(Vec::new())),
272 peer_id: self.peer_id.clone(),
273 router: self.router.clone(),
274 is_stopped: self.is_stopped.clone(),
275 shutdown_rx: self.shutdown_rx.clone(),
276 node: self.node.clone(),
277 metrics: self.metrics.clone(),
278 }
279 }
280
281 /// Spawns a child actor with an unbounded channel and returns its address.
282 ///
283 /// The actor runs in a tokio task. Its lifecycle is managed by this
284 /// context — calling `stop()` will send a stop signal and abort the task.
285 pub fn start_actor(&self, actor: Box<dyn Actor>) -> Addr {
286 self.start_actor_or_router(actor, false, None)
287 }
288
289 /// Spawns a child actor with a bounded channel and returns its address.
290 ///
291 /// The `bound` parameter sets the channel capacity. When full, `send`
292 /// returns `Err(())`, applying backpressure to senders. Use for
293 /// write-heavy actors where unbounded queue growth is undesirable.
294 pub fn start_actor_bounded(&self, actor: Box<dyn Actor>, bound: usize) -> Addr {
295 self.start_actor_or_router(actor, false, Some(bound))
296 }
297
298 /// Spawns a router actor. The router's context will have its `router`
299 /// field set to its own address (so messages forwarded to `router`
300 /// come back to itself).
301 pub fn start_router(&self, actor: Box<dyn Actor>) -> Addr {
302 self.start_actor_or_router(actor, true, None)
303 }
304
305 /// Spawns a child async task (non-blocking).
306 ///
307 /// The task's `JoinHandle` is tracked so it can be aborted on stop.
308 pub fn child_task<T>(&self, task: T)
309 where
310 T: Future<Output = ()> + Send + 'static,
311 {
312 let handle = crate::tokio_spawn::spawn(task);
313 self.task_handles.write().push(handle);
314 }
315
316 /// Spawns a blocking child task via `spawn_blocking`.
317 ///
318 /// Use for CPU-intensive work that should not block the async runtime.
319 #[cfg(not(target_arch = "wasm32"))]
320 pub fn blocking_child_task<F>(&self, task: F)
321 where
322 F: FnOnce() + Send + 'static,
323 {
324 let handle = tokio::task::spawn_blocking(task);
325 self.task_handles.write().push(handle);
326 }
327
328 fn start_actor_or_router(
329 &self,
330 mut actor: Box<dyn Actor>,
331 is_router: bool,
332 bound: Option<usize>,
333 ) -> Addr {
334 let capacity = bound.unwrap_or(DEFAULT_MAILBOX_CAPACITY);
335 let (sender, receiver) = mailbox::mailbox(capacity);
336 let addr = Addr::new(sender);
337 let (stop_sender, stop_receiver) = channel(1);
338 let new_context = self.child_context(addr.clone(), stop_sender.clone());
339 if is_router {
340 *new_context.router.write() = addr.clone();
341 }
342 self.stop_signals.write().insert(addr.clone(), stop_sender);
343 let stop_signals = self.stop_signals.clone();
344 let addr_clone = addr.clone();
345 crate::tokio_spawn::spawn(async move {
346 actor.run(receiver, stop_receiver, new_context).await;
347 stop_signals.write().remove(&addr_clone);
348 });
349 addr
350 }
351
352 /// Stops this actor and all its children.
353 ///
354 /// Aborts all child tasks and sends stop signals to all child actors.
355 /// Sets `is_stopped` to `true`.
356 pub fn stop(&self) {
357 for handle in self.task_handles.read().iter() {
358 handle.abort();
359 }
360 for signal in self.stop_signals.read().values() {
361 let _ = signal.try_send(());
362 }
363 *self.node.write() = None;
364 *self.is_stopped.write() = true;
365 }
366}
367
368/// A clonable, hashable address for sending messages to an actor.
369///
370/// `Addr` implements `PartialEq`, `Eq`, and `Hash` based on its `id` field
371/// (a random 32-character string), **not** the underlying channel sender.
372/// This means two `Addr`s are equal iff they refer to the same actor.
373///
374/// # Sending Messages
375///
376/// ```no_run
377/// use beam::actor::Addr;
378/// use beam::message::Message;
379/// use std::sync::Arc;
380///
381/// // addr.send(Message::Put(put)) — wraps in Arc internally
382/// // addr.send(Arc::clone(&msg)) — refcount bump, no allocation
383/// // Err(()) means the actor's mailbox is closed (actor stopped)
384/// ```
385#[derive(Clone, Debug)]
386pub struct Addr {
387 id: String,
388 sender: MailboxSender,
389}
390
391impl Addr {
392 /// Creates a new address wrapping a [`MailboxSender`].
393 pub fn new(sender: MailboxSender) -> Self {
394 Self {
395 id: random_string(32),
396 sender,
397 }
398 }
399
400 /// Sends a message to this actor.
401 ///
402 /// Accepts `impl Into<Arc<Message>>` so callers can pass either:
403 /// - `Message::Put(put)` — wrapped in `Arc` internally (one allocation)
404 /// - `Arc::clone(&msg)` — refcount bump, zero allocation (for fanout)
405 ///
406 /// Returns `Ok(())` if the message was enqueued, `Err(())` if the
407 /// mailbox is full (backpressure) or closed (actor stopped).
408 ///
409 /// Callers that must not lose messages should retry on `Err`. The
410 /// Router's storage dispatch uses `let _ = addr.send(...)` and accepts
411 /// occasional drops under extreme backpressure, which is the correct
412 /// trade-off for an LWW graph store.
413 #[allow(clippy::result_unit_err)] // mailbox-closed/full is unrecoverable; no meaningful error payload
414 pub fn send(&self, msg: impl Into<Arc<Message>>) -> Result<(), ()> {
415 self.sender.send(msg.into())
416 }
417
418 /// Returns a no-op address that silently drops all messages.
419 ///
420 /// Useful as a placeholder before a real address is set.
421 pub fn noop() -> Addr {
422 Addr::new(MailboxSender::noop())
423 }
424}
425
426impl PartialEq for Addr {
427 fn eq(&self, other: &Addr) -> bool {
428 self.id == other.id
429 }
430}
431
432impl Eq for Addr {}
433
434impl Hash for Addr {
435 fn hash<H: Hasher>(&self, state: &mut H) {
436 self.id.hash(state);
437 }
438}
439
440impl fmt::Display for Addr {
441 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
442 write!(f, "actor:{}", self.id)
443 }
444}
445
446#[cfg(test)]
447mod tests {
448 use super::*;
449
450 #[test]
451 fn test_addr_equality() {
452 let (s1, _r1) = crate::mailbox::mailbox(16);
453 let (s2, _r2) = crate::mailbox::mailbox(16);
454 let a1 = Addr::new(s1);
455 let a2 = Addr::new(s2);
456 assert_ne!(a1, a2, "different addrs are not equal");
457 assert_eq!(a1, a1.clone(), "clone is equal");
458 }
459
460 #[test]
461 fn test_addr_hash() {
462 let (s1, _r1) = crate::mailbox::mailbox(16);
463 let a1 = Addr::new(s1);
464 let a2 = a1.clone();
465 let mut set = std::collections::HashSet::new();
466 set.insert(a1);
467 assert!(set.contains(&a2), "clone should be found in HashSet");
468 }
469
470 #[test]
471 fn test_addr_display() {
472 let (s, _r) = crate::mailbox::mailbox(16);
473 let addr = Addr::new(s);
474 let display = format!("{}", addr);
475 assert!(display.starts_with("actor:"));
476 assert_eq!(display.len(), "actor:".len() + 32);
477 }
478
479 #[test]
480 fn test_addr_noop_sends_silently() {
481 let addr = Addr::noop();
482 assert_eq!(addr.id.len(), 32);
483 }
484
485 #[test]
486 fn test_addr_id_length() {
487 let (s, _r) = crate::mailbox::mailbox(16);
488 let addr = Addr::new(s);
489 assert_eq!(addr.id.len(), 32);
490 assert!(
491 addr.id.chars().all(|c| c.is_ascii_alphanumeric()),
492 "addr id should be alphanumeric"
493 );
494 }
495
496 struct TestActor {
497 received: Arc<RwLock<Vec<Message>>>,
498 }
499
500 #[async_trait]
501 impl Actor for TestActor {
502 async fn handle(&mut self, message: Arc<Message>, _ctx: &ActorContext) {
503 // Clone the inner Message out of the Arc for the test vector.
504 self.received.write().push((*message).clone());
505 }
506 }
507
508 #[tokio::test]
509 async fn test_actor_context_new() {
510 let ctx = ActorContext::new("peer1".to_string());
511 assert_eq!(*ctx.peer_id.read(), "peer1");
512 assert_eq!(ctx.child_actor_count(), 0);
513 assert!(!*ctx.is_stopped.read());
514 }
515
516 #[tokio::test]
517 async fn test_actor_start_and_send() {
518 let ctx = ActorContext::new("test".to_string());
519 let received = Arc::new(RwLock::new(Vec::new()));
520 let actor = TestActor {
521 received: received.clone(),
522 };
523 let _addr = ctx.start_actor(Box::new(actor));
524
525 // Give the actor a moment to start
526 crate::tokio_time::sleep(web_time::Duration::from_millis(50)).await;
527
528 assert_eq!(ctx.child_actor_count(), 1);
529
530 // Stop the actor
531 ctx.stop();
532 assert!(*ctx.is_stopped.read());
533 }
534}
535
536#[test]
537fn test_shutdown_signal_default_false() {
538 // A new ActorContext should have shutdown_rx initialized to false.
539 let ctx = ActorContext::new("test-peer".to_string());
540 assert!(
541 !*ctx.shutdown_rx.borrow(),
542 "shutdown_rx should default to false"
543 );
544}
545
546#[test]
547fn test_shutdown_signal_propagates_to_child() {
548 // When the parent sends true on shutdown, child contexts (which
549 // share the same watch channel) should observe the change.
550 let (tx, rx) = watch::channel(false);
551 let mut ctx = ActorContext::new("parent".to_string());
552 ctx.shutdown_rx = rx;
553
554 let (stop_tx, _stop_rx) = channel(1);
555 let child = ctx.child_context(Addr::noop(), stop_tx);
556
557 // Child starts with false
558 assert!(!*child.shutdown_rx.borrow());
559
560 // Parent signals shutdown
561 tx.send(true).unwrap();
562
563 // Child observes the change
564 assert!(
565 *child.shutdown_rx.borrow(),
566 "child should see shutdown signal"
567 );
568}
569
570#[test]
571fn test_shutdown_signal_isolated_per_node() {
572 // Different nodes create independent watch channels —
573 // signaling one should not affect the other.
574 let mut ctx_a = ActorContext::new("node-a".to_string());
575 let ctx_b = ActorContext::new("node-b".to_string());
576
577 // Both start false
578 assert!(!*ctx_a.shutdown_rx.borrow());
579 assert!(!*ctx_b.shutdown_rx.borrow());
580
581 // Replace ctx_a's channel with a controllable one
582 let (tx_a, rx_a) = watch::channel(false);
583 ctx_a.shutdown_rx = rx_a;
584 tx_a.send(true).unwrap();
585
586 // ctx_a sees shutdown, ctx_b does not
587 assert!(*ctx_a.shutdown_rx.borrow());
588 assert!(
589 !*ctx_b.shutdown_rx.borrow(),
590 "unrelated node should not see signal"
591 );
592}