From d7a2d88dc57e76f82f129d68bfc78563b5ae3ef2 Mon Sep 17 00:00:00 2001 From: Nico Ritschel Date: Sun, 15 Nov 2026 08:34:13 -0800 Subject: [PATCH 1/2] Store the runner-status cache only when a poll changes it Every 45-second poll rewrote the whole cache (about 100 KiB on a busy org) even when GitHub reported nothing new, because each poll renews its fetch timestamps. On celld every rewrite becomes an LTX segment, so one idle collector produced thousands of segments a day. Compare the cache with those timestamps removed and skip storage when it is unchanged, still storing at least every 10 minutes so a restart restores recent fetch times. --- examples/runner-status/collector.test.ts | 12 +++++++++++- examples/runner-status/collector.ts | 8 ++++++++ examples/runner-status/worker.test.ts | 4 ++++ examples/runner-status/worker.ts | 11 ++++++++++- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/examples/runner-status/collector.test.ts b/examples/runner-status/collector.test.ts index 3765fb6..f809f40 100644 --- a/examples/runner-status/collector.test.ts +++ b/examples/runner-status/collector.test.ts @@ -1,5 +1,5 @@ import { expect, test } from "bun:test"; -import { boundedSnapshot, emptyCache, eligible, githubList, readConfig, readRepo, refresh, relevant, type List } from "./collector"; +import { boundedSnapshot, emptyCache, eligible, githubList, readConfig, readRepo, refresh, relevant, storedContent, type List } from "./collector"; import type { Job, Run, Runner } from "./types"; const config = { org: "example", repos: ["example/app"], runnerPrefix: "" }; @@ -47,6 +47,16 @@ test("preserves per-source data and timestamps on failure, then clears errors on expect(first.snapshot!.errors).toEqual([]); }); +test("stored content ignores renewed fetch times but not changed data or errors", async () => { + const first = await refresh(config, emptyCache(), fixture, () => "2026-09-07T00:00:00Z"); + const same = await refresh(config, first, fixture, () => "2026-09-07T00:01:00Z"); + expect(storedContent(same)).toBe(storedContent(first)); + const idle = await refresh(config, first, async (endpoint: string) => (endpoint.includes("/actions/runners") ? [{ ...runner, busy: false }] : endpoint.includes("/jobs?") ? [job] : [run]) as T[], () => "2026-09-07T00:01:00Z"); + expect(storedContent(idle)).not.toBe(storedContent(first)); + const failed = await refresh(config, first, async () => { throw new Error("offline"); }, () => "2026-09-07T00:01:00Z"); + expect(storedContent(failed)).not.toBe(storedContent(first)); +}); + test("follows pagination with server credentials and bounds concurrency across callers", async () => { let active = 0, peak = 0, calls = 0; const list = githubList("test-token", new AbortController().signal, (async (input, init) => { diff --git a/examples/runner-status/collector.ts b/examples/runner-status/collector.ts index 81be224..607ef28 100644 --- a/examples/runner-status/collector.ts +++ b/examples/runner-status/collector.ts @@ -109,6 +109,14 @@ export async function refresh(config: Config, previous: Cache, list: List, now = return cache; } +/** The cache without the timestamps that every refresh renews, so a refresh that found nothing new can skip storage. */ +export function storedContent(cache: Cache): string { + return JSON.stringify({ ...cache, runnerFetchedAt: null, + repos: Object.fromEntries(Object.entries(cache.repos).map(([repo, data]) => [repo, { ...data, fetchedAt: null }])), + snapshot: cache.snapshot && { ...cache.snapshot, runnerFetchedAt: null, checkedAt: null, + sources: cache.snapshot.sources.map(source => ({ ...source, fetchedAt: null })) } }); +} + /** Leave room for the MCP result envelope within the bridge's 256 KiB limit. */ export function boundedSnapshot(snapshot: Snapshot): Snapshot { const result = { ...snapshot, runners: [...snapshot.runners], jobs: [...snapshot.jobs], errors: [...snapshot.errors] }; diff --git a/examples/runner-status/worker.test.ts b/examples/runner-status/worker.test.ts index efa252c..94a4218 100644 --- a/examples/runner-status/worker.test.ts +++ b/examples/runner-status/worker.test.ts @@ -46,6 +46,10 @@ test("authenticated viewers share a persisted collector; unauthorized calls neve const deadline = Date.now() + 50_000; while (calls < 12 && Date.now() < deadline) await Bun.sleep(250); expect(calls).toBe(12); + // GitHub reported nothing new, so the alarm poll left the stored cache as it was. + await mf.dispose(); + mf = new Miniflare(options); + expect(await (await mf.dispatchFetch("https://collector/api/status", { headers: auth })).json()).toEqual(responses[0]); } finally { await mf.dispose(); await rm(directory, { recursive: true, force: true }); } }, 60000); diff --git a/examples/runner-status/worker.ts b/examples/runner-status/worker.ts index 6710f3d..0b0b377 100644 --- a/examples/runner-status/worker.ts +++ b/examples/runner-status/worker.ts @@ -1,7 +1,9 @@ -import { boundedSnapshot, emptyCache, githubList, readConfig, refresh, refreshSeconds, type Cache } from "./collector"; +import { boundedSnapshot, emptyCache, githubList, readConfig, refresh, refreshSeconds, storedContent, type Cache } from "./collector"; export type Env = { RUNNER_STATUS: DurableObjectNamespace; GITHUB_TOKEN: string; RUNNER_STATUS_TOKEN: string; RUNNER_ORG: string; RUNNER_REPOS: string; RUNNER_NAME_PREFIX?: string }; const headers = { "cache-control": "no-store", "content-type": "application/json", "x-content-type-options": "nosniff" }; +/** Store an unchanged cache at most this often, so a restart restores recent fetch times. */ +const unchangedStoreMs = 10 * 60 * 1000; export default { async fetch(request: Request, env: Env): Promise { @@ -16,6 +18,9 @@ export default { export class RunnerStatus { private cache = emptyCache(); private inFlight: Promise | null = null; + // Each stored cache is about 100 KiB, so storing it on every 45-second poll + // dominates the object's write volume even when GitHub reports nothing new. + private stored: { content: string; at: number } | null = null; constructor(private state: DurableObjectState, private env: Env) { state.blockConcurrencyWhile(async () => { const saved = await state.storage.get<{ config: string; chunks: number }>("manifest"); @@ -27,6 +32,7 @@ export class RunnerStatus { parts.push(part); } this.cache = JSON.parse(parts.join("")) as Cache; + this.stored = { content: storedContent(this.cache), at: Date.now() }; } }); } @@ -40,6 +46,8 @@ export class RunnerStatus { try { const config = readConfig(this.env); this.cache = await refresh(config, this.cache, githubList(this.env.GITHUB_TOKEN, controller.signal)); + const content = storedContent(this.cache); + if (this.stored && content === this.stored.content && Date.now() - this.stored.at < unchangedStoreMs) return; // Cache source data can exceed the per-value DO storage limit even when // the response is small. Commit bounded chunks and their manifest atomically. const serialized = JSON.stringify(this.cache); @@ -53,6 +61,7 @@ export class RunnerStatus { for (let index = parts.length; index < (old?.chunks ?? 0); index++) await storage.delete(`cache:${index}`); await storage.put("manifest", { config: JSON.stringify(config), chunks: parts.length }); }); + this.stored = { content, at: Date.now() }; } finally { clearTimeout(timer); } })().finally(() => { this.inFlight = null; }); return this.inFlight; From f3b903cbf29c986db1e4c1a7706cc0f8249f3f1f Mon Sep 17 00:00:00 2001 From: Nico Ritschel Date: Sun, 15 Nov 2026 08:43:16 -0800 Subject: [PATCH 2/2] Move the conditional cache store into storeCache Keeps poll() free to run later steps after the store, such as recording history, instead of returning early when the cache is unchanged. --- examples/runner-status/worker.ts | 38 ++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/examples/runner-status/worker.ts b/examples/runner-status/worker.ts index 0b0b377..ad89e6a 100644 --- a/examples/runner-status/worker.ts +++ b/examples/runner-status/worker.ts @@ -1,4 +1,4 @@ -import { boundedSnapshot, emptyCache, githubList, readConfig, refresh, refreshSeconds, storedContent, type Cache } from "./collector"; +import { boundedSnapshot, emptyCache, githubList, readConfig, refresh, refreshSeconds, storedContent, type Cache, type Config } from "./collector"; export type Env = { RUNNER_STATUS: DurableObjectNamespace; GITHUB_TOKEN: string; RUNNER_STATUS_TOKEN: string; RUNNER_ORG: string; RUNNER_REPOS: string; RUNNER_NAME_PREFIX?: string }; const headers = { "cache-control": "no-store", "content-type": "application/json", "x-content-type-options": "nosniff" }; @@ -46,26 +46,30 @@ export class RunnerStatus { try { const config = readConfig(this.env); this.cache = await refresh(config, this.cache, githubList(this.env.GITHUB_TOKEN, controller.signal)); - const content = storedContent(this.cache); - if (this.stored && content === this.stored.content && Date.now() - this.stored.at < unchangedStoreMs) return; - // Cache source data can exceed the per-value DO storage limit even when - // the response is small. Commit bounded chunks and their manifest atomically. - const serialized = JSON.stringify(this.cache); - const parts: string[] = []; - // A UTF-16 code unit can encode to three UTF-8 bytes (including lone - // surrogates at a chunk boundary); leave room below the storage limit. - for (let offset = 0; offset < serialized.length; offset += 16 * 1024) parts.push(serialized.slice(offset, offset + 16 * 1024)); - await this.state.storage.transaction(async storage => { - const old = await storage.get<{ chunks: number }>("manifest"); - for (let index = 0; index < parts.length; index++) await storage.put(`cache:${index}`, parts[index]!); - for (let index = parts.length; index < (old?.chunks ?? 0); index++) await storage.delete(`cache:${index}`); - await storage.put("manifest", { config: JSON.stringify(config), chunks: parts.length }); - }); - this.stored = { content, at: Date.now() }; + await this.storeCache(config); } finally { clearTimeout(timer); } })().finally(() => { this.inFlight = null; }); return this.inFlight; } + /** Skips storage when the poll changed nothing but fetch times, unless the stored copy is getting old. */ + private async storeCache(config: Config): Promise { + const content = storedContent(this.cache); + if (this.stored && content === this.stored.content && Date.now() - this.stored.at < unchangedStoreMs) return; + // Cache source data can exceed the per-value DO storage limit even when + // the response is small. Commit bounded chunks and their manifest atomically. + const serialized = JSON.stringify(this.cache); + const parts: string[] = []; + // A UTF-16 code unit can encode to three UTF-8 bytes (including lone + // surrogates at a chunk boundary); leave room below the storage limit. + for (let offset = 0; offset < serialized.length; offset += 16 * 1024) parts.push(serialized.slice(offset, offset + 16 * 1024)); + await this.state.storage.transaction(async storage => { + const old = await storage.get<{ chunks: number }>("manifest"); + for (let index = 0; index < parts.length; index++) await storage.put(`cache:${index}`, parts[index]!); + for (let index = parts.length; index < (old?.chunks ?? 0); index++) await storage.delete(`cache:${index}`); + await storage.put("manifest", { config: JSON.stringify(config), chunks: parts.length }); + }); + this.stored = { content, at: Date.now() }; + } async fetch(): Promise { if (!this.cache.snapshot) await this.poll(); else if (Date.now() - Date.parse(this.cache.snapshot.checkedAt) >= refreshSeconds * 1000) this.state.waitUntil(this.poll());