Skip to content
File

Blob: src/worker/git/object-store/catalog.ts

typescript54 lines
1import type { CacheContext } from "@/worker/cache";
2import type { PackCatalogRow } from "./types";
3 
4import { createLogger, getRepoStub } from "@/worker/common";
5import { countSubrequest, getLimiter } from "@/worker/git/operations/limits";
6import { ensureMemo, logOnce } from "./support";
7 
8export async function loadActivePackCatalog(
9 env: Env,
10 repoId: string,
11 cacheCtx?: CacheContext
12): Promise<PackCatalogRow[]> {
13 ensureMemo(cacheCtx, repoId);
14 const log = createLogger(env.LOG_LEVEL, { service: "PackedObjectStore", repoId });
15 
16 if (cacheCtx?.memo?.packCatalog) {
17 logOnce(cacheCtx, "packed-catalog-logged", () => {
18 log.debug("pack-catalog:loaded", {
19 source: "memo",
20 packs: cacheCtx.memo?.packCatalog?.length,
21 });
22 });
23 return cacheCtx.memo.packCatalog;
24 }
25 if (cacheCtx?.memo?.packCatalogPromise) return await cacheCtx.memo.packCatalogPromise;
26 
27 const stub = getRepoStub(env, repoId);
28 const limiter = getLimiter(cacheCtx);
29 const inflight = limiter.run("do:get-active-pack-catalog", async () => {
30 if (!countSubrequest(cacheCtx)) {
31 logOnce(cacheCtx, "packed-catalog-soft-budget-warned", () => {
32 log.warn("soft-budget-exhausted", {
33 op: "do:get-active-pack-catalog",
34 });
35 });
36 }
37 return await stub.getActivePackCatalog();
38 });
39 if (cacheCtx?.memo) cacheCtx.memo.packCatalogPromise = inflight;
40 try {
41 const rows = await inflight;
42 if (cacheCtx?.memo) cacheCtx.memo.packCatalog = rows;
43 logOnce(cacheCtx, "packed-catalog-logged", () => {
44 log.debug("pack-catalog:loaded", { source: "do", packs: rows.length });
45 });
46 return rows;
47 } catch (error) {
48 log.warn("pack-catalog:load-error", { error: String(error) });
49 throw error;
50 } finally {
51 if (cacheCtx?.memo) cacheCtx.memo.packCatalogPromise = undefined;
52 }
53}