ave-actors-actor
Async actor model for Rust built on Tokio. Actors communicate exclusively through message passing, run concurrently in async tasks, and support supervision trees, lifecycle hooks, persistence, and event broadcasting.
This crate is part of the ave-actors workspace.
Core concepts
| Concept | Description |
|---|---|
Actor |
Trait that defines behavior, lifecycle hooks, and supervision strategy |
Handler<M> |
Trait for processing a message type M |
ActorRef<A> |
Typed handle to send messages to an actor |
ActorContext<A> |
Provided to the actor during execution; used to spawn children, emit errors, publish events |
ActorSystem |
Creates and manages actors; holds a registry keyed by ActorPath |
ActorPath |
Hierarchical address, e.g. /user/parent/child |
Main API
| API | Receives | Returns | Purpose |
|---|---|---|---|
ActorSystem::create |
graceful and crash CancellationTokens |
(SystemRef, SystemRunner) |
Creates the actor runtime and control loop |
SystemRef::create_root_actor |
root actor name and an IntoActor value |
Result<ActorRef<A>, Error> |
Spawns a top-level actor at /user/<name> |
ActorContext::create_child |
child name and an IntoActor value |
Result<ActorRef<C>, Error> |
Spawns a child actor under the current actor path |
ActorRef::tell |
one message | Result<(), Error> |
Sends fire-and-forget work |
ActorRef::ask |
one message | Result<Response, Error> |
Sends a request and waits for a reply |
ActorRef::ask_timeout |
one message and a Duration |
Result<Response, Error> |
Same as ask, but fails with Error::Timeout |
ActorRef::ask_stop |
nothing | Result<(), Error> |
Requests graceful shutdown and waits for confirmation |
ActorRef::tell_stop |
nothing | () |
Requests shutdown without waiting |
ActorRef::subscribe |
nothing | broadcast::Receiver<Event> |
Subscribes to future actor events |
SystemRef::run_sink |
Sink<E> |
() |
Spawns an event subscriber task |
Quick start
use ;
use ;
use async_trait;
use CancellationToken;
// --- Messages ---
;
;
// --- Actor ---
;
// --- Main ---
async
Lifecycle
pre_start → [message loop] → pre_stop → post_stop
↑ (on failure + Retry strategy)
pre_restart
Override any hook in the Actor trait:
async
async
async
async
Supervision
Root actors can restart automatically on failure:
use ;
use Duration;
Child faults are escalated to the parent via Handler::on_child_fault, which returns a ChildAction (Stop, Restart, or Delegate).
Message passing
// Fire-and-forget
actor_ref.tell.await?;
// Request-response (waits for reply)
let response = actor_ref.ask.await?;
// With custom timeout
let response = actor_ref.ask_timeout.await?;
// Graceful stop, waits for actor to finish
actor_ref.ask_stop.await?;
Events
Actors broadcast typed events to subscribers:
// Inside the actor:
ctx.publish_event.await;
// Subscriber outside:
let mut rx = actor_ref.subscribe;
while let Ok = rx.recv.await
// Or run a sink task:
use Sink;
system.run_sink.await;
Subscriber<E> is a trait with a single notify(&self, event: E) method.
Actor paths
Paths are hierarchical strings following a /user/<name> convention.
let path = from;
let child_path = path / "child"; // "/user/parent/child"
path.is_parent_of; // true
path.level; // 2
path.key; // "parent"