feat(bus): guarantee that every request:end has a request:start
`request:start` had one emitter and `request:end` had three, so the pairing every
consumer would reasonably assume held by accident rather than by promise. The
`maxConcurrentRequests` merge added the first terminal path that breaks it: a shed
request emits `request:end` and nothing before it, because it never reaches `handle()`.
Both shed paths now emit the pair, through one shared `emitShedPair` rather than two
copies — the guarantee lives in one place, and the two adapters already had a duplicate
of this block between them.
## Why the pair rather than documenting `request:end` as standalone
Both were defensible and the alternative was seriously considered. It loses on one
concrete case: the obvious in-flight gauge.
bus.on('request:start', () => inFlight++);
bus.on('request:end', () => inFlight--);
Without the guarantee that drifts negative the moment a server sheds — under load, which
is exactly when an operator is reading it, and it fails by producing a plausible wrong
number rather than an error. Documenting "do not do that" asks every consumer to read
the warning; guaranteeing the pair makes the obvious code correct.
Emitting a `start` for a shed request is not a fabrication, which was the objection worth
answering: the request *did* arrive. The gate refused it before the router, and
`request:start` says a request arrived, not that it routed.
The cost of the guarantee is that it binds every future emitter, and that is real. It is
paid down by `test/lifecycle-pairing.test.ts`, which enumerates every response shape —
200, 404, 500, 413, 204 preflight, and 503 on both adapters — and asserts the pair and
the shared `requestId`. The constraint is now something CI enforces rather than something
a contributor has to remember. Verified non-vacuous: dropping the `request:start`
emission fails the two shed rows and nothing else.
Also writes down, next to `LifecycleEvent` where a consumer reads it, the half of this
that is not new: `request:end` is terminal and universal and is the one to count;
`request:failed` and `route:unmatched` describe the same request and are additional.
Closes #73.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+300
-29
No files matched your search
+22
-15
@@ -7,7 +7,15 @@ import { readBody as readBodyBytes, deriveSecure, deriveIp } from './request';
|
||||
import { pipeStream } from './stream';
|
||||
import { attachWs } from './ws';
|
||||
import { mergeInjectedHeaders } from './headers';
|
||||
import { handle, correlateRequest, computeInjected, type HandleResult, type Preflight } from './core';
|
||||
import {
|
||||
handle,
|
||||
correlateRequest,
|
||||
computeInjected,
|
||||
emitShedPair,
|
||||
watchingRequests,
|
||||
type HandleResult,
|
||||
type Preflight,
|
||||
} from './core';
|
||||
import type { BodyFailure, BodyReader } from './body';
|
||||
import type { RouteDef, WsRouteDef, MeshControl, HttpOptions } from './types';
|
||||
import { nodeRequire } from '../node-require';
|
||||
@@ -102,23 +110,22 @@ function createRequestHandler(cfg: HandlerConfig) {
|
||||
|
||||
let result: HandleResult | Preflight;
|
||||
let acquired = false;
|
||||
const watchingEnd = bus?.hasListeners('request:end') ?? false;
|
||||
const startedAt = watchingEnd ? performance.now() : 0;
|
||||
const watching = watchingRequests(bus);
|
||||
const startedAt = watching ? performance.now() : 0;
|
||||
|
||||
try {
|
||||
if (!gate.acquire()) {
|
||||
if (watchingEnd) {
|
||||
const method = req.method ?? 'GET';
|
||||
const url = req.url ?? '/';
|
||||
bus!.emit('request:end', {
|
||||
name: `${method} ${url}`,
|
||||
method,
|
||||
status: 503,
|
||||
durationMs: performance.now() - startedAt,
|
||||
requestId: correlation.requestId,
|
||||
traceId: correlation.traceId,
|
||||
});
|
||||
}
|
||||
if (watching)
|
||||
emitShedPair(
|
||||
bus,
|
||||
{
|
||||
method: req.method ?? 'GET',
|
||||
url: req.url ?? '/',
|
||||
requestId: correlation.requestId,
|
||||
traceId: correlation.traceId,
|
||||
},
|
||||
startedAt,
|
||||
);
|
||||
|
||||
res.writeHead(503, {
|
||||
'content-type': 'application/json',
|
||||
|
||||
Reference in new issue
Block a user