Crates.io | quickwit-actors |
lib.rs | quickwit-actors |
version | 0.3.0 |
source | src |
created_at | 2022-03-28 04:48:55.309957 |
updated_at | 2022-06-07 06:58:10.817991 |
description | Actor framework used in quickwit |
homepage | https://quickwit.io/ |
repository | https://github.com/quickwit-oss/quickwit |
max_upload_size | |
id | 557570 |
size | 169,022 |
Yet another actor crate for rust. This crate exists specifically to answer quickwit needs. The API may change in the future.
Actor::runner
method makes it possible to run an actor with blocking code on a dedicated thread.use std::time::Duration;
use async_trait::async_trait;
use quickwit_actors::{Handler, Actor, Universe, ActorContext, ActorExitStatus, Mailbox};
#[derive(Default)]
struct PingReceiver;
impl Actor for PingReceiver {
type ObservableState = ();
fn observable_state(&self) -> Self::ObservableState {}
}
#[async_trait]
impl Handler<Ping> for PingReceiver {
type Reply = String;
async fn handle(
&mut self,
_msg: Ping,
_ctx: &ActorContext<Self>,
) -> Result<String, ActorExitStatus> {
Ok("Pong".to_string())
}
}
struct PingSender {
peer: Mailbox<PingReceiver>,
}
#[derive(Debug)]
struct Loop;
#[derive(Debug)]
struct Ping;
#[async_trait]
impl Actor for PingSender {
type ObservableState = ();
fn observable_state(&self) -> Self::ObservableState {}
async fn initialize(&mut self, ctx: &ActorContext<Self>) -> Result<(),ActorExitStatus> {
ctx.send_self_message(Loop).await?;
Ok(())
}
}
#[async_trait]
impl Handler<Loop> for PingSender {
type Reply = ();
async fn handle(
&mut self,
_: Loop,
ctx: &ActorContext<Self>,
) -> Result<(), ActorExitStatus> {
let reply_msg = ctx.ask(&self.peer, Ping).await.unwrap();
println!("{reply_msg}");
ctx.schedule_self_msg(Duration::from_secs(1), Loop).await;
Ok(())
}
}
#[tokio::main]
async fn main() {
let universe = Universe::new();
let (recv_mailbox, _) =
universe.spawn_actor(PingReceiver::default()).spawn();
let ping_sender = PingSender { peer: recv_mailbox };
let (_, ping_sender_handler) = universe.spawn_actor(ping_sender).spawn();
ping_sender_handler.join().await;
}