moon_ratelimit

    Deterministic rate-limiting and resilience primitives with verified decision laws

    rate-limiting
    token-bucket
    gcra
    resilience
    backoff
    Download zip
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    3 hours ago
    Downloads
    11

    No dependencies

    #Moon RateLimit

    Deterministic rate-limiting and resilience primitives, implemented in MoonBit with verified decision laws.

    #Implemented

    Token buckets with continuous refill and exact retry-after; GCRA stated as a theoretical-arrival-time invariant and proven equivalent to the bucket bitwise on dyadic schedules; fixed windows (with their boundary burst demonstrated, not hidden) and exact sliding windows over (now - window, now]; jittered exponential backoff in the three AWS shapes over a seeded SplitMix64, reproducible bit for bit. Every decision is a pure function of (state, now, weight): time is injected, never read.

    #Build and run

    Use MoonBit and Node.js.

    moon build --target js node _build/js/debug/build/cmd/main/main.js examples/token-bucket.json node _build/js/debug/build/cmd/main/main.js - < examples/gcra.json

    Pass - to read a UTF-8 JSON request from stdin (16 MiB cap). Success prints one JSON object; refusals print {"error": ...} and exit 1; usage mistakes exit 2. examples/ holds one runnable request per capability with pinned answers.

    #Library use

    import {
    "123123213weqw/moon_ratelimit" @ratelimit,
    "moonbitlang/core/json",
    }

    fn example(request : Json) -> Json raise {
    @ratelimit.execute(request)
    }

    execute(Json) -> Json raise is the standard boundary; run(Value) is the typed form. See docs/usage.md for every field.

    #Tests

    sh scripts/verify.sh # four backends, fixtures, oracle, stats SKIP_NATIVE=1 sh scripts/verify.sh

    19 MoonBit test declarations across four backends with --deny-warn; 8 CLI fixture cases; 4 pinned examples; an independent Python oracle that re-derives every answer (57 checks: fixtures, random dyadic schedules, bit-exact jitter tables); machine-checked statistics in PROJECT_STATS.md. See the project proposal (Chinese) and boundaries.

    InputError

    pub(all) suberror InputError {
    InputError(String)
    } derive(Eq,
    Debug
    )

    impl Show for InputError

    InputError::equal

    fn InputError::equal(InputError, InputError) -> Bool

    InputError::not_equal

    fn InputError::not_equal(x : InputError, y : InputError) -> Bool

    InputError::output

    fn InputError::output(self : InputError, logger : &Logger) -> Unit

    InputError::to_string

    fn InputError::to_string(self : InputError) -> String

    Value

    pub(all) enum Value {
    Null
    Bool(Bool)
    Number(Double)
    String(String)
    Array(Array[Value])
    Object(Map[String, Value])
    } derive(Eq,
    Debug
    )

    A JSON-shaped semantic value. Requests and reports travel as these; object key order is never significant, and numbers are finite doubles.

    Value::canonical

    fn Value::canonical(self : Value) -> String

    Character-lexical key order, so a report hashed or compared across backends and languages always spells the same.

    Value::equal

    fn Value::equal(Value, Value) -> Bool

    Value::not_equal

    fn Value::not_equal(x : Value, y : Value) -> Bool

    Value::to_json

    Value::to_repr

    arr

    fn arr(value : Value) -> Array[Value] raise InputError

    execute

    The standard JSON boundary: one request object in, one report object out. Failures raise InputError with a plain message.

    field

    fn field(value : Value, key : String) -> Value

    fields_only

    fn fields_only(value : Value, allowed : Array[String]) -> Unit raise InputError

    from_json

    fn from_json(value :
    Json
    ) -> Value

    get

    fn get(value : Value, key : String) -> Value

    has

    fn has(value : Value, key : String) -> Bool

    num

    fn num(value : Value) -> Double raise InputError

    obj

    fn obj(value : Value) -> Map[String, Value] raise InputError

    optional_num

    fn optional_num(value : Value, fallback : Double) -> Double raise InputError

    optional_str

    fn optional_str(value : Value, fallback : String) -> String raise InputError

    record

    fn record(fields : Array[(String, Value)]) -> Value

    required

    fn required(value : Value, key : String) -> Value raise InputError

    run

    fn run(request : Value) -> Value raise InputError

    The typed boundary over the same requests.

    str

    fn str(value : Value) -> String raise InputError

    strings

    fn strings(items : Array[String]) -> Value

    Powered by MoonBit

    Site sourceReport issueModulesSkillsBuild queueStatistics

    © 2026 mooncakes.io