diff --git a/src/lib/server/integrations/airtable.ts b/src/lib/server/integrations/airtable.ts index 82f0385..0715b26 100644 --- a/src/lib/server/integrations/airtable.ts +++ b/src/lib/server/integrations/airtable.ts @@ -72,6 +72,7 @@ function pathMatches(query: string, stored: string): boolean { } const recordCache = new Map(); +const inflightRequests = new Map>(); const CACHE_TTL = 15 * 60 * 1000; export async function findRecordsByUrl( @@ -85,8 +86,29 @@ export async function findRecordsByUrl( log.trace('findRecordsByUrl cache hit', { cacheKey, recordCount: cached.records.length }); return cached.records; } + + const inflight = inflightRequests.get(cacheKey); + if (inflight) { + log.trace('findRecordsByUrl coalesced with in-flight request', { cacheKey }); + return inflight; + } + log.trace('findRecordsByUrl cache miss', { cacheKey }); + const promise = fetchRecordsByUrl(playableUrl, codeUrl, cacheKey); + inflightRequests.set(cacheKey, promise); + try { + return await promise; + } finally { + inflightRequests.delete(cacheKey); + } +} + +async function fetchRecordsByUrl( + playableUrl: string, + codeUrl: string, + cacheKey: string +): Promise { const baseId = env.AIRTABLE_BASE_ID; const tableId = env.AIRTABLE_TABLE_ID; const pat = env.AIRTABLE_PAT;