Expand description
Fault-tolerant supervision trees for Rust with distributed capabilities.
Build resilient systems that automatically recover from failures using supervisor trees, restart strategies, and distributed supervision inspired by Erlang/OTP.
§Features
- Supervision Trees: Hierarchical supervision with nested supervisors and workers
- Restart Strategies:
OneForOne,OneForAll, andRestForOnestrategies - Restart Policies:
Permanent,Temporary, andTransientrestart behaviors - Restart Intensity: Configurable restart limits with sliding time windows
- Distributed: Run supervisors across processes or machines via TCP/Unix sockets
- Generic Workers: Trait-based worker system for any async workload
- Dynamic Management: Add/remove children at runtime
- Structured Logging: Built-in support for
slogstructured logging
§Quick Start
use ash_flare::{SupervisorSpec, SupervisorHandle, RestartPolicy, Worker};
use async_trait::async_trait;
struct Counter {
id: u32,
max: u32,
}
#[async_trait]
impl Worker for Counter {
type Error = std::io::Error;
async fn run(&mut self) -> Result<(), Self::Error> {
for i in 0..self.max {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
Ok(())
}
}
// Build supervisor tree
let spec = SupervisorSpec::new("root")
.with_worker("counter-1", || Counter { id: 1, max: 5 }, RestartPolicy::Permanent)
.with_worker("counter-2", || Counter { id: 2, max: 5 }, RestartPolicy::Permanent);
// Start supervision tree
let handle = SupervisorHandle::start(spec);
// Query children
let children = handle.which_children().await?;
// Graceful shutdown
handle.shutdown().await?;§Restart Strategies
- OneForOne: Restarts only the failed child (default)
- OneForAll: Restarts all children if any child fails
- RestForOne: Restarts the failed child and all children started after it
§Examples
See the examples directory for more:
counter.rs- Basic supervisor with multiple workersdistributed.rs- Network-distributed supervisorssuper_tree.rs- Complex nested supervision trees
Re-exports§
pub use supervisor_stateful::StatefulSupervisorError;pub use supervisor_stateful::StatefulSupervisorHandle;pub use supervisor_stateful::StatefulSupervisorSpec;
Modules§
- distributed
- Distributed supervision via TCP/Unix sockets
- supervisor_
stateful - Stateful supervisor with shared in-memory key-value store
Macros§
- connect_
supervisor - Connect to a remote supervisor via TCP or Unix socket
- distributed_
system - Create and start a distributed supervision system with server and client
- impl_
worker - Implement the Worker trait with minimal boilerplate for the run method only
- impl_
worker_ stateful - Implement the Worker trait with access to self for stateful workers
- serve_
supervisor - Start a distributed supervisor server with TCP or Unix socket
- stateful_
supervision_ tree - Build a stateful supervision tree with shared in-memory key-value store
- supervision_
tree - Build a supervision tree with a declarative syntax
Structs§
- Child
Info - Information about a child process
- Restart
Intensity - Restart intensity limits with max restarts within a time window
- Supervisor
Handle - Handle used to interact with a running supervisor tree.
- Supervisor
Spec - Describes a supervisor and its children in a tree structure.
- Worker
Context - Shared context for stateful workers with in-memory key-value store.
Enums§
- Child
Exit Reason - Result of a worker’s execution
- Child
Type - Type of child in supervision tree
- Restart
Policy - When to restart a child
- Restart
Strategy - Restart strategy for supervisor children
- Supervisor
Error - Errors returned by supervisor operations.
- Worker
Error - Errors returned by worker operations.
Traits§
- Worker
- A trait that all workers must implement to work with the supervisor tree. This allows for generic workers that can handle any type of work.
Type Aliases§
- ChildId
- Child identifier type