Struct hannibal::Supervisor
source · pub struct Supervisor;
Expand description
Actor supervisor
Supervisor gives the actor the ability to restart after failure. When the actor fails, recreate a new actor instance and replace it.
Implementations§
source§impl Supervisor
impl Supervisor
sourcepub async fn start<A, F>(f: F) -> Result<Addr<A>>
pub async fn start<A, F>(f: F) -> Result<Addr<A>>
Start a supervisor
§Examples
use hannibal::*;
use std::time::Duration;
#[message]
struct Die;
#[message]
struct Add;
#[message(result = i32)]
struct Get;
struct MyActor(i32);
impl Actor for MyActor {}
impl Handler<Add> for MyActor {
async fn handle(&mut self, ctx: &mut Context<Self>, _: Add) {
self.0 += 1;
}
}
impl Handler<Get> for MyActor {
async fn handle(&mut self, ctx: &mut Context<Self>, _: Get) -> i32 {
self.0
}
}
impl Handler<Die> for MyActor {
async fn handle(&mut self, ctx: &mut Context<Self>, _: Die) {
ctx.stop(None);
}
}
#[hannibal::main]
async fn main() -> Result<()> {
let mut addr = Supervisor::start(|| MyActor(0)).await?;
addr.send(Add)?;
assert_eq!(addr.call(Get).await?, 1);
addr.send(Add)?;
assert_eq!(addr.call(Get).await?, 2);
addr.send(Die)?;
sleep(Duration::from_secs(1)).await; // Wait for actor restart
assert_eq!(addr.call(Get).await?, 0);
Ok(())
}
Auto Trait Implementations§
impl Freeze for Supervisor
impl RefUnwindSafe for Supervisor
impl Send for Supervisor
impl Sync for Supervisor
impl Unpin for Supervisor
impl UnwindSafe for Supervisor
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