Gateway: clamp probe timeout to timer-safe max

This commit is contained in:
ted 2026-03-17 14:15:16 -07:00 committed by Ayaan Zaidi
parent a00cfb6764
commit e7a45e58ba
No known key found for this signature in database
2 changed files with 26 additions and 16 deletions

View File

@ -40,9 +40,15 @@ vi.mock("./client.js", () => ({
GatewayClient: MockGatewayClient,
}));
const { probeGateway } = await import("./probe.js");
const { clampProbeTimeoutMs, probeGateway } = await import("./probe.js");
describe("probeGateway", () => {
it("clamps probe timeout to timer-safe bounds", () => {
expect(clampProbeTimeoutMs(1)).toBe(250);
expect(clampProbeTimeoutMs(2_000)).toBe(2_000);
expect(clampProbeTimeoutMs(3_000_000_000)).toBe(2_147_483_647);
});
it("connects with operator.read scope", async () => {
const result = await probeGateway({
url: "ws://127.0.0.1:18789",

View File

@ -29,6 +29,13 @@ export type GatewayProbeResult = {
configSnapshot: unknown;
};
export const MIN_PROBE_TIMEOUT_MS = 250;
export const MAX_TIMER_DELAY_MS = 2_147_483_647;
export function clampProbeTimeoutMs(timeoutMs: number): number {
return Math.min(MAX_TIMER_DELAY_MS, Math.max(MIN_PROBE_TIMEOUT_MS, timeoutMs));
}
export async function probeGateway(opts: {
url: string;
auth?: GatewayProbeAuth;
@ -144,21 +151,18 @@ export async function probeGateway(opts: {
},
});
const timer = setTimeout(
() => {
settle({
ok: false,
connectLatencyMs,
error: connectError ? `connect failed: ${connectError}` : "timeout",
close,
health: null,
status: null,
presence: null,
configSnapshot: null,
});
},
Math.max(250, opts.timeoutMs),
);
const timer = setTimeout(() => {
settle({
ok: false,
connectLatencyMs,
error: connectError ? `connect failed: ${connectError}` : "timeout",
close,
health: null,
status: null,
presence: null,
configSnapshot: null,
});
}, clampProbeTimeoutMs(opts.timeoutMs));
client.start();
});