41

Rust Actor Runtime using async/await and tokio channels

 4 years ago
source link: https://github.com/LeonHartley/Coerce-rs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Coerce-rs

Coerce-rs is an asynchronous (async/await) Actor runtime for Rust. It allows for extremely simple yet powerful actor-based multithreaded application development.

async/await Actors

An actor is just another word for a unit of computation. It can have mutable state, it can receive messages and perform actions. One caveat though.. It can only do one thing at a time. This can be useful because it can alleviate the need for thread synchronisation, usually achieved by locking (using Mutex , RwLock etc).

How is this achieved in Coerce?

Coerce uses Tokio's MPSC channels ( tokio::sync::mpsc::channel ), every actor created spawns a task listening to messages from a Receiver , handling and awaiting the result of the message. Every reference ( ActorRef<A: Actor> ) holds a Sender<M> where A: Handler<M> , which can be cloned.

Actors can be stopped and actor references can be retrieved by ID (currently Uuid ) from anywhere in your application. Actors are automatically dropped (and Stopped ) when all references are dropped.

Example

pub struct EchoActor {}

#[async_trait]
impl Actor for EchoActor {}

pub struct EchoMessage(String);

impl Message for EchoMessage {
    type Result = String;
}

#[async_trait]
impl Handler<EchoMessage> for EchoActor {
    async fn handle(
        &mut self,
        message: EchoMessage,
        _ctx: &mut ActorHandlerContext,
    ) -> String {
        message.0.clone()
    }
}

pub async fn run() {
    let mut actor = new_actor(EchoActor {}).await.unwrap();

    let hello_world = "hello, world".to_string();
    let result = actor.send(EchoMessage(hello_world.clone())).await;
    
    assert_eq!(result, Ok(hello_world));
}

Distributed Actors currently in development


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK