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, GatewayClient: MockGatewayClient,
})); }));
const { probeGateway } = await import("./probe.js"); const { clampProbeTimeoutMs, probeGateway } = await import("./probe.js");
describe("probeGateway", () => { 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 () => { it("connects with operator.read scope", async () => {
const result = await probeGateway({ const result = await probeGateway({
url: "ws://127.0.0.1:18789", url: "ws://127.0.0.1:18789",

View File

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