Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
perf(identity): read lid allowlist entries in one query
Yudhi Armyndharis0623f247fb5c
diff --git a/src/engine/identity/lid-mapping-store.service.ts b/src/engine/identity/lid-mapping-store.service.tsindex 5ccd02df6..cd548796a 100644--- a/src/engine/identity/lid-mapping-store.service.ts+++ b/src/engine/identity/lid-mapping-store.service.ts@@ -203,7 +203,9 @@ export class LidMappingStoreService implements LidMappingStore, OnModuleInit {async phonesForLidsPersisted(lids: string[]): Promise<Record<string, string | null>> {const out: Record<string, string | null> = {};if (lids.length === 0) return out;- for (const lid of lids) out[lid] = this.getCached(lid) ?? null;+ // The mirror is only the fallback for a failed or empty table read, so it is read without getCached:+ // that warms every miss with a findOne of its own, one per lid ahead of the batched query below.+ for (const lid of lids) out[lid] = this.lidToPhone.get(lid) ?? null;try {for (const batch of chunk(lids)) {const rows = await this.repo.find({ where: { lid: In(batch) } });diff --git a/src/engine/identity/lid-mapping-store.spec.ts b/src/engine/identity/lid-mapping-store.spec.tsindex eef9c1817..c01a97c9f 100644--- a/src/engine/identity/lid-mapping-store.spec.ts+++ b/src/engine/identity/lid-mapping-store.spec.ts@@ -722,6 +722,28 @@ describe('LidMappingStoreService — deterministic persisted lookups (authorizatexpect(repo.find).toHaveBeenCalledWith({ where: { phone: In(['628888']) } });});+ // An allowlist of N lids that the mirror has not seen used to cost N single-row reads ahead of the one+ // batched query, because the mirror was read through getCached, which warms every miss.+ it('phonesForLidsPersisted answers a batch of lids the mirror lacks with one query and no per-lid reads', async () => {+ process.env.LID_MAPPING_CACHE_MAX = '1';+ try {+ const lids = Array.from({ length: 300 }, (_, i) => `9${i}`);+ const repo = makeFakeRepo(lids.map(lid => ({ lid, phone: `62${lid}` })));+ const store = await newStore(repo); // the cap keeps one of the 300 resident+ repo.find.mockClear();+ repo.findOne.mockClear();++ const answer = await store.phonesForLidsPersisted(lids);++ expect(Object.keys(answer)).toHaveLength(300);+ expect(answer['9299']).toBe('629299');+ expect(repo.findOne).not.toHaveBeenCalled();+ expect(repo.find).toHaveBeenCalledTimes(1);+ } finally {+ delete process.env.LID_MAPPING_CACHE_MAX;+ }+ });+it('batched lookups handle an empty input, a null-phone row, and a read error', async () => {const repo = makeFakeRepo([{ lid: '111', phone: '628999' },