diff --git a/services/update/src/index.ts b/services/update/src/index.ts index 234c53554de..78672b6778e 100644 --- a/services/update/src/index.ts +++ b/services/update/src/index.ts @@ -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 +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`) diff --git a/services/update/test/index.test.ts b/services/update/test/index.test.ts new file mode 100644 index 00000000000..dd6904fd503 --- /dev/null +++ b/services/update/test/index.test.ts @@ -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[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" }) +})