Actix: The Actor Model, Addr & Messages
Actix is a Rust actor framework implementing the actor model of concurrency -- independent actors communicating exclusively via message passing, each processing one message at a time. Actix Web, the popular web framework, was originally built on top of this actor system.
Defining an Actor
struct ChatRoom {
participants: Vec<Addr<Session>>,
}
impl Actor for ChatRoom {
type Context = Context<Self>;
}
struct Join(Addr<Session>);
impl Message for Join {
type Result = ();
}
impl Handler<Join> for ChatRoom {
type Result = ();
fn handle(&mut self, msg: Join, _ctx: &mut Context<Self>) {
self.participants.push(msg.0);
}
}Why the Actor Model Avoids Data Races
An actor's fields are private to that actor instance -- other code can't reach in and mutate them directly. Since each actor processes messages sequentially, there's no need for locks around shared mutable state accessed concurrently.
send() vs. do_send()
let result = room_addr.send(Join(session_addr)).await; // awaitable, expects a response
room_addr.do_send(Join(session_addr)); // fire-and-forgetCompile-Time Message Safety
Because messages are strongly-typed Rust structs, the compiler verifies an actor's Handler implementation matches the message type it claims to handle -- catching mismatches at compile time rather than at runtime.
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free