目录

MoonNebula (星云)

MoonNebula is a high-performance, strongly typed Actor concurrency runtime and ecosystem framework built natively on moonbitlang/async for the MoonBit ecosystem. It draws inspiration from Erlang/OTP and Rust’s Actix, providing a highly scalable, fault-tolerant, and lock-free programming model.

Architecture Overview

                     +---------------------------------------+
                     |             System Context            |
                     +---------------------------------------+
                                         |
                                         v
                      +-------------------------------------+
                      |           Supervisor Actor          |
                      +-------------------------------------+
                                 | (supervises/restarts)
                                 v
        +-------------------------------------------------------------+
        |                                                             |
        v                                                             v
+---------------+  (registers)   +---------------+  (resolves)   +---------------+
|  Actor Pool   | -------------> | Name Registry | <------------ | Client Actor  |
+---------------+                +---------------+               +---------------+
| Worker Actor  |                                                | SendAfter msg |
| Worker Actor  | <============================================= | (via Timer)   |
+---------------+                    (message)                   +---------------+

Features

  • Async Message-Driven: Build scalable programs without callback hell using pure lock-free async channels.
  • Strong Isolation: Memory state is fully isolated inside individual Actors, eliminating data races.
  • Supervision Trees: Built-in Supervisor actors monitoring children and executing OneForOne self-healing strategies.
  • Routing & Load Balancing: Load balance CPU-heavy tasks across worker pools using RoundRobin and Random Router actors.
  • Dynamic Name Registry: Decouple actors via a concurrent request-response Name Registry.
  • Cron-like Timer Wheel: Centralized Scheduler Actor managing leak-free interval and delayed message delivery.

Quick Start

1. Define an Actor

let worker = Actor::new(
  started=fn(ctx) {
    println("Actor started!")
  },
  handle=fn(ctx, msg : Int) {
    println("Received: " + msg.to_string())
    if msg == 0 {
      ctx.stop() // Gracefully shutdown
    }
  },
  stopped=fn(ctx) {
    println("Actor stopped!")
  }
)

2. Spawn and Send Messages

run_system(async fn(sys) {
  let addr = sys.spawn(worker)
  
  // Non-blocking send
  let _ = addr.try_send(42)
  
  // Wait a bit
  @async.sleep(10)
  
  // Graceful shutdown
  let _ = addr.try_send(0)
})

Running Tests

Verify the concurrent runtime and all ecosystem components:

moon test --target js
关于

星云 (MoonNebula) 是一款原生基于 moonbitlang/async 构建的高性能、强类型的 Actor 并发框架。它借鉴了 Erlang/OTP 和 Rust Actix 的成熟设计哲学,为 MoonBit 开发者提供了一套开箱即用的无锁并发模型。星云允许开发者将状态隔离在独立的 Actor 实体中,通过异步的消息邮箱 (Mailbox) 进行通信。框架内置了生命周期管理和基础的监

53.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号