目录

MoonResilience

MoonResilience is a MoonBit resilience toolkit for service calls, job execution, message handling, and other infrastructure-facing workloads. It provides retry policies, circuit breaking, rate limiting, bulkhead isolation, and a unified execution entry for composing those controls.

The first version is intentionally kept as a pure MoonBit core library. It does not depend on any framework runtime, so it can be reused in CLI tools, backend components, scheduling logic, and teaching examples around stability control.

Why this project

Resilience controls are a common requirement in mature backend systems:

  • retry transient failures without hiding permanent faults
  • stop cascading failures with circuit breaking
  • smooth burst traffic with rate limiting
  • isolate expensive or unstable work with bulkheads

MoonResilience packages these controls as a small reusable MoonBit library instead of scattering ad hoc logic across projects.

Core capabilities

  • fixed and exponential backoff retry policies
  • circuit breaker with Closed, Open, and HalfOpen states
  • token-bucket style rate limiter
  • bulkhead isolation with explicit in-flight capacity
  • event recording hooks for retries, breaker transitions, limiter rejection, and bulkhead rejection
  • unified execute_with flow for composing the controls

Package layout

  • root package: @q2weasd/moonresilience
  • demo CLI: cmd/main
  • competition materials: docs/competition

Quick start

moon test
moon run cmd/main

Example

let policy = @moonresilience.default_policy()
let hooks = @moonresilience.empty_hooks()
let result = @moonresilience.execute_with(
  policy,
  0,
  hooks,
  fn(attempt : Int) -> @moonresilience.ActionOutcome[String] {
    if attempt < 3 {
      Failure({
        code: "upstream_timeout",
        message: "temporary timeout",
        retryable: true,
      })
    } else {
      Success("ok")
    }
  },
)

Current scope

The current version focuses on a stable core model:

  • synchronous execution model with explicit now_ms
  • deterministic tests without sleeping or external clocks
  • event recording through ResilienceHooks

Planned follow-up work includes metrics adapters, configuration loading, and integration wrappers for HTTP clients or job runners.

License

Apache-2.0

关于

面向 MoonBit 服务端、任务执行和基础工具场景的弹性治理基础库,提供统一的重试策略、熔断、限流、并发隔离和策略组合能力,可用于 HTTP 调用保护、消息消费控制、后台作业调度保护等场景。项目首版聚焦纯 MoonBit 核心能力和可运行示例,后续可继续扩展指标钩子、配置加载和框架适配。

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

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