Supervisor

Struct Supervisor 

Source
pub struct Supervisor { /* private fields */ }
Expand description

A minimal supervisor that coordinates shutdown and tracks child threads.

Blessed pattern:

§Error semantics

  • A child thread that returns Err causes Supervisor::join_all to return Err.
  • A child thread that panics also causes Supervisor::join_all to return Err. When multiple children fail, errors are aggregated into a single message.

Implementations§

Source§

impl Supervisor

Source

pub fn new(cfg: SupervisorConfig) -> Self

Create a new supervisor with the provided configuration.

Contract: configuration is validated when spawning children.

Source

pub fn config(&self) -> SupervisorConfig

Get the supervisor configuration.

Source

pub fn shutdown(&self) -> Shutdown

Access the supervisor’s shutdown coordinator.

Source

pub fn token(&self) -> ShutdownToken

Convenience accessor for the supervisor shutdown token.

Source

pub fn validate(&self) -> Result<()>

Validate the current configuration.

Errors: returns Error if the configuration is internally inconsistent.

Source

pub fn spawn_thread<F>(&self, name: impl Into<String>, f: F) -> Result<()>
where F: FnOnce(ShutdownToken) -> Result<()> + Send + 'static,

Spawn a supervised thread.

Contract: the closure should return when it observes shutdown.

Errors:

  • Returns an error if configuration validation fails.
  • Returns an error if the OS thread cannot be spawned.

If the closure returns Err, Supervisor::join_all will return Err.

Source

pub fn spawn_restartable_thread<F>( &self, name: impl Into<String>, f: F, ) -> Result<()>
where F: Fn(ShutdownToken) -> Result<()> + Send + Sync + 'static,

Spawn a supervised thread that will be restarted with backoff on error.

Contract:

  • The child exits successfully if shutdown is requested.
  • If the closure returns Err and shutdown is not requested, the supervisor sleeps for a backoff duration and retries.

Errors:

  • Returns an error if configuration validation fails.
  • Returns an error if the OS thread cannot be spawned.
  • Returns an error if the child exceeds max_restarts.
Source

pub fn join_all(&self) -> Result<()>

Join all supervised children.

Contract: this drains the supervisor’s child list. A subsequent call will join zero children and return Ok(()).

Errors:

  • If any child returns an error, returns an aggregated Error.
  • If any child panics, returns an aggregated Error.
Source

pub fn shutdown_and_join(&self) -> Result<()>

Convenience: trigger shutdown and then join all children.

Contract: this is equivalent to calling Shutdown::trigger and then Supervisor::join_all.

Trait Implementations§

Source§

impl Clone for Supervisor

Source§

fn clone(&self) -> Supervisor

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.