-
Notifications
You must be signed in to change notification settings - Fork 48
feat: basic in-memory de-duping revalidation queue #360
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
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
28279cb
feat: basic in-memory de-duping revalidation queue
james-elicx 73599ad
change to pr for package
james-elicx ea2c7a6
add tests and fix mistake
james-elicx 9367396
remove dependency on memory queue from kv cache
james-elicx 670ce55
Revert "remove dependency on memory queue from kv cache"
james-elicx 224d42c
remove dependency on memory queue from kv cache again
james-elicx cb23b57
move manifest retrievel to own util for mocking
james-elicx 6256150
get test to be reliable
james-elicx 534704e
configurable revalidation timeout
james-elicx 27c6b55
split up tests
james-elicx e134601
review comments
james-elicx 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opennextjs/cloudflare": minor | ||
--- | ||
|
||
feat: basic in-memory de-duping revalidation queue |
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,73 @@ | ||
import { generateMessageGroupId } from "@opennextjs/aws/core/routing/queue.js"; | ||
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest"; | ||
|
||
import cache, { DEFAULT_REVALIDATION_TIMEOUT_MS } from "./memory-queue"; | ||
|
||
vi.mock("./.next/prerender-manifest.json", () => Promise.resolve({ preview: { previewModeId: "id" } })); | ||
|
||
describe("MemoryQueue", () => { | ||
beforeAll(() => { | ||
vi.useFakeTimers(); | ||
globalThis.internalFetch = vi.fn().mockReturnValue(new Promise((res) => setTimeout(() => res(true), 1))); | ||
}); | ||
|
||
afterEach(() => vi.clearAllMocks()); | ||
|
||
it("should process revalidations for a path", async () => { | ||
const firstRequest = cache.send({ | ||
MessageBody: { host: "test.local", url: "/test" }, | ||
MessageGroupId: generateMessageGroupId("/test"), | ||
MessageDeduplicationId: "", | ||
}); | ||
vi.advanceTimersByTime(DEFAULT_REVALIDATION_TIMEOUT_MS); | ||
await firstRequest; | ||
expect(globalThis.internalFetch).toHaveBeenCalledTimes(1); | ||
|
||
const secondRequest = cache.send({ | ||
MessageBody: { host: "test.local", url: "/test" }, | ||
MessageGroupId: generateMessageGroupId("/test"), | ||
MessageDeduplicationId: "", | ||
}); | ||
vi.advanceTimersByTime(1); | ||
await secondRequest; | ||
expect(globalThis.internalFetch).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should process revalidations for multiple paths", async () => { | ||
const firstRequest = cache.send({ | ||
MessageBody: { host: "test.local", url: "/test" }, | ||
MessageGroupId: generateMessageGroupId("/test"), | ||
MessageDeduplicationId: "", | ||
}); | ||
vi.advanceTimersByTime(1); | ||
await firstRequest; | ||
expect(globalThis.internalFetch).toHaveBeenCalledTimes(1); | ||
|
||
const secondRequest = cache.send({ | ||
MessageBody: { host: "test.local", url: "/test" }, | ||
MessageGroupId: generateMessageGroupId("/other"), | ||
MessageDeduplicationId: "", | ||
}); | ||
vi.advanceTimersByTime(1); | ||
await secondRequest; | ||
expect(globalThis.internalFetch).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should de-dupe revalidations", async () => { | ||
const requests = [ | ||
cache.send({ | ||
MessageBody: { host: "test.local", url: "/test" }, | ||
MessageGroupId: generateMessageGroupId("/test"), | ||
MessageDeduplicationId: "", | ||
}), | ||
cache.send({ | ||
MessageBody: { host: "test.local", url: "/test" }, | ||
MessageGroupId: generateMessageGroupId("/test"), | ||
MessageDeduplicationId: "", | ||
}), | ||
]; | ||
vi.advanceTimersByTime(1); | ||
await Promise.all(requests); | ||
expect(globalThis.internalFetch).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,49 @@ | ||
import logger from "@opennextjs/aws/logger.js"; | ||
import type { Queue, QueueMessage } from "@opennextjs/aws/types/overrides.js"; | ||
|
||
export const DEFAULT_REVALIDATION_TIMEOUT_MS = 10_000; | ||
|
||
/** | ||
* The Memory Queue offers basic ISR revalidation by directly requesting a revalidation of a route. | ||
* | ||
* It offers basic support for in-memory de-duping per isolate. | ||
*/ | ||
export class MemoryQueue implements Queue { | ||
readonly name = "memory-queue"; | ||
|
||
revalidatedPaths = new Map<string, ReturnType<typeof setTimeout>>(); | ||
|
||
constructor(private opts = { revalidationTimeoutMs: DEFAULT_REVALIDATION_TIMEOUT_MS }) {} | ||
|
||
async send({ MessageBody: { host, url }, MessageGroupId }: QueueMessage): Promise<void> { | ||
if (this.revalidatedPaths.has(MessageGroupId)) return; | ||
|
||
this.revalidatedPaths.set( | ||
MessageGroupId, | ||
// force remove to allow new revalidations incase something went wrong | ||
setTimeout(() => this.revalidatedPaths.delete(MessageGroupId), this.opts.revalidationTimeoutMs) | ||
); | ||
|
||
try { | ||
const protocol = host.includes("localhost") ? "http" : "https"; | ||
james-elicx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// TODO: Drop the import - https://github.com/opennextjs/opennextjs-cloudflare/issues/361 | ||
// @ts-ignore | ||
const manifest = await import("./.next/prerender-manifest.json"); | ||
await globalThis.internalFetch(`${protocol}://${host}${url}`, { | ||
method: "HEAD", | ||
headers: { | ||
"x-prerender-revalidate": manifest.preview.previewModeId, | ||
"x-isr": "1", | ||
}, | ||
}); | ||
} catch (e) { | ||
logger.error(e); | ||
} finally { | ||
clearTimeout(this.revalidatedPaths.get(MessageGroupId)); | ||
this.revalidatedPaths.delete(MessageGroupId); | ||
} | ||
} | ||
} | ||
|
||
export default new MemoryQueue(); |
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
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.