feat: rate-limit and metrics plugins #2

Merged
chrnx merged 10 commits from feature/rate-limit-metrics into develop 2026-09-19 16:58:43 +00:00
Owner

Plan B2. Two packages: rate-limit proves a branded 429 and a timer released by onShutdown; metrics consumes the lifecycle stream and closes green-tea#68 (that issue lives in core, so this PR cannot close it automatically). Both private: true until B3.

rate-limit is not a copy. The extraction from patitas-peludas had to change in four places, three of them convention violations invisible from a diff:

  • import { HttpError } was a value import of core — rule 8, and the lint rule rejects it. TooManyRequests now carries the Symbol.for("green-tea.http-error") brand.
  • The plugin was still the old bare-function contract, now { name, mount }.
  • The node registered as rate-limit while providing rateLimit, so two instances shared a node name and rule 2 could not hold.
  • MemoryStore.hit() called evictExpired on every new or expired key — a full walk of the map, O(N²) over N callers, which a client rotating addresses drives on purpose. A timer armed on the first hit does the same work off the hot path.

The sweep timer is deliberately not unrefed, so that failing to release it is visible rather than silent. The exit test measures exactly that, and was verified to fail with onShutdown removed — a leak test that passes while leaking is worth nothing. One consequence the plan did not foresee: every test that builds an app has to close it, or the timer keeps the runner alive after the assertions pass. A withApp helper does that and says why.

metrics counts request:end and nothing else. A failed request emits three lifecycle events, and the obvious collector reports one request as two — the defect issue #64 was filed for. request:failed gets its own series. Labels come from route, bounded by the route table, never from name, which carries the arriving path and is bounded by nothing (#65).

Gates: lint 0 errors, format, complexity, typecheck, 54 Node tests, 31 on Deno, 31 on Bun, deno publish --dry-run clean across three packages, edge correctly reporting nothing to run.

Plan B2. Two packages: `rate-limit` proves a branded 429 and a timer released by `onShutdown`; `metrics` consumes the lifecycle stream and closes green-tea#68 (that issue lives in core, so this PR cannot close it automatically). Both `private: true` until B3. **rate-limit is not a copy.** The extraction from patitas-peludas had to change in four places, three of them convention violations invisible from a diff: - `import { HttpError }` was a **value** import of core — rule 8, and the lint rule rejects it. `TooManyRequests` now carries the `Symbol.for("green-tea.http-error")` brand. - The plugin was still the old bare-function contract, now `{ name, mount }`. - The node registered as `rate-limit` while providing `rateLimit`, so two instances shared a node name and rule 2 could not hold. - `MemoryStore.hit()` called `evictExpired` on every new or expired key — a full walk of the map, O(N²) over N callers, which a client rotating addresses drives on purpose. A timer armed on the first hit does the same work off the hot path. The sweep timer is deliberately **not** `unref`ed, so that failing to release it is visible rather than silent. The exit test measures exactly that, and was verified to fail with `onShutdown` removed — a leak test that passes while leaking is worth nothing. One consequence the plan did not foresee: every test that builds an app has to close it, or the timer keeps the runner alive after the assertions pass. A `withApp` helper does that and says why. **metrics counts `request:end` and nothing else.** A failed request emits three lifecycle events, and the obvious collector reports one request as two — the defect issue #64 was filed for. `request:failed` gets its own series. Labels come from `route`, bounded by the route table, never from `name`, which carries the arriving path and is bounded by nothing (#65). Gates: lint 0 errors, format, complexity, typecheck, **54 Node tests**, 31 on Deno, 31 on Bun, `deno publish --dry-run` clean across three packages, edge correctly reporting nothing to run.
chrnx added 9 commits 2026-09-18 18:32:33 +00:00
The exit test spawns a probe and asserts it dies on its own, and the contract
files run unchanged on three runtimes — so the argv has to be decided at run
time rather than written for Node and skipped elsewhere.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
No workerd: the default store counts per isolate, and an edge deployment has
many, so the limit it would enforce is not the one configured. The README says
so rather than leaving someone to find out.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
hit() called evictExpired on every new or expired key, which is a full walk of
the map — O(N^2) over N callers, and a client rotating addresses drives it on
purpose. A timer armed on the first hit does the same work off the hot path.

It is not unref'ed, deliberately: the timer holds the process open so that
failing to release it fails the exit test rather than leaking quietly.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
The original extended core's HttpError, which is a value import and makes the
published plugin need core at run time. The brand is a registry-wide symbol, so
core renders the 429 without this package depending on it, and it survives two
copies of core in one tree where instanceof would not.

The fields asserted here are exactly the ones HttpErrorLike declares. That core
really renders them is proven end to end in the limiter's own tests, which read
a 429 and its Retry-After back out of app.fetch.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Extracted from patitas-peludas onto the object contract. The node now takes the
name it provides — the original registered 'rate-limit' while providing
'rateLimit', so two instances shared a node name and rule 2 could not hold.

onShutdown closes the store this plugin built. One passed in owns its own
lifecycle, and is closed only if it offers a close().

Every test that builds an app closes it, through a helper that says why: the
sweep timer is deliberately not unref'ed, so an app left open keeps the test
runner alive after its assertions have passed. That is the same behaviour the
exit test measures, so these exercise the shutdown path in passing.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Rule 7 says nothing a plugin opens outlives close(), and the only portable probe
is a process: a leaked interval keeps all three runtimes alive, a cleared one
lets each exit in well under a second.

Verified the test fails with onShutdown removed — it reports the process still
alive after 2s — because a leak test that passes while leaking is worth nothing.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
The README carries the cardinality rule up front: label on route, which the
route table bounds, never on name, which nothing bounds. That is the mistake
issue #65 was filed for and the one a collector makes by default.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
A failed request emits three lifecycle events. request:end is the terminal and
universal one, so it is the request counter; request:failed is additional and
gets its own series. Counting both as requests is the double count issue #64
was filed for.

Labels come from route, which the route table bounds, never from name, which
carries the arriving path and is bounded by nothing — issue #65.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
feat(metrics): publish the collector and a transformer to serve it
CI / dco (pull_request) Successful in 5s
CI / build (pull_request) Failing after 8s
CI / runtimes (bun) (pull_request) Failing after 6s
CI / runtimes (deno) (pull_request) Failing after 7s
CI / runtimes (edge) (pull_request) Failing after 6s
de0081ddae
The transformer is exported instead of a route: the path, the auth and whether
the endpoint exists are the application's decisions, and a plugin that mounted
/metrics itself would be taking them.

No onShutdown — a bus subscription is an entry in a set and keeps nothing alive,
which is what rule 7 asks about. Closes green-tea#68.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx added 1 commit 2026-09-18 18:33:43 +00:00
build: record the two new packages in the lockfile
CI / dco (pull_request) Successful in 5s
CI / build (pull_request) Successful in 12s
CI / runtimes (bun) (pull_request) Successful in 11s
CI / runtimes (deno) (pull_request) Successful in 10s
CI / runtimes (edge) (pull_request) Successful in 10s
e1773ae1e0
npm ci verifies the lockfile against every workspace package.json and refuses to
install when they disagree, so adding rate-limit and metrics without refreshing
the lock failed all four CI jobs at their first step.

Nothing caught it locally: the contract tests import through relative paths
(../src/index.ts) rather than by package name, so the new workspaces never
needed to be linked into node_modules for the suite to pass. Verified now by
running npm ci itself rather than npm install.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx merged commit a7253196dc into develop 2026-09-19 16:58:43 +00:00
Sign in to join this conversation.