Sign in

    moonorbit

    A lightweight, high-performance Actor concurrency framework for MoonBit

    actor
    concurrency
    async
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    3 months ago
    Downloads
    43

    #Orbit (MoonBit 异步 Actor 并发框架)

    #简介

    Orbit 是一个专为 MoonBit 设计的轻量级、高性能 Actor 模型并发框架。它基于 moonbitlang/async 构建,旨在简化 MoonBit 中的并发编程,提供高容错、易扩展的状态管理和消息传递机制。

    #目标与方向

    • 方向: 系统能力与运行时框架 (System capabilities and runtime framework)。
    • 本项目不仅满足 MoonBit 2026 开源大赛的基本要求,还致力于成为 MoonBit 生态中不可或缺的并发基石,为构建高吞吐量、低延迟的服务器和并发系统提供原生支持。

    #核心特性

    • Actor 核心抽象: 使用函数式编程模式定义的行为 (Behavior),支持强类型的状态和消息流转。
    • Mailbox: 为每个 Actor 分配独立的消息队列。
    • ActorSystem: 提供轻量级的执行环境和系统级别的 Actor 注册与生命周期管理。
    • Supervision: 提供核心的监管树策略 (OneForOne, OneForAll) 实现高容错能力。
    • 调度机制: 将 Actor 执行流与 MoonBit 底层异步事件循环深度结合。

    #开发进度

    目前处于大赛 Phase 1 阶段,完成了项目的核心概念验证和底层抽象建立。

    #许可证

    Apache-2.0

    Behavior

    type Behavior[State, Msg] = (Context, State, Msg) -> State raise
    A Behavior defines how an actor reacts to messages and updates its state.

    DummyError

    type DummyError derive(
    Debug
    )

    ActorRef

    pub struct ActorRef[Msg] {
    id : Int
    mailbox :
    Queue
    [Msg]
    }
    A strongly typed reference to an actor.

    ActorRef::send

    fn[Msg] ActorRef::send(self : ActorRef[Msg], msg : Msg) -> Unit
    Sends a message to the actor.

    ActorSystem

    pub struct ActorSystem {
    name : String
    next_actor_id :
    Ref
    [Int]
    group :
    TaskGroup
    [Unit]
    is_terminated :
    Ref
    [Bool]
    }
    An ActorSystem manages the lifecycle and execution of actors.

    ActorSystem::new

    fn ActorSystem::new(name : String, group :
    TaskGroup
    [Unit]) -> ActorSystem
    Creates a new ActorSystem.

    ActorSystem::spawn

    fn[State, Msg] ActorSystem::spawn(self : ActorSystem, behavior : (Context, State, Msg) -> State raise, initial_state : State, supervisor? : Supervisor) -> ActorRef[Msg]
    Spawns a new actor with the given behavior and initial state.

    ActorSystem::terminate

    fn ActorSystem::terminate(self : ActorSystem) -> Unit
    Terminates the ActorSystem, stopping the scheduler.

    Context

    pub struct Context {
    system_id : Int
    actor_id : Int
    }
    Represents the execution context for an Actor.

    Context::new

    fn Context::new(system_id : Int, actor_id : Int) -> Context
    Creates a new execution context for an actor.

    Context::send

    fn[Msg] Context::send(_self : Context, target : ActorRef[Msg], msg : Msg) -> Unit
    Sends a message to the given actor reference.

    Mailbox

    pub struct Mailbox[Msg] {
    messages : Array[Msg]
    }
    A simple mailbox for actors to queue incoming messages.

    Mailbox::dequeue

    fn[Msg] Mailbox::dequeue(self : Mailbox[Msg]) -> Msg?

    Mailbox::enqueue

    fn[Msg] Mailbox::enqueue(self : Mailbox[Msg], msg : Msg) -> Unit

    Mailbox::new

    fn[Msg] Mailbox::new() -> Mailbox[Msg]
    Creates a new, empty Mailbox.

    PingPongMsg

    type PingPongMsg

    Scheduler

    pub struct Scheduler {
    system : ActorSystem
    }
    The Scheduler is responsible for executing actor behaviors and managing async tasks.

    Scheduler::new

    fn Scheduler::new(system : ActorSystem) -> Scheduler
    Creates a new Scheduler for the given ActorSystem.

    Scheduler::run

    async fn Scheduler::run(self : Scheduler) -> Unit
    Starts the scheduler event loop, running until the system is terminated.

    SupervisionStrategy

    pub enum SupervisionStrategy {
    OneForOne
    OneForAll
    RestForOne
    }
    Defines the strategy used by a supervisor when a child actor fails.

    Supervisor

    pub struct Supervisor {
    strategy : SupervisionStrategy
    max_retries : Int
    }
    A Supervisor manages a set of child actors and applies a supervision strategy upon failure.

    Supervisor::new

    fn Supervisor::new(strategy : SupervisionStrategy, max_retries : Int) -> Supervisor
    Creates a new Supervisor with the specified strategy and retry limit.

    ping_pong_behavior

    fn ping_pong_behavior(_context : Context, _state : Int, msg : PingPongMsg) -> Int raise
    A simple behavior that responds to Ping with Pong.