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 xactor::*;
use std::time::Duration;
#[message]
struct Die;
#[message]
struct Add;
#[message(result = "i32")]
struct Get;
struct MyActor(i32);
impl Actor for MyActor {}
#[async_trait::async_trait]
impl Handler<Add> for MyActor {
async fn handle(&mut self, ctx: &mut Context<Self>, _: Add) {
self.0 += 1;
}
}
#[async_trait::async_trait]
impl Handler<Get> for MyActor {
async fn handle(&mut self, ctx: &mut Context<Self>, _: Get) -> i32 {
self.0
}
}
#[async_trait::async_trait]
impl Handler<Die> for MyActor {
async fn handle(&mut self, ctx: &mut Context<Self>, _: Die) {
ctx.stop(None);
}
}
#[xactor::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(())
}Examples found in repository?
examples/supervisor_clear_send_later.rs (line 44)
43async fn main() -> Result<(), Box<dyn std::error::Error>> {
44 let service_supervisor = xactor::Supervisor::start(PingLater::default).await?;
45 let service_addr = service_supervisor.clone();
46
47 let supervisor_task = xactor::spawn(async {
48 service_supervisor.wait_for_stop().await;
49 });
50
51 let send_ping = async {
52 println!(" main :: sending Ping");
53 service_addr.send(Ping("before halt")).unwrap();
54 };
55
56 let send_halt = async {
57 xactor::sleep(Duration::from_millis(1_000)).await;
58 println!(" main :: sending Halt");
59 service_addr.send(Halt).unwrap();
60 };
61
62 let _ = futures::join!(supervisor_task, send_halt, send_ping);
63
64 Ok(())
65}More examples
examples/supervisor_clear_interval.rs (line 69)
68async fn main() -> Result<(), Box<dyn std::error::Error>> {
69 let service_supervisor = xactor::Supervisor::start(PingTimer::default).await?;
70 let service_addr = service_supervisor.clone();
71
72 let supervisor_task = xactor::spawn(async {
73 service_supervisor.wait_for_stop().await;
74 });
75
76 let send_halt = async {
77 xactor::sleep(Duration::from_millis(5_200)).await;
78 println!(" main :: sending Halt");
79 service_addr.send(Halt).unwrap();
80 };
81
82 let _ = futures::join!(supervisor_task, send_halt);
83 // run this to see that the interval is not properly stopped if the ctx is stopped
84 // futures::join!(supervisor_task, send_panic); // there is no panic recovery
85
86 Ok(())
87}Auto Trait Implementations§
impl Freeze for Supervisor
impl RefUnwindSafe for Supervisor
impl Send for Supervisor
impl Sync for Supervisor
impl Unpin for Supervisor
impl UnsafeUnpin 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