mcl_core_guard_limits (mcl_core v1.0.0)

View Source

Per-procedure inbound limits + envelope, in persistent_term.

THE DATA SHAPE OF THE GUARD (mcl-om#13): every capability that flows through mcl_core_guard_pipeline has ONE entry here, keyed by its org-qualified procedure:

- declared: the limits the capability itself declares (its 'limits' key merged over the framework defaults), recomputed on every advertise tick; - overrides: runtime changes, from 'limits.set' (guardian tier, inside the envelope) or limits.set_operator (human, anything); - limits: the effective set = merge(declared, overrides) -- what the stages enforce on every call; - envelope / 'declared_envelope': per-key min/max clamps. Only the operator tier may change them, and a guardian-tier set that a key''s envelope does not cover fails with envelope_exceeded -- no envelope, no guardian movement, by default.

WHY persistent_term: the stages read limits on EVERY inbound call, in the caller''s own process, and must not serialize through a gen_server; reads are shared and copy-free, writes are rare (a declare tick, or an explicit limits.set).

A redeclare at republish time recomputes limits as merge(declared', overrides), so a service upgrade re-declares its base without wiping the guardian''s runtime changes -- the tick must never undo what an operator or a guardian set.

Summary

Functions

The limits this node's config sets for one procedure: {mcl_core, [{inbound_guard, #{procedures => #{Proc => Limits}}}]}, where Limits holds limit keys and optionally an envelope. It is a startup, per-environment setting (mcl-echo#11): merged OVER the capability's own limits at every declare, so an operator can tighten or loosen a service's numbers without a rebuild, and still under the runtime overrides. #{} when the config names nothing for Proc.

Register (or re-register) a capability''s declared limits and envelope, from its capability map''s limits key, with this node's configured entry for it (configured/1) merged over them. A redeclare with the same base is a no-op -- runtime overrides survive. Raises {mcl_core_guard_bad_limits, Proc, Reason} on a bad map, so the advertise tick that carries it fails loud and keeps the prior registration.

The shipped framework defaults, per procedure. Deliberately roomier than the echo''s hand-rolled numbers: a capability that needs tighter bounds declares its own limits.

The framework defaults with the app env''s {mcl_core, [{inbound_guard, #{default_limits => ...}}]} merged over them. Validated at guard boot (mcl_core_guard:init/1); this is the read side.

The effective limits and envelope of one procedure. Falls back to the framework defaults (and no envelope) before the first declare.

The largest window length any declared procedure uses -- the sweep''s conservative cutoff horizon (mcl_core_guard:sweep_old_windows/0).

Every procedure with a declared entry -- the alert report walks these (mcl_core_guard:report_window_denials/1).

Drop all runtime overrides and the runtime envelope: back to the declared base (which the next advertise tick recomputes).

Apply an override set at a tier. guardian: within the envelope only, never the envelope itself. operator: anything valid, and may carry a new envelope. Returns the new effective pair, or the validation error; on error nothing changes.

The guard's config as a whole, checked at guard boot (mcl_core_guard:init/1): the framework defaults and every per-procedure entry. A bad entry stops the node before anything advertises itself as guarded, rather than surfacing at some later advertise tick.

Envelope validation: every clamp names a limit key, and carries positive min and max integers, min not above max.

Full-map validation: known keys, positive integers, and the per-caller budget at most the shared one.

Types

envelope/0

-type envelope() :: #{atom() => #{min := pos_integer(), max := pos_integer()}}.

limits/0

-type limits() ::
          #{max_payload_external_size := pos_integer(),
            window_ms := pos_integer(),
            per_caller_max := pos_integer(),
            global_max := pos_integer(),
            max_distinct_callers := pos_integer()}.

Functions

configured(Proc)

-spec configured(binary()) -> map().

The limits this node's config sets for one procedure: {mcl_core, [{inbound_guard, #{procedures => #{Proc => Limits}}}]}, where Limits holds limit keys and optionally an envelope. It is a startup, per-environment setting (mcl-echo#11): merged OVER the capability's own limits at every declare, so an operator can tighten or loosen a service's numbers without a rebuild, and still under the runtime overrides. #{} when the config names nothing for Proc.

declare(Proc, CapLimits)

-spec declare(binary(), map()) -> ok.

Register (or re-register) a capability''s declared limits and envelope, from its capability map''s limits key, with this node's configured entry for it (configured/1) merged over them. A redeclare with the same base is a no-op -- runtime overrides survive. Raises {mcl_core_guard_bad_limits, Proc, Reason} on a bad map, so the advertise tick that carries it fails loud and keeps the prior registration.

defaults()

-spec defaults() -> limits().

The shipped framework defaults, per procedure. Deliberately roomier than the echo''s hand-rolled numbers: a capability that needs tighter bounds declares its own limits.

effective_defaults()

-spec effective_defaults() -> limits().

The framework defaults with the app env''s {mcl_core, [{inbound_guard, #{default_limits => ...}}]} merged over them. Validated at guard boot (mcl_core_guard:init/1); this is the read side.

get(Proc)

-spec get(binary()) -> #{limits := limits(), envelope := envelope()}.

The effective limits and envelope of one procedure. Falls back to the framework defaults (and no envelope) before the first declare.

max_window_ms()

-spec max_window_ms() -> pos_integer().

The largest window length any declared procedure uses -- the sweep''s conservative cutoff horizon (mcl_core_guard:sweep_old_windows/0).

procedures()

-spec procedures() -> [binary()].

Every procedure with a declared entry -- the alert report walks these (mcl_core_guard:report_window_denials/1).

reset(Proc)

-spec reset(binary()) -> {ok, #{limits := limits(), envelope := envelope()}}.

Drop all runtime overrides and the runtime envelope: back to the declared base (which the next advertise tick recomputes).

set(Proc, Overrides, Tier)

-spec set(binary(), map(), guardian | operator) ->
             {ok, #{limits := limits(), envelope := envelope()}} | {error, term()}.

Apply an override set at a tier. guardian: within the envelope only, never the envelope itself. operator: anything valid, and may carry a new envelope. Returns the new effective pair, or the validation error; on error nothing changes.

validate_config()

-spec validate_config() -> ok | {error, term()}.

The guard's config as a whole, checked at guard boot (mcl_core_guard:init/1): the framework defaults and every per-procedure entry. A bad entry stops the node before anything advertises itself as guarded, rather than surfacing at some later advertise tick.

validate_envelope(Envelope)

-spec validate_envelope(map()) -> ok | {error, term()}.

Envelope validation: every clamp names a limit key, and carries positive min and max integers, min not above max.

validate_limits(Map)

-spec validate_limits(map()) -> ok | {error, term()}.

Full-map validation: known keys, positive integers, and the per-caller budget at most the shared one.