mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
fix(site): stop sharing in-flight edge cache reads across requests (#66226)
This commit is contained in:
parent
1b297d1a49
commit
a75fcac14c
2 changed files with 52 additions and 16 deletions
|
|
@ -33,24 +33,25 @@ export function staleValue<T>(options: {
|
|||
/** False when the page reads fine without the value (the status bars), so its HTML may still be kept. */
|
||||
marksStandIn?: boolean
|
||||
}) {
|
||||
// Only settled values are shared between requests: a promise never settles once the request that
|
||||
// made it has ended. A refresh marks `tried` when it starts, so other reads don't start their own
|
||||
// until the retry window passes, and one whose request ended can't block the next.
|
||||
const state: {
|
||||
value?: T
|
||||
time: number
|
||||
tried: number
|
||||
pending?: Promise<T | undefined>
|
||||
stored?: Promise<void>
|
||||
stored?: boolean
|
||||
} = {
|
||||
time: 0,
|
||||
tried: 0,
|
||||
}
|
||||
|
||||
const refresh = () => {
|
||||
state.pending ??= options
|
||||
state.tried = Date.now()
|
||||
return options
|
||||
.load()
|
||||
.catch(() => undefined)
|
||||
.then(async (value) => {
|
||||
state.pending = undefined
|
||||
state.tried = Date.now()
|
||||
if (value === undefined) {
|
||||
if (state.value !== undefined) state.time = Date.now()
|
||||
return state.value
|
||||
|
|
@ -60,25 +61,26 @@ export function staleValue<T>(options: {
|
|||
await writeStored(options.key, { time: state.time, value })
|
||||
return value
|
||||
})
|
||||
return state.pending
|
||||
}
|
||||
|
||||
return async function read(wait = options.wait): Promise<{ value: T | undefined; standIn: boolean }> {
|
||||
state.stored ??= readStored<T>(options.key).then((stored) => {
|
||||
if (!stored || state.value !== undefined) return
|
||||
state.value = stored.value
|
||||
state.time = stored.time
|
||||
})
|
||||
await state.stored
|
||||
if (!state.stored) {
|
||||
const stored = await readStored<T>(options.key)
|
||||
state.stored = true
|
||||
if (stored && state.value === undefined) {
|
||||
state.value = stored.value
|
||||
state.time = stored.time
|
||||
}
|
||||
}
|
||||
const due =
|
||||
Date.now() - state.time >= options.freshFor && Date.now() - state.tried >= Math.min(options.freshFor, 60_000)
|
||||
if (state.value !== undefined) {
|
||||
if (due) background(refresh())
|
||||
return { value: state.value, standIn: false }
|
||||
}
|
||||
// Only the read that starts a refresh waits for it; reads while it runs take the stand-in at
|
||||
// once, unless they asked to wait longer than pages do (feeds).
|
||||
if (state.pending ? wait <= options.wait : !due) return standIn()
|
||||
// Only the read that starts a refresh waits for it; reads while it runs, or inside the retry window
|
||||
// after a failed one, take the stand-in at once (feeds included), so a failing source is backed off.
|
||||
if (!due) return standIn()
|
||||
const pending = refresh()
|
||||
background(pending)
|
||||
const value = await Promise.race([pending, new Promise<undefined>((resolve) => setTimeout(resolve, wait))])
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
import { describe, expect, test } from "bun:test"
|
||||
import { afterEach, describe, expect, setSystemTime, test } from "bun:test"
|
||||
import { staleValue } from "~/lib/edge-cache"
|
||||
|
||||
describe("staleValue", () => {
|
||||
afterEach(() => setSystemTime())
|
||||
|
||||
test("answers from the fallback when the source is slow, then from the source", async () => {
|
||||
let finish = (_value: string) => {}
|
||||
const read = staleValue<string>({
|
||||
|
|
@ -35,4 +37,36 @@ describe("staleValue", () => {
|
|||
await Bun.sleep(0)
|
||||
expect(await read()).toEqual({ value: "first", standIn: false })
|
||||
})
|
||||
|
||||
test("a refresh that never settles doesn't stop later ones", async () => {
|
||||
// A promise made by a request that has since ended never settles.
|
||||
const answers = [new Promise<string>(() => {}), Promise.resolve("live")]
|
||||
const read = staleValue<string>({
|
||||
key: "test/lost",
|
||||
freshFor: 60_000,
|
||||
wait: 20,
|
||||
load: () => answers.shift()!,
|
||||
fallback: () => "snapshot",
|
||||
})
|
||||
expect(await read()).toEqual({ value: "snapshot", standIn: true })
|
||||
expect(await read()).toEqual({ value: "snapshot", standIn: true })
|
||||
setSystemTime(Date.now() + 61_000)
|
||||
expect(await read()).toEqual({ value: "live", standIn: false })
|
||||
})
|
||||
test("feeds back off a failing source like pages do", async () => {
|
||||
let loads = 0
|
||||
const read = staleValue<string>({
|
||||
key: "test/feed-backoff",
|
||||
freshFor: 60_000,
|
||||
wait: 20,
|
||||
load: async () => {
|
||||
loads++
|
||||
return undefined
|
||||
},
|
||||
fallback: () => "snapshot",
|
||||
})
|
||||
expect(await read()).toEqual({ value: "snapshot", standIn: true })
|
||||
for (let i = 0; i < 5; i++) expect(await read(50)).toEqual({ value: "snapshot", standIn: true })
|
||||
expect(loads).toBe(1)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue