Implementation of a highly-scalable and ergonomic actor model for Rust
Axiom brings a highly-scalable actor model to the Rust language based on the many lessons learned over years of Actor model implementations in Akka and Erlang. Axiom is, however, not a direct re-implementation of either of the two aforementioned actor models but rather a new implementation deriving inspiration from the good parts of those projects.
Context
and
Message
to values. For closures-as-actors, wrap the body in an async
block. move |...| {...}
becomes |...| async move { ... }
. For regular function syntax, simply add async
in
front of fn
.move
may need to be different, depending on semantics. Values
cannot be moved out of the closure and into the async block.impl<F, S, R> Processor<S, R> for F where
S: Send + Sync,
R: Future<Output = AxiomResult<S>> + Send + 'static,
F: (FnMut(S, Context, Message) -> R) + Send + Sync + 'static {}
assert
s and panic
s will be caught
and converted, treated the same as errors. Errors should already be considered fatal, as
Actors should handle any errors in their own scope.start_on_launch
flag has been added to the ActorSystemConfig
struct. This
allows for an ActorSystem to be created without immediately starting it. See ActorSystem::start
for how to start an unstarted ActorSystem
.Status
to help with the return points in Actors. Each
variant has a corresponding function that takes the Actor's state. Ok(Status::Done)
is
instead Ok(Status::done(state))
.log
points have been added across the codebase.Release Notes for All Versions
An actor model is an architectural asynchronous programming paradigm characterized by the use of actors for all processing activities.
Actors have the following characteristics:
Note that within the language of Rust, rule five cannot be enforced by Rust but is a best practice which is important for developers creating actors based on Axiom. In Erlang and Elixir rule five cannot be violated because of the structure of the language but this also leads to performance limitations. It's better to allow internal mutable state and encourage the good practice of not sending mutable messages.
What is important to understand is that these rules combined together makes each actor operate like a micro-service in the memory space of the program using them. Since actor messages are immutable, actors can trade information safely and easily without copying large data structures.
Although programming in the actor model is quite an involved process you can get started with Axiom in only a few lines of code.
use axiom::prelude::*;
use std::sync::Arc;
use std::time::Duration;
let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2));
let aid = system
.spawn()
.with(
0 as usize,
|state: usize, _context: Context, _message: Message| async move {
Ok(Status::done(state))
}
)
.unwrap();
aid.send(Message::new(11)).unwrap();
// It is worth noting that you probably wouldn't just unwrap in real code but deal with
// the result as a panic in Axiom will take down a dispatcher thread and potentially
// hang the system.
// This will wrap the value `17` in a Message for you!
aid.send_new(17).unwrap();
// We can also create and send separately using just `send`, not `send_new`.
let message = Message::new(19);
aid.send(message).unwrap();
// Another neat capability is to send a message after some time has elapsed.
aid.send_after(Message::new(7), Duration::from_millis(10)).unwrap();
aid.send_new_after(7, Duration::from_millis(10)).unwrap();
This code creates an actor system, fetches a builder for an actor via the spawn()
method,
spawns an actor and finally sends the actor a message. Once the actor is done processing a
message it returns the new state of the actor and the status after handling this message. In
this case we didnt change the state so we just return it. Creating an Axiom actor is literally
that easy but there is a lot more functionality available as well.
Keep in mind that if you are capturing variables from the environment you will have to wrap
the async move {}
block in another block and then move your variables into the first block.
Please see the test cases for more examples of this.
If you want to create an actor with a struct that is simple as well. Let's create one that handles a couple of different message types:
use axiom::prelude::*;
use std::sync::Arc;
let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2));
struct Data {
value: i32,
}
impl Data {
fn handle_bool(mut self, message: bool) -> ActorResult<Self> {
if message {
self.value += 1;
} else {
self.value -= 1;
}
Ok(Status::done(self))
}
fn handle_i32(mut self, message: i32) -> ActorResult<Self> {
self.value += message;
Ok(Status::done(self))
}
async fn handle(mut self, _context: Context, message: Message) -> ActorResult<Self> {
if let Some(msg) = message.content_as::<bool>() {
self.handle_bool(*msg)
} else if let Some(msg) = message.content_as::<i32>() {
self.handle_i32(*msg)
} else {
panic!("Failed to dispatch properly");
}
}
}
let data = Data { value: 0 };
let aid = system.spawn().name("Fred").with(data, Data::handle).unwrap();
aid.send_new(11).unwrap();
aid.send_new(true).unwrap();
aid.send_new(false).unwrap();
This code creates a named actor out of an arbitrary struct. Since the only requirement to make
an actor is to have a function that is compliant with the [axiom::actors::Processor
] trait,
anything can be an actor. If this struct had been declared somewhere outside of your control you
could use it in an actor as state by declaring your own handler function and making the calls to
the 3rd party structure.
It's important to keep in mind that the starting state is moved into the actor and you will not
have external access to it afterwards. This is by design and although you could conceivably use
a [Arc
] or [Mutex
] enclosing a structure as state, that would definitely be a bad idea as it
would break the rules we laid out for actors.
There is a lot more to learn and explore and your best resource is the test code for Axiom. The developers have a belief that test code should be well architected and well commented to act as a set of examples for users of Axiom.
Based on previous experience with other actor models I wanted to design Axiom around some core principles: