release: promote 26.8.0-beta.1 — the trust release #43
No files matched your search
+15
@@ -60,6 +60,21 @@ export class Bus {
|
||||
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) ?? []) {
|
||||
|
||||
@@ -47,6 +47,13 @@ export function correlateRequest(headers: Record<string, string | string[] | und
|
||||
const incoming = first(headers['x-request-id'])?.trim();
|
||||
const traceId = first(headers.traceparent)?.trim();
|
||||
|
||||
// Eager, and `randomUUID` rather than something cheaper, both deliberately. A lazy getter is
|
||||
// defeated the moment anything spreads this object — which the adapters and the payload builders
|
||||
// both do — so it would buy nothing without threading a nested correlation object through the
|
||||
// whole request path. And a boot-prefix counter measured 5x cheaper (14.7ns vs 77.1ns) but
|
||||
// trades the standard identifier shape for ~1.7% of a real request. Neither is worth its
|
||||
// complexity until a profile says otherwise; the expensive part was building payloads for
|
||||
// nobody, and `Bus.hasListeners` is what fixed that.
|
||||
return { requestId: incoming || crypto.randomUUID(), traceId: traceId || undefined };
|
||||
}
|
||||
|
||||
|
||||
+27
-14
@@ -57,10 +57,17 @@ export async function runSteps(
|
||||
// N steps need N+1 readings rather than 2N. Measured at 25.2ns vs 45.3ns per step, against a
|
||||
// ~10.19us request — 0.25%, below the 0.3% run-to-run CV of the project's own benchmark, which
|
||||
// is why this is always on instead of hiding behind a flag nobody could tune with evidence.
|
||||
let mark = performance.now();
|
||||
// Hoisted out of the loop: listeners are registered at boot, so re-deciding this per step buys
|
||||
// nothing. When nothing is listening — the common case — the whole block below costs a branch
|
||||
// instead of building four payloads and reading three clocks for an audience of nobody.
|
||||
const watchingEnter = bus.hasListeners('request:step:enter');
|
||||
const watchingLeave = bus.hasListeners('request:step:leave');
|
||||
const watchingError = bus.hasListeners('request:step:error');
|
||||
const timing = watchingLeave || watchingError;
|
||||
let mark = timing ? performance.now() : 0;
|
||||
|
||||
for (const step of steps) {
|
||||
bus.emit('request:step:enter', { name: step.name, scope: step.origin, ...correlation });
|
||||
if (watchingEnter) bus.emit('request:step:enter', { name: step.name, scope: step.origin, ...correlation });
|
||||
|
||||
try {
|
||||
const output = await step.run(context);
|
||||
@@ -69,20 +76,25 @@ export async function runSteps(
|
||||
// Emitted here, where the step that failed is still known. The outer catch in runPipeline
|
||||
// only sees "something in the pipeline threw" and used to report `name: 'pipeline'`, which
|
||||
// threw away the one fact the event exists to carry. Rethrown unchanged — this observes.
|
||||
const now = performance.now();
|
||||
bus.emit('request:step:error', {
|
||||
name: step.name,
|
||||
scope: step.origin,
|
||||
error,
|
||||
durationMs: now - mark,
|
||||
...correlation,
|
||||
});
|
||||
if (watchingError) {
|
||||
bus.emit('request:step:error', {
|
||||
name: step.name,
|
||||
scope: step.origin,
|
||||
error,
|
||||
durationMs: performance.now() - mark,
|
||||
...correlation,
|
||||
});
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
const now = performance.now();
|
||||
bus.emit('request:step:leave', { name: step.name, scope: step.origin, durationMs: now - mark, ...correlation });
|
||||
mark = now;
|
||||
if (timing) {
|
||||
const now = performance.now();
|
||||
if (watchingLeave)
|
||||
bus.emit('request:step:leave', { name: step.name, scope: step.origin, durationMs: now - mark, ...correlation });
|
||||
mark = now;
|
||||
}
|
||||
}
|
||||
|
||||
return context;
|
||||
@@ -128,7 +140,8 @@ export async function runPipeline(args: {
|
||||
// Both fire for a failing step, deliberately: `request:step:error` above marks the span,
|
||||
// this marks the trace. A tracing exporter needs each, and only emitting the outer one is
|
||||
// what produces a trace that says a request failed without saying where.
|
||||
bus.emit('request:failed', { name: correlation.route ?? 'pipeline', error, ...correlation });
|
||||
if (bus.hasListeners('request:failed'))
|
||||
bus.emit('request:failed', { name: correlation.route ?? 'pipeline', error, ...correlation });
|
||||
const req = (context.req ?? {}) as { method?: string; url?: string; headers?: Record<string, unknown> };
|
||||
return renderError(
|
||||
error,
|
||||
|
||||
Reference in new issue
Block a user