A teapot that is thirty seconds behind and a teapot that does not exist look identical for the first thirty seconds, and only one of them should stop a deploy. bootTimeoutMs is that grace: retries with backoff until it passes. It still fails after that, deliberately. A provider the graph depends on is not optional, and booting without it would only move the failure to the first request, where it is a caller's 503 rather than the deploy's error. The full battery caught what the first cut got wrong: a bad secret was being retried. That is not a transient failure — it is the teapot's decision, and it will be the same decision in thirty seconds, so retrying only spends the deploy's patience to arrive at the identical error. Refusals now fail at once. Telling them apart without threading close codes through the neutral socket contract of four runtimes: a peer that accepted the connection and then hung up before answering rejected us on purpose; one that never accepted it may simply not be listening yet. That is exactly the distinction needed here. Retries are logged and emitted as mesh:boot:retry, kept separate from mesh:disconnect — that one is a link that was up and went away, this one never came up, and a deploy watching for trouble wants to tell those apart. 437 vitest / 12 Deno / 7 Bun / edge / JSR dry run. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
99 lines
4.1 KiB
TypeScript
99 lines
4.1 KiB
TypeScript
/** Names of every lifecycle event the framework emits over the {@link Bus}. */
|
|
export type LifecycleEvent =
|
|
| 'boot:provider:start'
|
|
| 'boot:provider:ok'
|
|
| 'boot:provider:fail'
|
|
| 'request:step:enter'
|
|
| 'request:step:leave'
|
|
| 'request:step:error'
|
|
| 'stream:open'
|
|
| 'stream:close'
|
|
| 'stream:error'
|
|
| 'mesh:connect'
|
|
| 'mesh:disconnect'
|
|
| 'mesh:rpc:error'
|
|
// A boot-time connect attempt that failed and will be retried until `bootTimeoutMs` passes.
|
|
// Separate from `mesh:disconnect`, which is a link that was up and went away — this one never
|
|
// came up, and a deploy watching for trouble wants to tell those apart.
|
|
| 'mesh:boot:retry'
|
|
| 'plugin:mounted'
|
|
| 'request:start'
|
|
| 'request:end'
|
|
| 'request:failed'
|
|
| 'route:matched'
|
|
// Covers a 404 *and* a 405 — the report's `route.not_found` names one outcome and would
|
|
// misreport the other, and "no route ran" is the fact a consumer actually wants.
|
|
| 'route:unmatched';
|
|
|
|
/**
|
|
* Data carried by a lifecycle event: the subject's name plus optional scope, error, timing and
|
|
* the fields that say which request it belongs to.
|
|
*
|
|
* Everything past `name` is optional and stays that way. Boot and mesh events have no request to
|
|
* name, and requiring a shape they cannot fill would only mean inventing values for it.
|
|
*/
|
|
export interface EventPayload {
|
|
name: string;
|
|
scope?: string;
|
|
error?: unknown;
|
|
durationMs?: number;
|
|
/** Correlates every event of one request. Adopted from `x-request-id` when a gateway sent one. */
|
|
requestId?: string;
|
|
/** A `traceparent` header carried verbatim. Core parses nothing — that is the exporter's job. */
|
|
traceId?: string;
|
|
/** The matched *pattern* (`/users/:id`), never the concrete path — see {@link Correlation}. */
|
|
route?: string;
|
|
method?: string;
|
|
transport?: string;
|
|
status?: number;
|
|
}
|
|
|
|
/**
|
|
* The subset of {@link EventPayload} that identifies a request, spread into each of its events.
|
|
*
|
|
* `route` carries the matched pattern rather than the URL that arrived, and that is load-bearing
|
|
* rather than cosmetic: a metrics consumer that labels a counter with a concrete path gets one
|
|
* label per distinct URL, and unbounded label cardinality takes down the metrics backend rather
|
|
* than the application. Handing anyone that shape by default would be the framework's fault.
|
|
*/
|
|
export type Correlation = Pick<EventPayload, 'requestId' | 'traceId' | 'route' | 'method' | 'transport'>;
|
|
|
|
/** In-process pub/sub for framework lifecycle events; observer failures are swallowed so they never break the pipeline. */
|
|
export class Bus {
|
|
private readonly listeners = new Map<LifecycleEvent, Set<(p: EventPayload) => void>>();
|
|
|
|
/** Subscribe `listener` to `event`; returns an unsubscribe function. */
|
|
on(event: LifecycleEvent, listener: (p: EventPayload) => void): () => void {
|
|
const listenerSet = this.listeners.get(event) ?? new Set();
|
|
listenerSet.add(listener);
|
|
this.listeners.set(event, listenerSet);
|
|
return () => listenerSet.delete(listener);
|
|
}
|
|
|
|
/**
|
|
* Whether anything is listening to `event` — for skipping the *construction* of a payload nobody
|
|
* will read.
|
|
*
|
|
* Not a micro-optimisation looking for a problem. A correlated payload is built by spreading the
|
|
* request's identity into it, and that spread was measured at ~197 ns per request on a two-step
|
|
* pipeline — against a ~4.5 µs in-process request, on a path where the overwhelmingly common
|
|
* case is an application that subscribes to nothing at all. `emit` cannot help: its argument is
|
|
* already built by the time it is called. Only the caller can decline to build it.
|
|
*/
|
|
hasListeners(event: LifecycleEvent): boolean {
|
|
const listenerSet = this.listeners.get(event);
|
|
return listenerSet !== undefined && listenerSet.size > 0;
|
|
}
|
|
|
|
/** Dispatch `payload` to every listener of `event`; a throwing listener is isolated and does not affect the others. */
|
|
emit(event: LifecycleEvent, payload: EventPayload): void {
|
|
for (const listener of this.listeners.get(event) ?? []) {
|
|
try {
|
|
listener(payload);
|
|
} catch {
|
|
/* observers must never break the pipeline */
|
|
}
|
|
}
|
|
}
|
|
}
|