Skip to content

Commit 852a602

Browse files
committed
perf(gateway): stabilize parallel channel status probes
1 parent 564ba95 commit 852a602

2 files changed

Lines changed: 68 additions & 11 deletions

File tree

‎src/gateway/server-methods/channels.status.test.ts‎

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,47 @@ describe("channelsHandlers channels.status", () => {
249249
expect(probeArgs.cfg).toBe(autoEnabledConfig);
250250
});
251251

252+
it("runs channel probes concurrently and preserves deterministic status-map order", async () => {
253+
vi.useFakeTimers();
254+
try {
255+
const started: string[] = [];
256+
const createDelayedProbe = (id: string) => async () => {
257+
started.push(id);
258+
await new Promise((resolve) => {
259+
setTimeout(resolve, 1000);
260+
});
261+
return { ok: true };
262+
};
263+
configureAutoEnabledChannels([
264+
createChannelPlugin({ id: "zeta", probeAccount: createDelayedProbe("zeta") }),
265+
createChannelPlugin({ id: "alpha", probeAccount: createDelayedProbe("alpha") }),
266+
]);
267+
mocks.buildChannelUiCatalog.mockImplementation((plugins: Array<{ id: string }>) => ({
268+
order: plugins.map((plugin) => plugin.id),
269+
labels: {},
270+
detailLabels: {},
271+
systemImages: {},
272+
entries: {},
273+
}));
274+
const startedAt = Date.now();
275+
const run = runChannelsStatus({ probe: true, timeoutMs: 2000 });
276+
277+
await vi.advanceTimersByTimeAsync(0);
278+
expect(started).toEqual(["alpha", "zeta"]);
279+
await vi.advanceTimersByTimeAsync(1000);
280+
const payload = await run;
281+
282+
expect(Date.now() - startedAt).toBe(1000);
283+
expect(payload.channelOrder).toEqual(["zeta", "alpha"]);
284+
expect(Object.keys(requireRecord(payload.channels, "channels payload"))).toEqual([
285+
"alpha",
286+
"zeta",
287+
]);
288+
} finally {
289+
vi.useRealTimers();
290+
}
291+
});
292+
252293
it("filters channel status to a requested channel", async () => {
253294
const whatsappProbe = vi.fn(async () => ({ ok: true }));
254295
const imessageProbe = vi.fn(async () => ({ ok: true }));
@@ -314,13 +355,22 @@ describe("channelsHandlers channels.status", () => {
314355
expect(String(accountProbe.error)).toContain("probe failed");
315356
});
316357

317-
it("returns a partial snapshot when a channel probe exceeds the status budget", async () => {
358+
it("isolates a timed-out channel probe while another channel succeeds", async () => {
318359
vi.useFakeTimers();
319360
try {
320361
const autoEnabledConfig = { autoEnabled: true };
321-
const probeAccount = vi.fn(() => new Promise(() => {}));
362+
const hangingProbe = vi.fn(() => new Promise(() => {}));
363+
const healthyProbe = vi.fn(async () => ({ ok: true, identity: "healthy" }));
322364
mocks.applyPluginAutoEnable.mockReturnValue({ config: autoEnabledConfig, changes: [] });
323-
mocks.listChannelPlugins.mockReturnValue([createChannelPlugin({ probeAccount })]);
365+
mocks.listChannelPlugins.mockReturnValue([
366+
createChannelPlugin({ id: "hanging", probeAccount: hangingProbe }),
367+
createChannelPlugin({ id: "healthy", probeAccount: healthyProbe }),
368+
]);
369+
mocks.buildChannelAccountSnapshot.mockImplementation(async ({ accountId, probe }) => ({
370+
accountId,
371+
configured: true,
372+
probe,
373+
}));
324374
const respond = vi.fn();
325375
const run = expectDefined(
326376
channelsHandlers["channels.status"],
@@ -330,15 +380,18 @@ describe("channelsHandlers channels.status", () => {
330380
await vi.advanceTimersByTimeAsync(1000);
331381
await run;
332382

333-
const snapshotArgs = requireRecord(
334-
requireFirstCallArg(mocks.buildChannelAccountSnapshot),
335-
"snapshot args",
336-
);
337-
const probe = requireRecord(snapshotArgs.probe, "snapshot probe");
338-
expect(probe.timedOut).toBe(true);
339383
const payload = requireRespondPayload(respond);
384+
expect(
385+
requireRecord(firstChannelAccount(payload, "hanging").probe, "hanging probe").timedOut,
386+
).toBe(true);
387+
expect(requireRecord(firstChannelAccount(payload, "healthy").probe, "healthy probe")).toEqual(
388+
{
389+
ok: true,
390+
identity: "healthy",
391+
},
392+
);
340393
expect(payload.partial).toBe(true);
341-
expect(payload.warnings).toEqual(["whatsapp:default probe timed out after 1000ms"]);
394+
expect(payload.warnings).toEqual(["hanging:default probe timed out after 1000ms"]);
342395
} finally {
343396
vi.useRealTimers();
344397
}

‎src/gateway/server-methods/channels.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ export const channelsHandlers: GatewayRequestHandlers = {
361361
const selectedPlugins = requestedChannel
362362
? plugins.filter((plugin) => plugin.id === requestedChannel)
363363
: plugins;
364+
// Preserve registry-defined UI order while stabilizing keyed status maps for prompt-cache input.
365+
const statusPlugins = selectedPlugins.toSorted((left, right) =>
366+
left.id < right.id ? -1 : left.id > right.id ? 1 : 0,
367+
);
364368
if (rawChannel !== undefined && !requestedChannel) {
365369
respond(
366370
false,
@@ -545,7 +549,7 @@ export const channelsHandlers: GatewayRequestHandlers = {
545549
const accountsMap = payload.channelAccounts as Record<string, unknown>;
546550
const defaultAccountIdMap = payload.channelDefaultAccountId as Record<string, unknown>;
547551
const { results: channelResults } = await runTasksWithConcurrency({
548-
tasks: selectedPlugins.map((plugin) => async () => {
552+
tasks: statusPlugins.map((plugin) => async () => {
549553
const { accounts, defaultAccountId, defaultAccount, resolvedAccounts } =
550554
await buildChannelAccounts(plugin.id);
551555
const fallbackAccount =

0 commit comments

Comments
 (0)