Expand description
Tokio Actors is a light-weight, Tokio-native actor framework for building hierarchical systems with strongly-typed mailboxes, timers, and supervision.
§Overview
- Thread-safe actors that run as exclusive tasks on Tokio’s multi-threaded runtime.
- Typed request/response semantics through
ActorHandle::send. - Recurring timers, supervision hooks, and bounded mailboxes out of the box.
use tokio_actors::{actor::{Actor, ActorExt, context::ActorContext}, ActorResult, StopReason};
#[derive(Default)]
struct Counter(i64);
impl Actor for Counter {
type Message = i64;
type Response = i64;
async fn handle(&mut self, msg: i64, _ctx: &mut ActorContext<Self>) -> ActorResult<i64> {
self.0 += msg;
Ok(self.0)
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let counter = Counter::default().spawn().named("counter").await?;
counter.notify(5).await?; // fire-and-forget
let total = counter.send(3).await?; // request-response -> 8
counter.stop(StopReason::Graceful).await?;
Ok(())
}Re-exports§
pub use actor::context::ActorContext;pub use actor::context::ChildSpawnBuilder;pub use actor::context::RecurringScheduleBuilder;pub use actor::context::ScheduleBuilder;pub use actor::handle::ActorHandle;pub use actor::runtime::ActorConfig;pub use actor::runtime::MailboxConfig;pub use actor::supervision::SupervisionConfig;pub use actor::Actor;pub use actor::ActorExt;pub use actor::SpawnBuilder;pub use error::ActorError;pub use error::ActorResult;pub use error::AskError;pub use error::SendError;pub use error::SpawnError;pub use error::StreamError;pub use error::SupervisionError;pub use error::TimerError;pub use error::TrySendError;pub use system::ActorSystem;pub use system::ShutdownPolicy;pub use system::SystemConfig;pub use types::ActorId;pub use types::ActorStatusInfo;pub use types::ChildEvent;pub use types::ChildInfo;pub use types::MissPolicy;pub use types::RecurringId;pub use types::RestartStrategy;pub use types::RestartType;pub use types::SchedulePolicy;pub use types::Shutdown;pub use types::StopReason;pub use types::StreamEvent;pub use types::StreamId;pub use types::SupervisionAction;