fix(update): return 503 instead of 1101 when D1 is overloaded (#54920)

Co-authored-by: rekram1-node <63023139+rekram1-node@users.noreply.github.com>
This commit is contained in:
opencode-agent[bot]
2026-10-12 19:27:14 -05:00
committed by GitHub
Unverified
parent df63a72da4
commit 67f5c2197a
2 changed files with 49 additions and 24 deletions
+30 -24
View File
@@ -77,33 +77,39 @@ export default {
return new Response("Not found", { status: 404 })
}
const resolved = resolveChannel(path[0])
const agent = request.headers.get("User-Agent")?.match(/^opencode\/(?:([^/]+)\/([^/]+)\/cli|(.*))$/)
const current = url.searchParams.get("current") ?? agent?.[2] ?? agent?.[3]
const source = agent?.[1] ?? current?.match(/^v?0\.0\.0-(.+)-\d+(?:\.\d+)?(?:\+.*)?$/)?.[1]
const caller = source === undefined || resolveChannel(source) === resolved ? current : undefined
const rollout = await env.DB.prepare("SELECT duration_hours FROM channel_rollout WHERE channel = ?")
.bind(resolved)
.first<{ duration_hours: number }>()
const ip = request.headers.get("CF-Connecting-IP")
const hash =
rollout?.duration_hours && ip
? new DataView(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${resolved}:${ip}`))).getUint32(
0,
)
: undefined
// Missing IPs wait for full rollout. The same IP keeps its position across releases.
const cutoff =
Date.now() - (rollout?.duration_hours ?? 0) * 3_600_000 * (hash === undefined ? 1 : (hash + 1) / 2 ** 32)
if (path.length === 4) {
return artifactDistribution(env.DB, resolved, path[1], path[2], caller, cutoff, path[3])
}
if (path.length === 1) return channel(env.DB, resolved, caller, cutoff)
if (path.length === 2) return artifactName(env.DB, resolved, path[1], caller, cutoff)
return artifactDistribution(env.DB, resolved, path[1], path[2], caller, cutoff)
// D1 rejects queries while the database is overloaded. Return a retryable 503 instead of a 1101 exception page.
return lookup(request, env, url, path).catch((error) => {
console.error("Update lookup failed", error)
return json({ error: "Update service temporarily unavailable" }, 503, { "Retry-After": "60" })
})
},
} satisfies ExportedHandler<Env>
async function lookup(request: Request, env: Env, url: URL, path: string[]) {
const resolved = resolveChannel(path[0])
const agent = request.headers.get("User-Agent")?.match(/^opencode\/(?:([^/]+)\/([^/]+)\/cli|(.*))$/)
const current = url.searchParams.get("current") ?? agent?.[2] ?? agent?.[3]
const source = agent?.[1] ?? current?.match(/^v?0\.0\.0-(.+)-\d+(?:\.\d+)?(?:\+.*)?$/)?.[1]
const caller = source === undefined || resolveChannel(source) === resolved ? current : undefined
const rollout = await env.DB.prepare("SELECT duration_hours FROM channel_rollout WHERE channel = ?")
.bind(resolved)
.first<{ duration_hours: number }>()
const ip = request.headers.get("CF-Connecting-IP")
const hash =
rollout?.duration_hours && ip
? new DataView(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${resolved}:${ip}`))).getUint32(0)
: undefined
// Missing IPs wait for full rollout. The same IP keeps its position across releases.
const cutoff =
Date.now() - (rollout?.duration_hours ?? 0) * 3_600_000 * (hash === undefined ? 1 : (hash + 1) / 2 ** 32)
if (path.length === 4) {
return artifactDistribution(env.DB, resolved, path[1], path[2], caller, cutoff, path[3])
}
if (path.length === 1) return channel(env.DB, resolved, caller, cutoff)
if (path.length === 2) return artifactName(env.DB, resolved, path[1], caller, cutoff)
return artifactDistribution(env.DB, resolved, path[1], path[2], caller, cutoff)
}
async function channel(db: D1Database, channel: string, current: string | undefined, cutoff: number) {
const result = await db
.prepare(`${select} WHERE channel = ? AND (active = 1 OR minimum = 1) ORDER BY name, distribution`)
+19
View File
@@ -0,0 +1,19 @@
import { expect, test } from "bun:test"
import worker from "../src/index"
test("returns a retryable 503 when D1 rejects a lookup", async () => {
const overloaded = new Error("D1_ERROR: D1 DB is overloaded. Requests queued for too long.")
const env = {
DB: { prepare: () => ({ bind: () => ({ first: () => Promise.reject(overloaded) }) }) },
EVENTS: { send: () => Promise.resolve() },
} as unknown as Parameters<typeof worker.fetch>[1]
const response = await worker.fetch(
new Request("https://opencode.ai/update/api/latest/desktop/opencode/opencode-desktop-win-x64.exe"),
env,
{ waitUntil: () => {} },
)
expect(response.status).toBe(503)
expect(response.headers.get("Retry-After")).toBe("60")
expect(response.headers.get("Cache-Control")).toBe("no-store")
expect(await response.json()).toEqual({ error: "Update service temporarily unavailable" })
})