-
Notifications
You must be signed in to change notification settings - Fork 72
Durable Queue #459
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
Durable Queue #459
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bfa8cd3
initial implementation
conico974 e823060
Handle failure and add some test
conico974 eaf8bf5
Fix linting
conico974 5e28907
update aws dep
conico974 0a49f3c
add comment to env
conico974 e7de1bb
review fix
conico974 b18d4fe
review fix
conico974 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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { defineCloudflareConfig } from "@opennextjs/cloudflare"; | ||
import d1TagCache from "@opennextjs/cloudflare/d1-tag-cache"; | ||
import kvIncrementalCache from "@opennextjs/cloudflare/kv-cache"; | ||
import memoryQueue from "@opennextjs/cloudflare/memory-queue"; | ||
import doQueue from "@opennextjs/cloudflare/durable-queue"; | ||
|
||
export default defineCloudflareConfig({ | ||
incrementalCache: kvIncrementalCache, | ||
tagCache: d1TagCache, | ||
queue: memoryQueue, | ||
queue: doQueue, | ||
}); |
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
312 changes: 312 additions & 0 deletions
312
packages/cloudflare/src/api/durable-objects/queue.spec.ts
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,312 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { DurableObjectQueueHandler } from "./queue"; | ||
|
||
vi.mock("cloudflare:workers", () => ({ | ||
DurableObject: class { | ||
constructor( | ||
public ctx: DurableObjectState, | ||
public env: CloudflareEnv | ||
) {} | ||
}, | ||
})); | ||
|
||
const createDurableObjectQueue = ({ | ||
fetchDuration, | ||
statusCode, | ||
headers, | ||
}: { | ||
fetchDuration: number; | ||
statusCode?: number; | ||
headers?: Headers; | ||
}) => { | ||
const mockState = { | ||
waitUntil: vi.fn(), | ||
blockConcurrencyWhile: vi.fn().mockImplementation(async (fn) => fn()), | ||
storage: { | ||
setAlarm: vi.fn(), | ||
getAlarm: vi.fn(), | ||
}, | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return new DurableObjectQueueHandler(mockState as any, { | ||
NEXT_CACHE_REVALIDATION_WORKER: { | ||
fetch: vi.fn().mockReturnValue( | ||
new Promise<Response>((res) => | ||
setTimeout( | ||
() => | ||
res( | ||
new Response(null, { | ||
status: statusCode, | ||
headers: headers ?? new Headers([["x-nextjs-cache", "REVALIDATED"]]), | ||
}) | ||
), | ||
fetchDuration | ||
) | ||
) | ||
), | ||
connect: vi.fn(), | ||
}, | ||
}); | ||
}; | ||
|
||
const createMessage = (dedupId: string, lastModified = Date.now()) => ({ | ||
MessageBody: { host: "test.local", url: "/test", eTag: "test", lastModified }, | ||
MessageGroupId: "test.local/test", | ||
MessageDeduplicationId: dedupId, | ||
previewModeId: "test", | ||
}); | ||
|
||
describe("DurableObjectQueue", () => { | ||
describe("successful revalidation", () => { | ||
it("should process a single revalidation", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
const firstRequest = await queue.revalidate(createMessage("id")); | ||
expect(firstRequest).toBeUndefined(); | ||
expect(queue.ongoingRevalidations.size).toBe(1); | ||
expect(queue.ongoingRevalidations.has("id")).toBe(true); | ||
|
||
await queue.ongoingRevalidations.get("id"); | ||
|
||
expect(queue.ongoingRevalidations.size).toBe(0); | ||
expect(queue.ongoingRevalidations.has("id")).toBe(false); | ||
expect(queue.service.fetch).toHaveBeenCalledWith("https://test.local/test", { | ||
method: "HEAD", | ||
headers: { | ||
"x-prerender-revalidate": "test", | ||
"x-isr": "1", | ||
}, | ||
signal: expect.any(AbortSignal), | ||
}); | ||
}); | ||
|
||
it("should dedupe revalidations", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
await queue.revalidate(createMessage("id")); | ||
await queue.revalidate(createMessage("id")); | ||
expect(queue.ongoingRevalidations.size).toBe(1); | ||
expect(queue.ongoingRevalidations.has("id")).toBe(true); | ||
}); | ||
|
||
it("should block concurrency", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
await queue.revalidate(createMessage("id")); | ||
await queue.revalidate(createMessage("id2")); | ||
await queue.revalidate(createMessage("id3")); | ||
await queue.revalidate(createMessage("id4")); | ||
await queue.revalidate(createMessage("id5")); | ||
// the next one should block until one of the previous ones finishes | ||
const blockedReq = queue.revalidate(createMessage("id6")); | ||
|
||
expect(queue.ongoingRevalidations.size).toBe(queue.maxRevalidations); | ||
expect(queue.ongoingRevalidations.has("id6")).toBe(false); | ||
expect(Array.from(queue.ongoingRevalidations.keys())).toEqual(["id", "id2", "id3", "id4", "id5"]); | ||
|
||
// @ts-expect-error | ||
expect(queue.ctx.blockConcurrencyWhile).toHaveBeenCalledTimes(1); | ||
|
||
// Here we await the blocked request to ensure it's resolved | ||
await blockedReq; | ||
// We then need to await for the actual revalidation to finish | ||
await Promise.all(Array.from(queue.ongoingRevalidations.values())); | ||
expect(queue.ongoingRevalidations.size).toBe(0); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(6); | ||
}); | ||
}); | ||
|
||
describe("failed revalidation", () => { | ||
it("should not put it in failed state for an incorrect 200", async () => { | ||
const queue = createDurableObjectQueue({ | ||
fetchDuration: 10, | ||
statusCode: 200, | ||
headers: new Headers([["x-nextjs-cache", "MISS"]]), | ||
}); | ||
await queue.revalidate(createMessage("id")); | ||
|
||
await queue.ongoingRevalidations.get("id"); | ||
|
||
expect(queue.routeInFailedState.size).toBe(0); | ||
}); | ||
|
||
it("should not put it in failed state for a failed revalidation with 404", async () => { | ||
const queue = createDurableObjectQueue({ | ||
fetchDuration: 10, | ||
statusCode: 404, | ||
}); | ||
await queue.revalidate(createMessage("id")); | ||
|
||
await queue.ongoingRevalidations.get("id"); | ||
|
||
expect(queue.routeInFailedState.size).toBe(0); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(1); | ||
|
||
await queue.revalidate(createMessage("id")); | ||
|
||
expect(queue.routeInFailedState.size).toBe(0); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should put it in failed state if revalidation fails with 500", async () => { | ||
const queue = createDurableObjectQueue({ | ||
fetchDuration: 10, | ||
statusCode: 500, | ||
}); | ||
await queue.revalidate(createMessage("id")); | ||
|
||
await queue.ongoingRevalidations.get("id"); | ||
|
||
expect(queue.routeInFailedState.size).toBe(1); | ||
expect(queue.routeInFailedState.has("id")).toBe(true); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(1); | ||
|
||
await queue.revalidate(createMessage("id")); | ||
|
||
expect(queue.routeInFailedState.size).toBe(1); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should put it in failed state if revalidation fetch throw", async () => { | ||
const queue = createDurableObjectQueue({ | ||
fetchDuration: 10, | ||
}); | ||
// @ts-expect-error - This is mocked above | ||
queue.service.fetch.mockImplementationOnce(() => Promise.reject(new Error("fetch error"))); | ||
await queue.revalidate(createMessage("id")); | ||
|
||
await queue.ongoingRevalidations.get("id"); | ||
|
||
expect(queue.routeInFailedState.size).toBe(1); | ||
expect(queue.routeInFailedState.has("id")).toBe(true); | ||
expect(queue.ongoingRevalidations.size).toBe(0); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(1); | ||
|
||
await queue.revalidate(createMessage("id")); | ||
|
||
expect(queue.routeInFailedState.size).toBe(1); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe("addAlarm", () => { | ||
const getStorage = (queue: DurableObjectQueueHandler): DurableObjectStorage => { | ||
// @ts-expect-error - ctx is a protected field | ||
return queue.ctx.storage; | ||
}; | ||
|
||
it("should not add an alarm if there are no failed states", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
await queue.addAlarm(); | ||
expect(getStorage(queue).setAlarm).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should add an alarm if there are failed states", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
queue.routeInFailedState.set("id", { msg: createMessage("id"), retryCount: 0, nextAlarmMs: 1000 }); | ||
await queue.addAlarm(); | ||
expect(getStorage(queue).setAlarm).toHaveBeenCalledWith(1000); | ||
}); | ||
|
||
it("should not add an alarm if there is already an alarm set", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
queue.routeInFailedState.set("id", { msg: createMessage("id"), retryCount: 0, nextAlarmMs: 1000 }); | ||
// @ts-expect-error | ||
queue.ctx.storage.getAlarm.mockResolvedValueOnce(1000); | ||
await queue.addAlarm(); | ||
expect(getStorage(queue).setAlarm).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should set the alarm to the lowest nextAlarm", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
queue.routeInFailedState.set("id", { msg: createMessage("id"), retryCount: 0, nextAlarmMs: 1000 }); | ||
queue.routeInFailedState.set("id2", { msg: createMessage("id2"), retryCount: 0, nextAlarmMs: 500 }); | ||
await queue.addAlarm(); | ||
expect(getStorage(queue).setAlarm).toHaveBeenCalledWith(500); | ||
}); | ||
}); | ||
|
||
describe("addToFailedState", () => { | ||
it("should add a failed state", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
await queue.addToFailedState(createMessage("id")); | ||
expect(queue.routeInFailedState.size).toBe(1); | ||
expect(queue.routeInFailedState.has("id")).toBe(true); | ||
expect(queue.routeInFailedState.get("id")?.retryCount).toBe(1); | ||
}); | ||
|
||
it("should add a failed state with the correct nextAlarm", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
await queue.addToFailedState(createMessage("id")); | ||
expect(queue.routeInFailedState.get("id")?.nextAlarmMs).toBeGreaterThan(Date.now()); | ||
expect(queue.routeInFailedState.get("id")?.retryCount).toBe(1); | ||
}); | ||
|
||
it("should add a failed state with the correct nextAlarm for a retry", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
await queue.addToFailedState(createMessage("id")); | ||
await queue.addToFailedState(createMessage("id")); | ||
expect(queue.routeInFailedState.get("id")?.nextAlarmMs).toBeGreaterThan(Date.now()); | ||
expect(queue.routeInFailedState.get("id")?.retryCount).toBe(2); | ||
}); | ||
|
||
it("should not add a failed state if it has been retried 6 times", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
queue.routeInFailedState.set("id", { msg: createMessage("id"), retryCount: 6, nextAlarmMs: 1000 }); | ||
await queue.addToFailedState(createMessage("id")); | ||
expect(queue.routeInFailedState.size).toBe(0); | ||
}); | ||
}); | ||
|
||
describe("alarm", () => { | ||
it("should execute revalidations for expired events", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
queue.routeInFailedState.set("id", { | ||
msg: createMessage("id"), | ||
retryCount: 0, | ||
nextAlarmMs: Date.now() - 1000, | ||
}); | ||
queue.routeInFailedState.set("id2", { | ||
msg: createMessage("id2"), | ||
retryCount: 0, | ||
nextAlarmMs: Date.now() - 1000, | ||
}); | ||
await queue.alarm(); | ||
expect(queue.routeInFailedState.size).toBe(0); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should execute revalidations for the next event to retry", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
queue.routeInFailedState.set("id", { | ||
msg: createMessage("id"), | ||
retryCount: 0, | ||
nextAlarmMs: Date.now() + 1000, | ||
}); | ||
queue.routeInFailedState.set("id2", { | ||
msg: createMessage("id2"), | ||
retryCount: 0, | ||
nextAlarmMs: Date.now() + 500, | ||
}); | ||
await queue.alarm(); | ||
expect(queue.routeInFailedState.size).toBe(1); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(1); | ||
expect(queue.routeInFailedState.has("id2")).toBe(false); | ||
}); | ||
|
||
it("should execute revalidations for the next event to retry and expired events", async () => { | ||
const queue = createDurableObjectQueue({ fetchDuration: 10 }); | ||
queue.routeInFailedState.set("id", { | ||
msg: createMessage("id"), | ||
retryCount: 0, | ||
nextAlarmMs: Date.now() + 1000, | ||
}); | ||
queue.routeInFailedState.set("id2", { | ||
msg: createMessage("id2"), | ||
retryCount: 0, | ||
nextAlarmMs: Date.now() - 1000, | ||
}); | ||
await queue.alarm(); | ||
expect(queue.routeInFailedState.size).toBe(0); | ||
expect(queue.service.fetch).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
}); |
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.