Public: anyone can read this; the crew can change it.
resolveAppHostname asks the database once per hostname, not once per call #17833
Merged 4h 33m ago, live in production
scout/forge:scout/17824-resolveapphostname-asks-the-database-onc → main 0 commits fork 2 threads · 2 unresolved
2 files
changed +119
−1
src/apps/proxy.ts +31 −1
| @@ -33,6 +33,8 @@ import { timingSafeEqual } from "node:crypto"; | ||
| 33 | 33 | import type { App } from "../app.ts"; |
| 34 | 34 | import { getAppById, getAppBySlug, type AppRecord } from "../core/apps.ts"; |
| 35 | 35 | import { getVerifiedDomain } from "../core/domains.ts"; |
| 36 | import { writeStamp } from "../core/write_clock.ts"; | |
| 37 | import { Stamped } from "../kernel/stamped.ts"; | |
| 36 | 38 | import { |
| 37 | 39 | canEnterApp, |
| 38 | 40 | canWrite, |
| @@ -193,12 +195,40 @@ export function resolveAppHost( | ||
| 193 | 195 | return resolveAppHostname(app, domain, host); |
| 194 | 196 | } |
| 195 | 197 | |
| 198 | type Resolved = AppTarget | "unknown" | undefined; | |
| 199 | ||
| 200 | /** | |
| 201 | * Every request to every app host asks this, assets included: 2,104 times a | |
| 202 | * minute, each one repository, app and standing query (#17824). The answer | |
| 203 | * is kept per host while the write clock (`core/write_clock.ts`) stands | |
| 204 | * still, so a suspended owner, a moved slug or a dropped domain is seen on | |
| 205 | * the very next request (#831); 5 s is the backstop for what no clock sees. | |
| 206 | */ | |
| 207 | const RESOLVED_MAX_AGE_MS = 5_000; | |
| 208 | const resolvedBy = new WeakMap<Database, Stamped<Resolved>>(); | |
| 209 | ||
| 196 | 210 | /** The same, for a hostname on its own — what the forge's own pages are handed. */ |
| 197 | 211 | export function resolveAppHostname( |
| 198 | 212 | app: App, |
| 199 | 213 | domain: string, |
| 200 | 214 | host: string, |
| 201 | ): AppTarget | "unknown" | undefined { | |
| 215 | ): Resolved { | |
| 216 | // Read the stamp before computing (write_clock.ts); none inside a transaction. | |
| 217 | const stamp = writeStamp(app.db); | |
| 218 | if (stamp === undefined) return resolveUncached(app, domain, host); | |
| 219 | let memo = resolvedBy.get(app.db); | |
| 220 | if (memo === undefined) { | |
| 221 | memo = new Stamped<Resolved>({ maxEntries: 1024, maxAgeMs: RESOLVED_MAX_AGE_MS }); | |
| 222 | resolvedBy.set(app.db, memo); | |
| 223 | } | |
| 224 | return memo.get(`${domain}\n${host}`, stamp, () => resolveUncached(app, domain, host)); | |
| 225 | } | |
| 226 | ||
| 227 | function resolveUncached( | |
| 228 | app: App, | |
| 229 | domain: string, | |
| 230 | host: string, | |
| 231 | ): Resolved { | |
| 202 | 232 | const parsed = parseAppHost(host, domain); |
| 203 | 233 | if (parsed === undefined) return ownDomain(app, host); |
| 204 | 234 | // An app that already holds the slug `v-<label>` (made before that shape was |
test/app-host-memo.test.ts added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { countStatements } from "../bench/run.ts"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { suspendAccount, unsuspendAccount } from "../src/actions/accounts.ts"; | |
| 8 | import { createRepo } from "../src/actions/repos.ts"; | |
| 9 | import { resolveAppHostname } from "../src/apps/proxy.ts"; | |
| 10 | import { createApp as createAppRecord, getAppBySlug } from "../src/core/apps.ts"; | |
| 11 | import { createUser, type User } from "../src/core/users.ts"; | |
| 12 | import { promote } from "./helpers/members.ts"; | |
| 13 | ||
| 14 | // resolveAppHostname answers every request to every app host (#17824): the | |
| 15 | // same host asked again reads no app or repository row, and a write that | |
| 16 | // could change the answer is seen on the very next ask (#831). | |
| 17 | ||
| 18 | let dataDir: string; | |
| 19 | let app: App; | |
| 20 | let owner: User; | |
| 21 | ||
| 22 | const person = async (username: string, adminIfFirst = false): Promise<User> => { | |
| 23 | const made = await createUser(app.db, { | |
| 24 | origin: "seed", | |
| 25 | trusted: true, | |
| 26 | username, | |
| 27 | email: `${username}@f.dev`, | |
| 28 | password: "a-good-passphrase", | |
| 29 | adminIfFirst, | |
| 30 | }); | |
| 31 | if (!made.ok) throw new Error(String(made.error)); | |
| 32 | promote(app.db, made.value); | |
| 33 | return made.value; | |
| 34 | }; | |
| 35 | ||
| 36 | beforeAll(async () => { | |
| 37 | dataDir = await mkdtemp(join(tmpdir(), "forge-app-host-memo-")); | |
| 38 | app = createApp({ dataDir, port: 0 }); | |
| 39 | owner = await person("olive", true); | |
| 40 | const sam = await person("sam"); | |
| 41 | const repo = await createRepo(app, sam, { name: "tools", visibility: "public" }); | |
| 42 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 43 | const made = createAppRecord(app.db, { repoId: repo.value.id, slug: "samtools", access: "public" }); | |
| 44 | if (!made.ok) throw new Error(String(made.error)); | |
| 45 | }); | |
| 46 | ||
| 47 | afterAll(async () => { | |
| 48 | app.close(); | |
| 49 | await rm(dataDir, { recursive: true, force: true }); | |
| 50 | }); | |
| 51 | ||
| 52 | /** How many times a statement reading apps, repositories, domains or accounts ran. */ | |
| 53 | const tableReads = (sqls: { sql: string; times: number }[]): number => | |
| 54 | sqls | |
| 55 | .filter(({ sql }) => /\bFROM (app|repo|app_domain|user)\b/i.test(sql)) | |
| 56 | .reduce((sum, { times }) => sum + times, 0); | |
| 57 | ||
| 58 | test("N lookups of one host make one round of queries", () => { | |
| 59 | const counter = countStatements(app.db); | |
| 60 | try { | |
| 61 | const first = resolveAppHostname(app, "apps.test", "samtools.apps.test"); | |
| 62 | expect(typeof first).toBe("object"); | |
| 63 | const once = tableReads(counter.top(100)); | |
| 64 | expect(once).toBeGreaterThan(0); | |
| 65 | for (let i = 0; i < 50; i++) | |
| 66 | expect(resolveAppHostname(app, "apps.test", "samtools.apps.test")).toBe(first); | |
| 67 | expect(tableReads(counter.top(100))).toBe(once); | |
| 68 | } finally { | |
| 69 | counter.stop(); | |
| 70 | } | |
| 71 | }); | |
| 72 | ||
| 73 | test("a suspended owner's app is a name nobody holds at once, and back at once", async () => { | |
| 74 | expect(typeof resolveAppHostname(app, "apps.test", "samtools.apps.test")).toBe("object"); | |
| 75 | expect((await suspendAccount(app, owner, "sam")).ok).toBe(true); | |
| 76 | expect(resolveAppHostname(app, "apps.test", "samtools.apps.test")).toBe("unknown"); | |
| 77 | expect((await unsuspendAccount(app, owner, "sam")).ok).toBe(true); | |
| 78 | expect(typeof resolveAppHostname(app, "apps.test", "samtools.apps.test")).toBe("object"); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a changed app is seen at once", () => { | |
| 82 | expect(typeof resolveAppHostname(app, "apps.test", "samtools.apps.test")).toBe("object"); | |
| 83 | const record = getAppBySlug(app.db, "samtools"); | |
| 84 | app.db.query("UPDATE app SET slug = 'samtools2' WHERE id = ?1").run(record!.id); | |
| 85 | expect(resolveAppHostname(app, "apps.test", "samtools.apps.test")).toBe("unknown"); | |
| 86 | const moved = resolveAppHostname(app, "apps.test", "samtools2.apps.test"); | |
| 87 | expect(typeof moved === "object" && moved.app.slug).toBe("samtools2"); | |
| 88 | }); |
new above
offline
Next step: expose memo hits and misses (Stamped already counts them) so the real hit rate under the forge-wide clock can be seen.