ractor-supervisor
A pure-Rust supervisor built atop the ractor framework. It provides OTP-style supervision trees so you can define how child actors should be restarted under different failure conditions, along with meltdown logic to prevent runaway restart loops.
Overview
ractor-supervisor is inspired by the way Erlang/Elixir OTP handles actor supervision. It helps you:
- Define how you want to restart failing children—through different supervision strategies.
- Configure meltdown thresholds so that if too many restarts occur in a short period, the supervisor itself shuts down abnormally.
- Optionally add child-level backoff or meltdown reset intervals for even more granular control.
Installation
Add the following to your Cargo.toml:
[]
= "0.1"
= "0.14"
To get started, you should already have familiarity with ractor. This crate builds on top of ractor’s actor model.
SupervisorOptions
These options control the supervisor-wide meltdown logic and overall restart behavior:
-
strategy
Defines which children get restarted when any one child fails:- OneForOne: Only the failing child is restarted.
- OneForAll: If any child fails, all children are stopped and restarted.
- RestForOne: The failing child and all subsequent children (in definition order) are stopped and restarted.
-
max_restarts+max_seconds
Meltdown window. Ifmax_restartsis exceeded withinmax_seconds, the supervisor triggers a meltdown and stops abnormally. -
restart_counter_reset_after
If the supervisor sees no restarts for this many seconds, it resets its meltdown counter back to zero. This prevents old failures from accumulating indefinitely.
ChildSpec
These specs define how each child actor is spawned and restarted. You provide:
id: A unique identifier for the child (used in logs, meltdown tracking, etc.).restart: One ofPermanent(always restart),Transient(only if fails abnormally), orTemporary(never restart).spawn_fn: A user-provided function that spawns (and links) the child actor; typically callsActor::spawn_linked.backoff_fn(optional): A function returning an extraDurationdelay before restarting this child (e.g., exponential backoff).restart_counter_reset_after(optional): If the child remains alive for that many seconds, its own restart count is reset next time it fails.
Multi-Level Supervision Trees
Supervisors can manage other supervisors as children, forming a hierarchical or tree structure. This way, different subsystems can each have their own meltdown thresholds or strategies. A meltdown in one subtree doesn’t necessarily mean the entire application must go down, unless the top-level supervisor is triggered.
For example, you might have:
- Root Supervisor (OneForOne)
- Sub-supervisor A (OneForAll)
- Child actor #1
- Child actor #2
- Sub-supervisor B (RestForOne)
- Child actor #3
- Child actor #4
- Sub-supervisor A (OneForAll)
With nested supervision, you can isolate failures and keep the rest of your system running.
Usage
Below is a full code snippet showing how to configure and spawn the supervisor. We skip demonstrating the child actor implementation itself—assuming you already have one. Notice how we pass in a custom spawn_my_worker function, define meltdown thresholds, and pick a specific restart strategy.
use Actor;
use *;
use ;
use Instant;
use FutureExt;
// A minimal child actor that simply does some work in `handle`.
;
// A function to spawn the child actor. This will be used in ChildSpec::spawn_fn.
async
async
License
This project is licensed under MIT. It is heavily inspired by Elixir/Erlang OTP patterns, but implemented in pure Rust for the ractor framework