目录

MoonLimit

MoonLimit is a composable rate limiting toolkit for MoonBit. It provides pure algorithmic limiters that can be embedded in CLIs, async services, task queues, API gateways, and tests without depending on a specific runtime.

Algorithms

  • Token bucket for burst-friendly quotas.
  • Leaky bucket for smooth draining.
  • Fixed window counters for simple quotas.
  • Sliding window counters for reduced boundary spikes.
  • Sliding log for exact timestamp-based windows.
  • GCRA for precise arrival-time based throttling.
  • Warmup token bucket for gradual rollout.
  • Quota window for long-period product or billing limits.
  • Concurrency limiter for in-flight work protection.
  • Keyed limiter wrappers for per-user or per-resource limits.
  • Policy engine for all-of / any-of rule composition.
  • Workload simulation and cookbook recipes for demos and tuning.

Example

fn main {
  let limiter = @moonlimit.TokenBucket::new(2, 1, 1000)
  let first = limiter.allow_at(0)
  let second = limiter.allow_at(0)
  let third = limiter.allow_at(0)

  inspect(first.is_allowed(), content="true")
  inspect(second.is_allowed(), content="true")
  inspect(third.retry_after_ms(), content="1000")
}

Public API Shape

Limiters return Decision values:

  • Allowed({ remaining, reset_after_ms })
  • Rejected({ retry_after_ms, reason })

Callers pass monotonic milliseconds into allow_at, so the algorithms are deterministic in tests and portable across MoonBit backends.

Non-goals

MoonLimit is not a web framework, API gateway, Redis integration, or distributed quota service. It intentionally stays focused on the reusable rate limiting core.

Development

$env:MOONBIT_INSTALL_VERSION = "nightly"
irm https://cli.moonbitlang.com/install/powershell.ps1 | iex
moon check --deny-warn
moon fmt --check
moon info
git diff --exit-code -- pkg.generated.mbti cmd/main/pkg.generated.mbti
moon test --deny-warn
moon run cmd/main

The CI names the required pre-acceptance flows as moon check, moon fmt --deny-warn, moon info --deny-warn, and moon test. Current public MoonBit nightly builds support --deny-warn for check and test; for fmt and info, CI uses the strict equivalent supported by the toolchain: moon fmt --check and moon info followed by a generated-interface diff check.

Current Contest Readiness

  • MoonBit source scale: over 4k effective nonblank, noncomment lines.
  • Test suite: 245 tests covering algorithms, keyed variants, policy engines, configuration parsing, workloads, cookbook recipes, and regression matrices.
  • CI: GitHub Actions runs check, format, generated-interface, test, and CLI demo flows.
  • License: Apache-2.0.

This project is being built for the MoonBit open source contest. The goal is a small, well-tested, reusable infrastructure library rather than a full gateway or distributed rate limiting service.

关于

Composable rate limiting algorithms, policy engine, simulation tools, and cookbook recipes for MoonBit applications.

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

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