Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
perf(identity): stop re-querying the table for a lid it has no row for
Yudhi Armyndharis61273634b707
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.tsindex 319dbc545..7c6d053b9 100644--- a/src/engine/identity/lid-mapping-store.service.ts+++ b/src/engine/identity/lid-mapping-store.service.ts@@ -55,6 +55,14 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {private readonly phoneToLids = new Map<string, Set<string>>();/** Repository fallbacks in flight, one per lid, so a hot miss path can't stack duplicate queries. */private readonly pendingLookups = new Set<string>();+ /**+ * Lids the table answered for and had no row for. Without it every lookup of an unmapped lid+ * re-queries, and the callers are on hot paths: a webhook filter resolves both the event's actor+ * and each of its own rule values on every dispatch. Cleared for a lid the moment one is learned+ * ({@link index}) or the whole table is reloaded, so a negative can never shadow a later mapping.+ * Bounded by the same cap as the forward map, oldest first.+ */+ private readonly absentFromTable = new Set<string>();// 0 = unbounded (legacy behaviour). Every other long-lived map in the engine surface is bounded, so// the default is finite; the env override exists for operators who explicitly want the old behaviour.private readonly maxCachedLids: number;@@ -89,6 +97,8 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {});this.lidToPhone.clear();this.phoneToLids.clear();+ // A reload re-reads the table, so every recorded absence is a fresh question again.+ this.absentFromTable.clear();for (const row of rows) {this.index(row.lid, row.phone);}@@ -144,7 +154,7 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {* a read error is swallowed (the table may not exist yet), the same posture as reload().*/private warmFromTable(lid: string): void {- if (!lid || this.pendingLookups.has(lid)) return;+ if (!lid || this.pendingLookups.has(lid) || this.absentFromTable.has(lid)) return;this.pendingLookups.add(lid);void this.repo.findOne({ where: { lid } })@@ -152,7 +162,9 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {// Last-write-wins: a remember() that landed while the lookup was in flight is newer.if (row && !this.lidToPhone.has(row.lid)) {this.index(row.lid, row.phone);+ return;}+ if (!row) this.noteAbsent(lid);}).catch(() => undefined).finally(() => this.pendingLookups.delete(lid));@@ -172,9 +184,21 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {set.add(lid);this.phoneToLids.set(phone, set);}+ this.absentFromTable.delete(lid);this.evictIfOverCap();}+ /** Record that the table holds no row for this lid, bounded oldest-first like the forward map. */+ private noteAbsent(lid: string): void {+ this.absentFromTable.add(lid);+ if (!this.maxCachedLids) return;+ while (this.absentFromTable.size > this.maxCachedLids) {+ const oldest = this.absentFromTable.values().next().value;+ if (oldest === undefined) break;+ this.absentFromTable.delete(oldest);+ }+ }+/** Evict the least-recently-used forward entry (and its reverse index) while over the cap. */private evictIfOverCap(): void {if (!this.maxCachedLids) return; // unboundeddiff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.tsindex 5435ec28e..ee9182311 100644--- a/src/engine/identity/lid-mapping-store.spec.ts+++ b/src/engine/identity/lid-mapping-store.spec.ts@@ -229,6 +229,43 @@ describe('LidMappingStoreService — deterministic preload + repository fallbackexpect(store.getCached('lid-a')).toBe('620001');});+ it('stops re-querying the table for a lid it has no row for', async () => {+ // The callers are hot: a webhook filter resolves the event's actor AND each of its own rule+ // values on every dispatch, so an unmapped lid used to mean one query per event per webhook.+ const repo = makeFakeRepo(); // empty table: every lookup misses+ const store = new LidMappingStoreService(repo as unknown as Repository<LidMapping>);++ expect(store.getCached('lid-absent')).toBeUndefined();+ await new Promise(resolve => setImmediate(resolve));+ expect(repo.findOne).toHaveBeenCalledTimes(1);++ for (let i = 0; i < 5; i++) {+ expect(store.getCached('lid-absent')).toBeUndefined();+ await new Promise(resolve => setImmediate(resolve));+ }+ expect(repo.findOne).toHaveBeenCalledTimes(1);+ });++ it('a recorded absence never shadows a mapping learned afterwards, even once it is evicted', async () => {+ // The forward map answers a learned mapping directly, so the absence only matters after the+ // entry is evicted: a stale one would block the warm-back and make the row unresolvable for+ // the life of the process, although it is still in the table.+ process.env.LID_MAPPING_CACHE_MAX = '1';+ const repo = makeFakeRepo();+ const store = new LidMappingStoreService(repo as unknown as Repository<LidMapping>);+ await store.onModuleInit();++ expect(store.getCached('lid-late')).toBeUndefined(); // records the absence+ await new Promise(resolve => setImmediate(resolve));++ await store.remember('lid-late', '620009');+ await store.remember('lid-other', '620010'); // evicts lid-late (cap 1); its row stays persisted++ expect(store.getCached('lid-late')).toBeUndefined(); // the miss itself stays a miss+ await new Promise(resolve => setImmediate(resolve));+ expect(store.getCached('lid-late')).toBe('620009'); // warmed back rather than blocked+ });+it('swallows a fallback read error (table unavailable) — the miss stays a miss and never throws', async () => {const repo = makeFakeRepo();repo.findOne.mockRejectedValueOnce(new Error('no such table: lid_mappings'));