fix(admission): honour the route's queueMs in the structural stage and close the test DB

- withChatAdmission passed CHAT_ADMISSION_QUEUE_MAX_MS to admitChatStructure
  even when the route set queueMs, so queueMs 0 could still wait and admit a
  busy request. The structure stage now gets the same wait as the byte stage
  (regression test: a lease freed during the wait is not taken).
- The structure test drains the call-log writes and resets the DB handle.
This commit is contained in:
serenity
2026-11-18 23:58:59 -03:00
committed by diegosouzapw
parent f3f689c269
commit 4e94e65c65
2 changed files with 39 additions and 2 deletions

View File

@@ -29,7 +29,11 @@ type RouteHandler = (request: Request, ...args: any[]) => Promise<Response> | Re
async function admitSmallBodyStructure(
request: Request,
lease: ChatAdmissionLease | null,
options: { controller?: ChatAdmissionController; heapPressureCheck?: () => boolean }
options: {
controller?: ChatAdmissionController;
heapPressureCheck?: () => boolean;
queueMs?: number;
}
) {
if (lease) return { admit: true as const, lease };
let parsed: unknown;
@@ -42,7 +46,8 @@ async function admitSmallBodyStructure(
controller: options.controller,
heapPressureCheck: options.heapPressureCheck,
sessionId: resolveSessionId(request),
queueMs: CHAT_ADMISSION_QUEUE_MAX_MS,
// The same wait the byte stage was given: queueMs 0 sheds a busy request at once.
queueMs: options.queueMs ?? CHAT_ADMISSION_QUEUE_MAX_MS,
signal: request.signal,
});
}

View File

@@ -9,6 +9,15 @@ import assert from "node:assert/strict";
const { ChatAdmissionController } =
await import("../../src/shared/middleware/chatBodyAdmission.ts");
const { withChatAdmission } = await import("../../src/shared/middleware/withChatAdmission.ts");
const core = await import("../../src/lib/db/core.ts");
const callLogs = await import("../../src/lib/usage/callLogs.ts");
// Admission reads the body-size settings (DB) and a rejection is logged as a call log: drain
// those writes, then close the DB handle, or the runner can hang on the open connection.
test.after(async () => {
await callLogs.closeCallLogSaves();
core.resetDbInstance();
});
function request(body: string): Request {
return new Request("http://x/v1/messages", {
@@ -50,6 +59,29 @@ test("a small but structurally heavy body is shed while heavyweight capacity is
held.release();
});
test("queueMs 0 is honoured by the structure stage: a lease freed during the wait is not taken", async () => {
const { controller, held } = busyController();
let called = false;
const wrapped = withChatAdmission(
async () => {
called = true;
return new Response("ok");
},
{ controller, queueMs: 0, heapPressureCheck: () => true }
);
// Free the busy lease shortly after the request arrives: a stage that waits (the default
// queue time) would take it and admit; a zero-wait stage has already shed the request.
const timer = setTimeout(() => held.release(), 50);
try {
const res = await wrapped(request(heavyBody()));
assert.equal(res.status, 503);
assert.equal(called, false);
} finally {
clearTimeout(timer);
if (controller.activeHeavy > 0) held.release();
}
});
test("a small structurally heavy body holds a lease while the handler runs, then releases it", async () => {
const controller = new ChatAdmissionController(1, undefined, 0);
let activeDuringHandler = -1;