pub struct Supervisor { /* private fields */ }Expand description
Host-side child restarter (design notes §15-16).
A Supervisor is not a bytecode Flow. It is a dedicated OS
thread plus a table of ChildSpecs. When a supervised flow
becomes FlowState::Failed (or completes, under
RestartPolicy::Always), the worker delivers the
FlowOutcome here instead of letting the fault take anything
else down. The supervisor then consults the child’s
RestartPolicy and, if intensity allows, respawns it under a
fresh FlowId — Pids are never reused (see
super::process::FlowId).
Constructed from a RuntimeSpawner so it does not have to own the
runtime’s worker JoinHandles.
Implementations§
Source§impl Supervisor
impl Supervisor
pub fn new(spawner: RuntimeSpawner) -> Result<Self, SpawnError>
Sourcepub fn with_config(
spawner: RuntimeSpawner,
config: SupervisorConfig,
) -> Result<Self, SpawnError>
pub fn with_config( spawner: RuntimeSpawner, config: SupervisorConfig, ) -> Result<Self, SpawnError>
Start the dedicated supervisor OS thread. Thread-spawn failure is
SpawnError::ThreadSpawnFailed — same category-A surface as
super::runtime::Runtime::new, not a panic.
Examples found in repository?
15fn main() {
16 let rt = match Runtime::with_config(
17 samples::boom(),
18 RuntimeConfig {
19 workers: 1,
20 quantum: 1_000,
21 mailbox: byteflow::MailboxConfig::DEFAULT,
22 ..Default::default()
23 },
24 ) {
25 Ok(rt) => rt,
26 Err(e) => {
27 eprintln!("runtime: {e}");
28 std::process::exit(1);
29 }
30 };
31 let Some(boom) = rt.function_index("boom") else {
32 eprintln!("missing boom");
33 std::process::exit(1);
34 };
35 let sup = match Supervisor::with_config(
36 rt.spawner(),
37 SupervisorConfig {
38 max_restarts: 2,
39 max_period: Duration::from_secs(5),
40 },
41 ) {
42 Ok(s) => s,
43 Err(e) => {
44 eprintln!("supervisor: {e}");
45 std::process::exit(1);
46 }
47 };
48 if let Err(e) = sup.start_child(ChildSpec::new("boom", boom).restart(RestartPolicy::OnFailure))
49 {
50 eprintln!("start_child: {e}");
51 std::process::exit(1);
52 }
53
54 let start = std::time::Instant::now();
55 while !sup.intensity_exceeded() {
56 if start.elapsed() > Duration::from_secs(2) {
57 eprintln!("supervisor did not hit intensity in time");
58 std::process::exit(1);
59 }
60 thread::sleep(Duration::from_millis(5));
61 }
62
63 let metrics = rt.metrics();
64 println!("intensity exceeded after {} failures", metrics.processes_failed);
65 println!("{metrics}");
66 sup.shutdown();
67 rt.shutdown();
68}Sourcepub fn start_child(&self, spec: ChildSpec) -> Result<FlowHandle, SpawnError>
pub fn start_child(&self, spec: ChildSpec) -> Result<FlowHandle, SpawnError>
Spawn spec and start supervising it. The returned handle is for
this incarnation only — a restart allocates a new Pid and a new
completion channel.
Examples found in repository?
15fn main() {
16 let rt = match Runtime::with_config(
17 samples::boom(),
18 RuntimeConfig {
19 workers: 1,
20 quantum: 1_000,
21 mailbox: byteflow::MailboxConfig::DEFAULT,
22 ..Default::default()
23 },
24 ) {
25 Ok(rt) => rt,
26 Err(e) => {
27 eprintln!("runtime: {e}");
28 std::process::exit(1);
29 }
30 };
31 let Some(boom) = rt.function_index("boom") else {
32 eprintln!("missing boom");
33 std::process::exit(1);
34 };
35 let sup = match Supervisor::with_config(
36 rt.spawner(),
37 SupervisorConfig {
38 max_restarts: 2,
39 max_period: Duration::from_secs(5),
40 },
41 ) {
42 Ok(s) => s,
43 Err(e) => {
44 eprintln!("supervisor: {e}");
45 std::process::exit(1);
46 }
47 };
48 if let Err(e) = sup.start_child(ChildSpec::new("boom", boom).restart(RestartPolicy::OnFailure))
49 {
50 eprintln!("start_child: {e}");
51 std::process::exit(1);
52 }
53
54 let start = std::time::Instant::now();
55 while !sup.intensity_exceeded() {
56 if start.elapsed() > Duration::from_secs(2) {
57 eprintln!("supervisor did not hit intensity in time");
58 std::process::exit(1);
59 }
60 thread::sleep(Duration::from_millis(5));
61 }
62
63 let metrics = rt.metrics();
64 println!("intensity exceeded after {} failures", metrics.processes_failed);
65 println!("{metrics}");
66 sup.shutdown();
67 rt.shutdown();
68}pub fn live_children(&self) -> usize
Sourcepub fn intensity_exceeded(&self) -> bool
pub fn intensity_exceeded(&self) -> bool
true once more than SupervisorConfig::max_restarts respawns
landed inside the intensity window. Remaining children keep
running; we just stop bringing them back (no safe abort of a
mid-quantum flow).
Examples found in repository?
15fn main() {
16 let rt = match Runtime::with_config(
17 samples::boom(),
18 RuntimeConfig {
19 workers: 1,
20 quantum: 1_000,
21 mailbox: byteflow::MailboxConfig::DEFAULT,
22 ..Default::default()
23 },
24 ) {
25 Ok(rt) => rt,
26 Err(e) => {
27 eprintln!("runtime: {e}");
28 std::process::exit(1);
29 }
30 };
31 let Some(boom) = rt.function_index("boom") else {
32 eprintln!("missing boom");
33 std::process::exit(1);
34 };
35 let sup = match Supervisor::with_config(
36 rt.spawner(),
37 SupervisorConfig {
38 max_restarts: 2,
39 max_period: Duration::from_secs(5),
40 },
41 ) {
42 Ok(s) => s,
43 Err(e) => {
44 eprintln!("supervisor: {e}");
45 std::process::exit(1);
46 }
47 };
48 if let Err(e) = sup.start_child(ChildSpec::new("boom", boom).restart(RestartPolicy::OnFailure))
49 {
50 eprintln!("start_child: {e}");
51 std::process::exit(1);
52 }
53
54 let start = std::time::Instant::now();
55 while !sup.intensity_exceeded() {
56 if start.elapsed() > Duration::from_secs(2) {
57 eprintln!("supervisor did not hit intensity in time");
58 std::process::exit(1);
59 }
60 thread::sleep(Duration::from_millis(5));
61 }
62
63 let metrics = rt.metrics();
64 println!("intensity exceeded after {} failures", metrics.processes_failed);
65 println!("{metrics}");
66 sup.shutdown();
67 rt.shutdown();
68}Sourcepub fn shutdown(self)
pub fn shutdown(self)
Stop the drive thread. Does not terminate live children — they belong to the runtime, not to us.
Examples found in repository?
15fn main() {
16 let rt = match Runtime::with_config(
17 samples::boom(),
18 RuntimeConfig {
19 workers: 1,
20 quantum: 1_000,
21 mailbox: byteflow::MailboxConfig::DEFAULT,
22 ..Default::default()
23 },
24 ) {
25 Ok(rt) => rt,
26 Err(e) => {
27 eprintln!("runtime: {e}");
28 std::process::exit(1);
29 }
30 };
31 let Some(boom) = rt.function_index("boom") else {
32 eprintln!("missing boom");
33 std::process::exit(1);
34 };
35 let sup = match Supervisor::with_config(
36 rt.spawner(),
37 SupervisorConfig {
38 max_restarts: 2,
39 max_period: Duration::from_secs(5),
40 },
41 ) {
42 Ok(s) => s,
43 Err(e) => {
44 eprintln!("supervisor: {e}");
45 std::process::exit(1);
46 }
47 };
48 if let Err(e) = sup.start_child(ChildSpec::new("boom", boom).restart(RestartPolicy::OnFailure))
49 {
50 eprintln!("start_child: {e}");
51 std::process::exit(1);
52 }
53
54 let start = std::time::Instant::now();
55 while !sup.intensity_exceeded() {
56 if start.elapsed() > Duration::from_secs(2) {
57 eprintln!("supervisor did not hit intensity in time");
58 std::process::exit(1);
59 }
60 thread::sleep(Duration::from_millis(5));
61 }
62
63 let metrics = rt.metrics();
64 println!("intensity exceeded after {} failures", metrics.processes_failed);
65 println!("{metrics}");
66 sup.shutdown();
67 rt.shutdown();
68}