pub struct Supervisor<E: Event, T: Topic<E> = DefaultTopic> { /* private fields */ }Expand description
Coordinates actors and the broker, and owns the top-level runtime.
§Actor Registration
ⓘ
// Subscribe to specific topics
supervisor.add_actor("processor", |ctx| Processor::new(ctx), &[MyTopic::Data])?;
// Subscribe to all topics (e.g., monitoring)
supervisor.add_actor("monitor", |ctx| Monitor::new(ctx), Subscribe::all())?;
// Subscribe to no topics (pure event producer)
supervisor.add_actor("producer", |ctx| Producer::new(ctx), Subscribe::none())?;§Runtime Control
start()spawns the broker loop and returns immediately (non-blocking).join()awaits all actor tasks to finish; typically used afterstart().run()combinesstart()andjoin(), blocking until shutdown.stop()graceful shutdown; lets actors consume active eventssend(event)emits events into the broker.
Implementations§
Source§impl<E: Event, T: Topic<E>> Supervisor<E, T>
impl<E: Event, T: Topic<E>> Supervisor<E, T>
Sourcepub fn new(config: Config) -> Self
pub fn new(config: Config) -> Self
Create a new supervisor with the given runtime configuration.
Sourcepub fn add_actor<A, F, S>(
&mut self,
name: &str,
factory: F,
topics: S,
) -> Result<ActorId>
pub fn add_actor<A, F, S>( &mut self, name: &str, factory: F, topics: S, ) -> Result<ActorId>
Register a new actor with a factory that receives a Context<E>.
This is the primary way to register actors with the supervisor.
§Arguments
name- Actor identifier used for metadata and routingfactory- Closure that receives a Context and returns the actortopics- Slice of topics the actor subscribes to
§Example
ⓘ
supervisor.add_actor(
"processor",
|ctx| DataProcessor::new(ctx),
&[MyTopic::Data, MyTopic::Control]
)?;Sourcepub async fn start(&mut self) -> Result<()>
pub async fn start(&mut self) -> Result<()>
Start the broker loop in a background task. This returns immediately.
Sourcepub async fn join(&mut self) -> Result<()>
pub async fn join(&mut self) -> Result<()>
Waits until at least one of the actor tasks completes then triggers a shutdown if not already requested.
Sourcepub async fn run(&mut self) -> Result<()>
pub async fn run(&mut self) -> Result<()>
Convenience method to start and then await completion of all tasks. Blocks until shutdown.
Sourcepub async fn send(&self, event: E) -> Result<()>
pub async fn send(&self, event: E) -> Result<()>
Emit an event into the broker from the supervisor.
Sourcepub async fn stop(&mut self) -> Result<()>
pub async fn stop(&mut self) -> Result<()>
Request a graceful shutdown, then await all actor tasks.
§Shutdown Process
- Waits for the broker to receive all pending events (up to 10 ms)
- Stops the broker and waits for it to drain actor queues
- Cancels all actors and waits for tasks t
pub fn config(&self) -> &Config
Trait Implementations§
Auto Trait Implementations§
impl<E, T> Freeze for Supervisor<E, T>
impl<E, T = DefaultTopic> !RefUnwindSafe for Supervisor<E, T>
impl<E, T> Send for Supervisor<E, T>
impl<E, T> Sync for Supervisor<E, T>
impl<E, T> Unpin for Supervisor<E, T>
impl<E, T = DefaultTopic> !UnwindSafe for Supervisor<E, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more