Crates.io | witty-actors |
lib.rs | witty-actors |
version | 0.6.0 |
source | src |
created_at | 2023-05-30 22:41:49.470027 |
updated_at | 2023-05-30 22:41:49.470027 |
description | Fork of quickwit-actors, Actor framework used in quickwit |
homepage | https://docs.rs/witty-actors |
repository | https://github.com/valyagolev/witty-actors |
max_upload_size | |
id | 878361 |
size | 230,872 |
Forked from https://github.com/quickwit-oss/quickwit/blob/83041f78a21072df091f6d945cc1b5859cf72326/quickwit/quickwit-common/ .
Probably will be maintained in the future.
Docs: https://docs.rs/witty-actors
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;
}