-
Notifications
You must be signed in to change notification settings - Fork 17.1k
feat(core): add fence to make all methods strongly consistent when syncing #22679
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
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| import { EventEmitter } from "events" | ||
|
|
||
| export type GlobalEvent = { | ||
| directory?: string | ||
| project?: string | ||
| workspace?: string | ||
| payload: any | ||
| } | ||
|
|
||
| export const GlobalBus = new EventEmitter<{ | ||
| event: [ | ||
| { | ||
| directory?: string | ||
| project?: string | ||
| workspace?: string | ||
| payload: any | ||
| }, | ||
| ] | ||
| event: [GlobalEvent] | ||
| }>() |
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,37 @@ | ||
| import { GlobalBus, type GlobalEvent } from "@/bus/global" | ||
|
|
||
| export function waitEvent(input: { timeout: number; signal?: AbortSignal; fn: (event: GlobalEvent) => boolean }) { | ||
| if (input.signal?.aborted) return Promise.reject(input.signal.reason ?? new Error("Request aborted")) | ||
|
|
||
| return new Promise<void>((resolve, reject) => { | ||
| const abort = () => { | ||
| cleanup() | ||
| reject(input.signal?.reason ?? new Error("Request aborted")) | ||
| } | ||
|
|
||
| const handler = (event: GlobalEvent) => { | ||
| try { | ||
| if (!input.fn(event)) return | ||
| cleanup() | ||
| resolve() | ||
| } catch (error) { | ||
| cleanup() | ||
| reject(error) | ||
| } | ||
| } | ||
|
|
||
| const cleanup = () => { | ||
| clearTimeout(timeout) | ||
| GlobalBus.off("event", handler) | ||
| input.signal?.removeEventListener("abort", abort) | ||
| } | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| cleanup() | ||
| reject(new Error("Timed out waiting for global event")) | ||
| }, input.timeout) | ||
|
|
||
| GlobalBus.on("event", handler) | ||
| input.signal?.addEventListener("abort", abort, { once: true }) | ||
| }) | ||
| } |
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,81 @@ | ||
| import type { MiddlewareHandler } from "hono" | ||
| import { Database, inArray } from "@/storage/db" | ||
| import { EventSequenceTable } from "@/sync/event.sql" | ||
| import { Workspace } from "@/control-plane/workspace" | ||
| import type { WorkspaceID } from "@/control-plane/schema" | ||
| import { Log } from "@/util/log" | ||
|
|
||
| const HEADER = "x-opencode-sync" | ||
| type State = Record<string, number> | ||
| const log = Log.create({ service: "fence" }) | ||
|
|
||
| export function load(ids?: string[]) { | ||
| const rows = Database.use((db) => { | ||
| if (!ids?.length) { | ||
| return db.select().from(EventSequenceTable).all() | ||
| } | ||
|
|
||
| return db.select().from(EventSequenceTable).where(inArray(EventSequenceTable.aggregate_id, ids)).all() | ||
| }) | ||
|
|
||
| return Object.fromEntries(rows.map((row) => [row.aggregate_id, row.seq])) as State | ||
| } | ||
|
|
||
| export function diff(prev: State, next: State) { | ||
| const ids = new Set([...Object.keys(prev), ...Object.keys(next)]) | ||
| return Object.fromEntries( | ||
| [...ids] | ||
| .map((id) => [id, next[id] ?? -1] as const) | ||
| .filter(([id, seq]) => { | ||
| return (prev[id] ?? -1) !== seq | ||
| }), | ||
| ) as State | ||
| } | ||
|
|
||
| export function parse(headers: Headers) { | ||
| const raw = headers.get(HEADER) | ||
| if (!raw) return | ||
|
|
||
| let data | ||
|
|
||
| try { | ||
| data = JSON.parse(raw) | ||
| } catch (err) { | ||
| return | ||
| } | ||
|
|
||
| if (!data || typeof data !== "object") return | ||
|
|
||
| return Object.fromEntries( | ||
| Object.entries(data).filter(([id, seq]) => { | ||
| return typeof id === "string" && Number.isInteger(seq) | ||
| }), | ||
| ) as State | ||
| } | ||
|
|
||
| export async function wait(workspaceID: WorkspaceID, state: State, signal?: AbortSignal) { | ||
| log.info("waiting for state", { | ||
| workspaceID, | ||
| state, | ||
| }) | ||
| await Workspace.waitForSync(workspaceID, state, signal) | ||
| log.info("state fully synced", { | ||
| workspaceID, | ||
| state, | ||
| }) | ||
| } | ||
|
|
||
| export const FenceMiddleware: MiddlewareHandler = async (c, next) => { | ||
| if (c.req.method === "GET" || c.req.method === "HEAD" || c.req.method === "OPTIONS") return next() | ||
|
|
||
| const prev = load() | ||
| await next() | ||
| const current = diff(prev, load()) | ||
|
|
||
| if (Object.keys(current).length > 0) { | ||
| log.info("header", { | ||
| diff: current, | ||
| }) | ||
| c.res.headers.set(HEADER, JSON.stringify(current)) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this end up being too large?
It depends on the runtime, but Bun has 16k limit on sum of all headers.
Can be changed thou: https://bun.com/docs/runtime#param-max-http-header-size