-
Notifications
You must be signed in to change notification settings - Fork 328
fix(cloudflare): retry catalog load after failure #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BlackHole1
merged 2 commits into
oomol-lab:main
from
cyphercodes:fix/cloudflare-catalog-retry
Aug 2, 2026
+219
−18
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { IsolatePromiseCache } from "./isolate-promise-cache.ts"; | ||
|
|
||
| describe("IsolatePromiseCache", () => { | ||
| it("creates once per key and reuses the resolved promise", async () => { | ||
| const cache = new IsolatePromiseCache<string>(); | ||
| let creations = 0; | ||
| const create = async (): Promise<string> => { | ||
| creations++; | ||
| return "value"; | ||
| }; | ||
|
|
||
| await expect(cache.get("k", create)).resolves.toBe("value"); | ||
| await expect(cache.get("k", create)).resolves.toBe("value"); | ||
| expect(creations).toBe(1); | ||
| }); | ||
|
|
||
| it("shares one in-flight promise between concurrent callers", async () => { | ||
| const cache = new IsolatePromiseCache<string>(); | ||
| let creations = 0; | ||
| let release = (): void => {}; | ||
| const create = (): Promise<string> => { | ||
| creations++; | ||
| return new Promise<string>((resolve) => { | ||
| release = () => resolve("value"); | ||
| }); | ||
| }; | ||
|
|
||
| const first = cache.get("k", create); | ||
| const second = cache.get("k", create); | ||
| expect(first).toBe(second); | ||
| release(); | ||
| await expect(Promise.all([first, second])).resolves.toEqual(["value", "value"]); | ||
| expect(creations).toBe(1); | ||
| }); | ||
|
|
||
| it("evicts a rejected promise so the next caller retries", async () => { | ||
| const cache = new IsolatePromiseCache<string>(); | ||
| let attempts = 0; | ||
| const create = async (): Promise<string> => { | ||
| attempts++; | ||
| if (attempts === 1) { | ||
| throw new Error("transient"); | ||
| } | ||
| return "recovered"; | ||
| }; | ||
|
|
||
| await expect(cache.get("k", create)).rejects.toThrow("transient"); | ||
| await expect(cache.get("k", create)).resolves.toBe("recovered"); | ||
| expect(attempts).toBe(2); | ||
| }); | ||
|
|
||
| it("keeps rejecting concurrent callers of the failed attempt, then retries once", async () => { | ||
| const cache = new IsolatePromiseCache<string>(); | ||
| let attempts = 0; | ||
| const create = async (): Promise<string> => { | ||
| attempts++; | ||
| if (attempts === 1) { | ||
| throw new Error("transient"); | ||
| } | ||
| return "recovered"; | ||
| }; | ||
|
|
||
| const settled = await Promise.allSettled([cache.get("k", create), cache.get("k", create)]); | ||
| expect(settled.map((result) => result.status)).toEqual(["rejected", "rejected"]); | ||
| expect(attempts).toBe(1); | ||
| await expect(cache.get("k", create)).resolves.toBe("recovered"); | ||
| expect(attempts).toBe(2); | ||
| }); | ||
|
|
||
| it("replaces the slot when the key changes", async () => { | ||
| const cache = new IsolatePromiseCache<string>(); | ||
| const create = (value: string) => async (): Promise<string> => value; | ||
|
|
||
| await expect(cache.get("a", create("first"))).resolves.toBe("first"); | ||
| await expect(cache.get("b", create("second"))).resolves.toBe("second"); | ||
| await expect(cache.get("a", create("third"))).resolves.toBe("third"); | ||
| }); | ||
|
|
||
| it("does not evict a newer key when an older promise rejects afterwards", async () => { | ||
| const cache = new IsolatePromiseCache<string>(); | ||
| let failOld = (): void => {}; | ||
| const oldEntry = cache.get( | ||
| "old", | ||
| () => | ||
| new Promise<string>((_resolve, reject) => { | ||
| failOld = () => reject(new Error("stale")); | ||
| }), | ||
| ); | ||
| let newCreations = 0; | ||
| const createNew = async (): Promise<string> => { | ||
| newCreations++; | ||
| return "fresh"; | ||
| }; | ||
|
|
||
| await expect(cache.get("new", createNew)).resolves.toBe("fresh"); | ||
| failOld(); | ||
| await expect(oldEntry).rejects.toThrow("stale"); | ||
|
|
||
| // The rejection belongs to the replaced entry, so the live "new" slot must still be memoized. | ||
| await expect(cache.get("new", createNew)).resolves.toBe("fresh"); | ||
| expect(newCreations).toBe(1); | ||
| }); | ||
|
|
||
| it("does not report an unhandled rejection when nobody awaits the evicted promise", async () => { | ||
| const cache = new IsolatePromiseCache<string>(); | ||
| const unhandled: unknown[] = []; | ||
| const onUnhandled = (reason: unknown): void => { | ||
| unhandled.push(reason); | ||
| }; | ||
| process.on("unhandledRejection", onUnhandled); | ||
| try { | ||
| const rejected = cache.get("k", async () => { | ||
| throw new Error("transient"); | ||
| }); | ||
| await expect(rejected).rejects.toThrow("transient"); | ||
| await new Promise((resolve) => setImmediate(resolve)); | ||
| await new Promise((resolve) => setImmediate(resolve)); | ||
| expect(unhandled).toEqual([]); | ||
| } finally { | ||
| process.off("unhandledRejection", onUnhandled); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * A one-slot promise cache scoped to a Workers isolate. | ||
| * | ||
| * An isolate is reused across many requests, so boot work is memoized in module state. Memoizing a | ||
| * *rejected* promise turns one transient failure into sustained errors until the isolate is | ||
| * recycled, with no self-healing in between. Each slot therefore drops itself as soon as its | ||
| * promise rejects, so the next request retries, while concurrent callers still share the in-flight | ||
| * promise and do the work once. | ||
| */ | ||
| export class IsolatePromiseCache<T> { | ||
| private entry: IsolateCacheEntry<T> | undefined; | ||
|
|
||
| /** | ||
| * Return the cached promise for `key`, creating and memoizing it when the slot holds another key | ||
| * or is empty. `create` runs synchronously on a miss, so a burst of concurrent callers arriving | ||
| * before the first one settles all share the same promise. | ||
| */ | ||
| get(key: string, create: () => Promise<T>): Promise<T> { | ||
| if (this.entry?.key === key) { | ||
| return this.entry.value; | ||
| } | ||
|
|
||
| const entry: IsolateCacheEntry<T> = { key, value: create() }; | ||
| this.entry = entry; | ||
| // Evict only while this entry still owns the slot: a different key may have replaced it while | ||
| // the promise was in flight, and that newer entry must survive. | ||
| void entry.value.catch(() => { | ||
| if (this.entry === entry) { | ||
| this.entry = undefined; | ||
| } | ||
| }); | ||
| return entry.value; | ||
| } | ||
| } | ||
|
|
||
| interface IsolateCacheEntry<T> { | ||
| key: string; | ||
| value: Promise<T>; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.