#actor #actor-framework #context #indeed #send #messages #response

gabriel2

Gabriel2: Indeed, an actor library, not a framework, written in Rust

3 releases (stable)

1.0.1 Mar 19, 2024
0.0.1 Mar 19, 2024

#253 in Concurrency

Download history 285/week @ 2024-03-17 8/week @ 2024-03-24 68/week @ 2024-03-31 3/week @ 2024-04-07

101 downloads per month

Custom license

27KB
199 lines

Gabriel2

Gabriel2

Gabriel2: Indeed, an actor library, not a framework, written in Rust

Features

  • Async for sending messages
  • Async for messages processing in actor
  • Support messaging like send and forget
  • Support messaging like send and wait response
  • Mutable state of actor
  • Self reference in actor from context
  • Actor lifecycle (pre_start, pre_stop)

Usage

Cargo.toml

[dependencies]
gabriel2 = "1.0.1"

echo.rs

#[derive(Debug)]
pub struct Echo;

#[derive(Debug)]
pub enum Message {
    Ping,
}

#[derive(Debug)]
pub enum Response {
    Pong {counter: u32},
}

#[derive(Debug,Clone)]
pub struct State {
    pub counter: u32,
}

#[derive(Debug, Error)]
pub enum EchoError {
    #[error("unknown error")]
    Unknown,
    #[error("std::io::Error")]
    StdErr(#[from] std::io::Error),
}

#[async_trait]
impl Handler<Echo, Message, State, Response, EchoError> for Echo {
    async fn receive(&self, ctx: Arc<Context<Echo, Message, State, Response, EchoError>>) -> Result<Response, EchoError> {
        match ctx.mgs {
            Message::Ping => {
                println!("Received Ping");
                let mut state_lock = ctx.state.lock().await;
                state_lock.counter += 1;
                if state_lock.counter > 10 {
                    Err(EchoError::Unknown)
                } else {
                    Ok(Response::Pong{counter: state_lock.counter})
                }
            }
        }
    }
}

main.rs

#[tokio::main]
async fn main() -> Result<(), EchoError> {
    let state = State {
        counter: 0,
    };

    let echo_ref = ActorRef::new("echo".to_string(), Echo{},  state, 100000).await?;

    println!("Sent Ping");
    echo_ref.send(Message::Ping).await?;

    println!("Sent Ping and ask response");
    let pong = echo_ref.ask(Message::Ping).await?;
    println!("Got {:?}", pong);

    _ = echo_ref.stop();
    tokio::time::sleep(Duration::from_millis(1000)).await;
    Ok(())
}

Example output:

Sent Ping
Sent Ping and ask response
Received Ping
Received Ping
Got Pong { counter: 2 }

Example sources: https://github.com/evgenyigumnov/actor-lib/tree/main/test

Dependencies

~3–10MB
~93K SLoC