Trait heph::supervisor::Supervisor [−][src]
pub trait Supervisor<NA> where
NA: NewActor, { fn decide(
&mut self,
error: <NA::Actor as Actor>::Error
) -> SupervisorStrategy<NA::Argument>; fn decide_on_restart_error(
&mut self,
error: NA::Error
) -> SupervisorStrategy<NA::Argument>; fn second_restart_error(&mut self, error: NA::Error); }
Expand description
The supervisor of an actor.
For more information about supervisors see the module documentation, here only the design of the trait is discussed.
The trait is designed to be generic over the NewActor
implementation
(NA
). This means that the same type can implement supervision for a number
of different actors. But a word of caution, supervisors should generally be
small and simple, which means that having a different supervisor for each
actor is often a good thing.
The restart_supervisor!
macro can be used to easily create a supervisor
implementation that restarts the actor.
Supervisor
can be implemented using a simple function if the NewActor
implementation doesn’t return an error (i.e. NewActor::Error = !
), which
is the case for asynchronous functions. See the module documentation for
an example of this.
Required methods
Decide what happens to the actor that returned error
.
Notes
A restarted actor will immediately will be run again. This has the benefit that the actor can setup any asynchronous operations without having to wait to be run again.
However it also has a downside: it’s easy to create create an infinite loop. When a supervisor always restarts an actor that always returns an error, we’ve got an effective infinite loop of restarting and running the actor, the actor crashes and is restarted and run again, etc.
To avoid creating such an infinite loop limit the amount times an actor
can be restarted. Or use the restart_supervisor!
macro to
automatically create a supervisor that handles this for you.
fn decide_on_restart_error(
&mut self,
error: NA::Error
) -> SupervisorStrategy<NA::Argument>
fn decide_on_restart_error(
&mut self,
error: NA::Error
) -> SupervisorStrategy<NA::Argument>
fn second_restart_error(&mut self, error: NA::Error)
fn second_restart_error(&mut self, error: NA::Error)
Method that gets call if an actor fails to restart twice.
This is only called if decide
returns a restart strategy, the actors
fails to restart, after which decide_on_restart_error
is called and
also returns a restart strategy and restarting the actor a second time
also fails. We will not create an endless loop of restarting failures
and instead call this function before stopping the actor (which can’t be
restarted any more).