Crate joerl

Crate joerl 

Source
Expand description

§joerl - An Erlang-inspired Actor Model Library for Rust

Named in tribute to Joe Armstrong, the creator of Erlang, joerl brings the power and elegance of the Erlang actor model to Rust using async/await.

§Features

  • Actor Model: Lightweight actors that communicate via message passing
  • Supervision Trees: Robust error handling with configurable restart strategies
  • Links and Monitors: Actor relationships for failure detection and propagation
  • Bounded Mailboxes: Backpressure support to prevent resource exhaustion
  • Async/Await: Built on tokio for excellent performance
  • Erlang Conventions: Familiar API for Erlang/OTP developers
  • Distributed Clustering: EPMD-based node discovery and location transparency

§Quick Start

use joerl::{Actor, ActorContext, ActorSystem, Message};
use async_trait::async_trait;

// Define your actor
struct Counter {
    count: i32,
}

#[async_trait]
impl Actor for Counter {
    async fn started(&mut self, ctx: &mut ActorContext) {
        println!("Counter started with pid {}", ctx.pid());
    }

    async fn handle_message(&mut self, msg: Message, ctx: &mut ActorContext) {
        if let Some(cmd) = msg.downcast_ref::<&str>() {
            match *cmd {
                "increment" => self.count += 1,
                "get" => println!("Count: {}", self.count),
                _ => {}
            }
        }
    }
}

#[tokio::main]
async fn main() {
    let system = ActorSystem::new();
    let counter = system.spawn(Counter { count: 0 });
     
    counter.send(Box::new("increment")).await.unwrap();
    counter.send(Box::new("get")).await.unwrap();
}

§Erlang Terminology Mapping

Erlangjoerl
spawn/1system.spawn(actor)
PidPid
send/2 (!)actor_ref.send(msg)
link/1actor_ref.link(from_pid)
monitor/2actor_ref.monitor(from_pid)
process_flag(trap_exit, true)ctx.trap_exit(true)
{'EXIT', Pid, Reason}Signal::Exit { from, reason }
{'DOWN', Ref, process, Pid, Reason}Signal::Down { reference, pid, reason }

§Architecture

The library is organized into several key modules:

  • actor: Core actor trait and context
  • system: Actor system runtime and registry
  • message: Message types and signals
  • mailbox: Bounded mailbox implementation
  • supervisor: Supervision trees and restart strategies
  • gen_server: Generic server behavior (Erlang’s gen_server)
  • gen_statem: Generic state machine behavior (Erlang’s gen_statem)
  • epmd: Node discovery and clustering (EPMD protocol)
  • distributed: Distributed actor systems with location transparency
  • error: Error types and results

§Panic Handling

joerl implements full Erlang/OTP-style panic handling. When an actor panics:

  • The panic is caught and converted to ExitReason::Panic
  • Linked actors receive EXIT signals
  • Monitoring actors receive DOWN signals
  • Supervisors automatically restart the panicked actor
  • Cleanup always happens (no resource leaks)

See the panic_handling example for a comprehensive demonstration.

Re-exports§

pub use actor::Actor;
pub use actor::ActorContext;
pub use error::ActorError;
pub use error::Result;
pub use gen_server::CallResponse;
pub use gen_server::ReplyHandle;
pub use health::HealthConfig;
pub use health::HealthIssue;
pub use health::HealthStatus;
pub use health::IssueSeverity;
pub use health::SystemHealth;
pub use message::ExitReason;
pub use message::Message;
pub use message::MonitorRef;
pub use message::Signal;
pub use pid::Pid;
pub use registry::Registry;
pub use scheduler::Destination;
pub use scheduler::TimerRef;
pub use serialization::get_global_registry;
pub use serialization::register_message_type;
pub use supervisor::RestartStrategy;
pub use supervisor::Supervisor;
pub use supervisor::SupervisorSpec;
pub use system::ActorRef;
pub use system::ActorSystem;
pub use async_trait;

Modules§

actor
Core actor trait and context.
distributed
Distributed actor system support.
epmd
EPMD (Erlang Port Mapper Daemon) equivalent for joerl.
error
Error types for the joerl actor system.
gen_server
Generic server behavior inspired by Erlang’s gen_server.
gen_statem
Generic state machine behavior inspired by Erlang’s gen_statem.
health
Health check system for monitoring actor system health.
mailbox
Mailbox implementation for actors.
message
Message types for inter-actor communication.
pid
Process identifier (Pid) for actors.
registry
Named process registry for Erlang-style process registration.
scheduler
Message scheduler for delayed and recurring message delivery.
serialization
Trait-based message serialization for distributed actor systems.
supervisor
Supervision trees for fault-tolerant systems.
system
Actor system for managing the lifecycle of actors.
telemetry
Telemetry and observability for joerl.

Macros§

impl_serializable
Helper macro to implement SerializableMessage with less boilerplate.

Attribute Macros§

gen_statem
Generate a GenStatem implementation from a Mermaid state diagram.