Supervision the Erlang/OTP way. A supervisor runs its children as tasks, restarts them when they exit, and gives up when restarts come too fast. Giving up means failing, so the supervisor above it restarts it in turn.
Effects: time (for the restart window).
fn main(): int! uses net, time {
let web = sup.child("web", () => serve(8080))
let jobs = sup.transient("jobs", () => work())
sup.run([web, jobs], sup.one_for_one(5, 60_000))? // at most 5 restarts per minute
0
}
// one child, one-for-one
sup.restart(() => serve(8080), 5, 60_000)?A child is a function () => ()!. It exits in one of three ways: it returns normally, it returns an Err, or it panics. A panic counts as a failure, just like an Err. Every restart calls the function again, so the child starts from scratch. Children share no state with each other.
API#
enum Restart { Permanent, Transient, Temporary }: when to restart an exited child.Permanentrestarts it always,Transientonly after a failure, andTemporarynever.enum Strategy { OneForOne, OneForAll, RestForOne }: what a restart takes down.OneForOnerestarts only the child that exited,OneForAllrestarts every child, andRestForOnerestarts that child and the children started after it. The others in the group are stopped last-first, then the group restarts in order. ATemporarysibling isn't started again.struct Child { id: str, start: () => ()!, restart: Restart }, built withchild(id, f)(permanent),transient(id, f)ortemporary(id, f).struct Policy { strategy: Strategy, intensity: int, period: int }, built withone_for_one(intensity, period),one_for_all(..)orrest_for_one(..). Up tointensityrestarts are allowed within anyperiodms. One more stops every child and fails the supervisor.run(kids: [Child], p: Policy): ()!: blocks until every child has exited for good, which returnsok, or until the restart limit is hit, which returns anErrnaming the child and its last exit. Supervisors nest: a child can callsup.runitself.restart(f, intensity, period): ()!: shorthand forrunwith one permanent child underone_for_one.
Every exit is reported on stderr (sup: web exited: err(...); restarting).
How it maps to OTP#
- Children are tasks. A child's panic ends only that child: the supervisor gets it from
await anyas anErr, the equivalent of OTP's{'EXIT', Pid, Reason}. - Children are stopped with
t.cancel(). It is cooperative: the child stops at its next loop iteration or blocking call. Acatchinside the child can't hold it. - With
auk run --live, a code change that alters the layout of a struct or enum stops only the children that use it. Their supervisor restarts them on the new code, which is OTP's code upgrade without writingcode_change. - Tasks are linked to the tasks they start. A supervisor that crashes or is cancelled takes its whole subtree down with it.
- Children share nothing. Each runs in its own heap: its start function (a closure) and everything it captures are copied in. When a child crashes or is stopped, its heap is freed, and so are the connections it still held.
- Differences from OTP: there are no named processes, no dynamic
start_child, and no message queue per task. Use aChan<T>instead.