Skip to content

Commit 95a686c

Browse files
fix(cli): catalog loads registry items a few at a time so a slow link no longer times them out (#5829)
* fix(cli): catalog loads registry items a few at a time so a slow link no longer times them out * chore(cli): keep the moved concurrency helper's doc to one line * chore(cli): wrap the concurrency helper's doc under the line limit
1 parent e9181d0 commit 95a686c

6 files changed

Lines changed: 96 additions & 55 deletions

File tree

‎packages/cli/src/commands/lambda/render-batch.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { CliRuntimeError } from "../../utils/commandResult.js";
66
import {
77
buildLambdaBatchRenderConfig,
88
parseBatchFile,
9-
runWithConcurrencyLimit,
109
type RenderBatchArgs,
1110
} from "./render-batch.js";
11+
import { runWithConcurrencyLimit } from "../../utils/concurrency.js";
1212

1313
let tmpDir: string;
1414

‎packages/cli/src/commands/lambda/render-batch.ts‎

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
} from "../../utils/variables.js";
4040
import { normalizeErrorMessage } from "../../utils/errorMessage.js";
4141
import { warnOnDimensionMismatch } from "./_dimensions.js";
42+
import { runWithConcurrencyLimit } from "../../utils/concurrency.js";
4243
import { requireStack } from "./state.js";
4344

4445
// Dynamic-import the SDK so tsup keeps it out of the static-import head of
@@ -451,38 +452,3 @@ export function parseBatchFile(path: string): Array<{ entry: BatchEntry; lineNum
451452
}
452453
return out;
453454
}
454-
455-
/**
456-
* Run `worker` against every input with at most `limit` concurrent
457-
* invocations. Preserves input order in the returned array; each output
458-
* is positional with its input.
459-
*
460-
* Implementation: index-cursor + N concurrent producers each picking the
461-
* next index until the input is drained. Uses `Promise.all` and
462-
* propagates the first worker rejection — partial-failure isolation is
463-
* the caller's responsibility (in this file, `startEntry` wraps the
464-
* per-entry render in try/catch so a single failure surfaces in the
465-
* manifest rather than aborting the batch).
466-
*
467-
* Exported so unit tests can pin the concurrency cap independently of
468-
* the AWS-dependent fan-out path.
469-
*/
470-
export async function runWithConcurrencyLimit<T, R>(
471-
inputs: readonly T[],
472-
limit: number,
473-
worker: (input: T, index: number) => Promise<R>,
474-
): Promise<R[]> {
475-
if (limit < 1) throw new Error(`runWithConcurrencyLimit: limit must be ≥ 1, got ${limit}`);
476-
const results = new Array<R>(inputs.length);
477-
let cursor = 0;
478-
const workerCount = Math.min(limit, inputs.length);
479-
await Promise.all(
480-
Array.from({ length: workerCount }, async () => {
481-
while (cursor < inputs.length) {
482-
const idx = cursor++;
483-
results[idx] = await worker(inputs[idx]!, idx);
484-
}
485-
}),
486-
);
487-
return results;
488-
}

‎packages/cli/src/registry/remote.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export async function fetchItemManifest(
151151
name: string,
152152
type: ItemType,
153153
baseUrl: string = DEFAULT_REGISTRY_URL,
154+
options?: { cacheOnly?: boolean },
154155
): Promise<RegistryItem> {
155156
if (!validRegistryName(name) || !Object.hasOwn(ITEM_TYPE_DIRS, type))
156157
throw new Error("Invalid registry item name or type");
@@ -159,7 +160,8 @@ export async function fetchItemManifest(
159160
const url = registryPathUrl(baseUrl, dir, name, "registry-item.json");
160161
const cacheFile = cachePath(baseUrl, `${dir}__${name}`);
161162
const cached = readCacheEntry(cacheFile, validate);
162-
if (cached && isFresh(cached)) return cached.data;
163+
if (cached && (isFresh(cached) || options?.cacheOnly)) return cached.data;
164+
if (options?.cacheOnly) throw new Error("not fetched after an earlier registry timeout");
163165

164166
try {
165167
const item = await fetchJson(url, validate);

‎packages/cli/src/registry/resolver.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,49 @@ describe("registry resolver", () => {
132132
});
133133
});
134134

135+
describe("loadAllItems on a slow registry", () => {
136+
const entries = Array.from({ length: 100 }, (_, i) => ({
137+
name: `item-${i}`,
138+
type: "hyperframes:block" as const,
139+
}));
140+
const itemName = (url: string) => /\/blocks\/([^/]+)\/registry-item\.json$/.exec(url)?.[1];
141+
142+
it("keeps at most 32 item fetches in flight", async () => {
143+
let inFlight = 0;
144+
let peak = 0;
145+
vi.stubGlobal(
146+
"fetch",
147+
vi.fn(async (url: string) => {
148+
inFlight += 1;
149+
peak = Math.max(peak, inFlight);
150+
await new Promise((resolve) => setImmediate(resolve));
151+
inFlight -= 1;
152+
return new Response(JSON.stringify(buildItem(itemName(url)!, "hyperframes:block")));
153+
}),
154+
);
155+
const items = await loadAllItems(entries, { baseUrl: uniqueBaseUrl() });
156+
expect(items).toHaveLength(100);
157+
expect(peak).toBe(32);
158+
});
159+
160+
it("stops fetching after a timeout and warns once for every skipped item", async () => {
161+
const fetchMock = vi.fn(async () => {
162+
throw new DOMException("The operation was aborted due to timeout", "TimeoutError");
163+
});
164+
vi.stubGlobal("fetch", fetchMock);
165+
const warnings: string[] = [];
166+
const items = await loadAllItems(entries, {
167+
baseUrl: uniqueBaseUrl(),
168+
onWarn: (m) => warnings.push(m),
169+
});
170+
expect(items).toEqual([]);
171+
expect(fetchMock).toHaveBeenCalledTimes(32);
172+
expect(warnings).toEqual([
173+
'skipped 100 items ("item-0", "item-1", "item-2", ...): TimeoutError: The operation was aborted due to timeout',
174+
]);
175+
});
176+
});
177+
135178
describe("resolveItem", () => {
136179
it("returns the full manifest for a known item", async () => {
137180
const baseUrl = uniqueBaseUrl();

‎packages/cli/src/registry/resolver.ts‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
import type { ItemType, RegistryItem, RegistryManifestEntry } from "@hyperframes/core";
88
import { fetchItemManifest, fetchRegistryManifest, DEFAULT_REGISTRY_URL } from "./remote.js";
9+
import { runWithConcurrencyLimit } from "../utils/concurrency.js";
910

1011
export interface ResolveOptions {
1112
baseUrl?: string;
1213
/** Bypass the 24h manifest cache and fetch fresh data from the registry. */
1314
skipCache?: boolean;
1415
/**
15-
* Called once per item that fails to load inside `loadAllItems`. Defaults
16-
* to writing a diagnostic line to stderr. Pass a quieter implementation
17-
* when rendering structured output (clack prompts, JSON, etc.).
16+
* Called once with a summary when items fail to load inside `loadAllItems`.
17+
* Defaults to writing a diagnostic line to stderr. Pass a quieter
18+
* implementation when rendering structured output (clack prompts, JSON, etc.).
1819
*/
1920
onWarn?: (message: string) => void;
2021
}
@@ -38,31 +39,39 @@ export async function listRegistryItems(
3839
return manifest.items.filter((item) => item.type === filter.type);
3940
}
4041

42+
// Hundreds of item manifests sharing one slow link all started their timeouts at once.
43+
const ITEM_FETCH_CONCURRENCY = 32;
44+
4145
/**
42-
* Load every item's full manifest in parallel. Used by the interactive init
43-
* picker to populate titles/descriptions for all examples at once. Items that
44-
* fail to load are skipped with a warning so one missing manifest doesn't
45-
* break the picker.
46+
* Load every item's full manifest, a bounded number at a time. Items that fail
47+
* to load are skipped with one summary warning so one missing manifest doesn't
48+
* break the picker; after a timeout the rest come from cache only.
4649
*/
4750
export async function loadAllItems(
4851
entries: RegistryManifestEntry[],
4952
options: ResolveOptions = {},
5053
): Promise<RegistryItem[]> {
5154
const baseUrl = options.baseUrl ?? DEFAULT_REGISTRY_URL;
5255
const warn = options.onWarn ?? defaultWarn;
53-
const results = await Promise.allSettled(
54-
entries.map((e) => fetchItemManifest(e.name, e.type, baseUrl)),
55-
);
56-
const items: RegistryItem[] = [];
57-
results.forEach((r, i) => {
58-
if (r.status === "fulfilled") {
59-
items.push(r.value);
60-
} else {
61-
const name = entries[i]?.name ?? "<unknown>";
62-
warn(`skipped item "${name}": ${String(r.reason)}`);
56+
const skipped: string[] = [];
57+
let firstReason = "";
58+
let timedOut = false;
59+
const loaded = await runWithConcurrencyLimit(entries, ITEM_FETCH_CONCURRENCY, async (e) => {
60+
try {
61+
return await fetchItemManifest(e.name, e.type, baseUrl, { cacheOnly: timedOut });
62+
} catch (err) {
63+
if (err instanceof Error && err.name === "TimeoutError") timedOut = true;
64+
skipped.push(`"${e.name}"`);
65+
firstReason ||= String(err);
66+
return undefined;
6367
}
6468
});
65-
return items;
69+
if (skipped.length === 1) warn(`skipped item ${skipped[0]}: ${firstReason}`);
70+
else if (skipped.length > 1)
71+
warn(
72+
`skipped ${skipped.length} items (${skipped.slice(0, 3).join(", ")}${skipped.length > 3 ? ", ..." : ""}): ${firstReason}`,
73+
);
74+
return loaded.filter((item): item is RegistryItem => item !== undefined);
6675
}
6776

6877
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** Runs `worker` over `inputs` with at most `limit` in flight. Results keep input order, and the
2+
* first rejection rejects the run. */
3+
export async function runWithConcurrencyLimit<T, R>(
4+
inputs: readonly T[],
5+
limit: number,
6+
worker: (input: T, index: number) => Promise<R>,
7+
): Promise<R[]> {
8+
if (limit < 1) throw new Error(`runWithConcurrencyLimit: limit must be ≥ 1, got ${limit}`);
9+
const results = new Array<R>(inputs.length);
10+
let cursor = 0;
11+
const workerCount = Math.min(limit, inputs.length);
12+
await Promise.all(
13+
Array.from({ length: workerCount }, async () => {
14+
while (cursor < inputs.length) {
15+
const idx = cursor++;
16+
results[idx] = await worker(inputs[idx]!, idx);
17+
}
18+
}),
19+
);
20+
return results;
21+
}

0 commit comments

Comments
 (0)