Skip to content

Driving policies by hand

You can own the call

execute() is convenience. Every policy is a synchronous state machine, so you can drive it directly — from a stream consumer, a queue worker, or anywhere a promise wrapper is in the way:

ts
const gate = api.gate(req);
if (!gate.ok) throw new Error(`refused: ${gate.reason}`);

const started = performance.now();
try {
  const res = await fetch(req.url);
  gate.settle(res, performance.now() - started);
} catch (err) {
  gate.settle(err, performance.now() - started);
}

Why this is possible at all

A resilix policy is four synchronous methods — admit, settle, stats, snapshot. Only the executor knows about promises. That is what makes gate() a first-class way to use the library rather than an escape hatch, and it is why a policy decision is allocation-free on the hot path.

The reasoning, and what it costs, is in ADR-004.

MIT licensed. Zero runtime dependencies.