From afa76578d48fe3f92d39ad4202589d4893a08400 Mon Sep 17 00:00:00 2001 From: Kieran Klukas Date: Mon, 9 Mar 2026 07:56:30 -0400 Subject: [PATCH] feat: less kv reads and writes --- src/index.ts | 7 +++++-- src/manifest.ts | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8031a0f..fb11e7b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -157,8 +157,11 @@ export default { } } } else { - // Service is up — clear failure counter - await env.KV.delete(`triage:${svc.name}:failures`); + // Service is up — clear failure counter (only if one exists, to avoid unnecessary KV delete ops) + const failKey = `triage:${svc.name}:failures`; + if (await env.KV.get(failKey)) { + await env.KV.delete(failKey); + } // Auto-resolve active incidents const active = await getActiveIncidentForService(env.DB, svc.name); diff --git a/src/manifest.ts b/src/manifest.ts index 1989a6e..e42e7ea 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -2,19 +2,20 @@ import type { Env, ServicesManifest } from "./types"; const MANIFEST_URL = "https://dots.dunkirk.sh/services.json"; const KV_KEY = "services_manifest"; -const TTL_SECONDS = 300; export async function getManifest(env: Env): Promise { - const cached = await env.KV.get(KV_KEY, "json"); - if (cached) return cached as ServicesManifest; - - const res = await fetch(MANIFEST_URL); + const [res, existing] = await Promise.all([ + fetch(MANIFEST_URL), + env.KV.get(KV_KEY), + ]); if (!res.ok) throw new Error(`Failed to fetch manifest: ${res.status}`); - const manifest: ServicesManifest = await res.json(); - await env.KV.put(KV_KEY, JSON.stringify(manifest), { - expirationTtl: TTL_SECONDS, - }); + const serialized = JSON.stringify(await res.json()); + + // Only write to KV if the manifest changed + if (serialized !== existing) { + await env.KV.put(KV_KEY, serialized); + } - return manifest; + return JSON.parse(serialized); } -- 2.51.2