Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
perf(baileys): stop re-querying chat state for chats that have none
Yudhi Armyndharis3ae85a3be858
diff --git a/src/engine/adapters/baileys-chat-state-store.service.spec.ts b/src/engine/adapters/baileys-chat-state-store.service.spec.tsindex 5a194c79e..fda1e4294 100644--- a/src/engine/adapters/baileys-chat-state-store.service.spec.ts+++ b/src/engine/adapters/baileys-chat-state-store.service.spec.ts@@ -135,6 +135,28 @@ describe('ChatStateStoreService', () => {expect(svc.get('s', 'c')).toEqual({ muteEndTime: 5, archived: false, pinned: true });});+ it('does not query again for a chat the table has no row for', async () => {+ const repo = makeRepo();+ const svc = svcWith(repo);+ expect(svc.get('s', 'c')).toBeUndefined();+ await tick();+ expect(svc.get('s', 'c')).toBeUndefined();+ await tick();+ expect(repo.findOne).toHaveBeenCalledTimes(1);+ });++ it('reads through again once a chat known to have no row gets one and is evicted', async () => {+ process.env[ENV] = '1';+ const svc = svcWith(makeRepo());+ svc.get('s', 'c');+ await tick(); // 'c' is now known to have no row+ await svc.remember('s', 'c', { archived: true });+ await svc.remember('s', 'd', { pinned: true }); // cap 1: evicts 'c' from the cache+ expect(svc.get('s', 'c')).toBeUndefined();+ await tick();+ expect(svc.get('s', 'c')).toEqual({ muteEndTime: null, archived: true, pinned: false });+ });+it('swallows a repo error on reload and remember (table may not exist yet)', async () => {const repo = {find: jest.fn(() => Promise.reject(new Error('no such table'))),diff --git a/src/engine/adapters/baileys-chat-state-store.service.ts b/src/engine/adapters/baileys-chat-state-store.service.tsindex 135d70bb5..f7e32a6a4 100644--- a/src/engine/adapters/baileys-chat-state-store.service.ts+++ b/src/engine/adapters/baileys-chat-state-store.service.ts@@ -46,6 +46,12 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {private readonly states = new Map<string, ChatStateValue>();/** Repository fallbacks in flight, one per key, so a hot miss path can't stack duplicate queries. */private readonly pendingLookups = new Set<string>();+ /**+ * Keys the table has no row for, so a chat never muted, archived or pinned (most of them) is not+ * queried again on every chat-list read. Kept apart from `states` so it never evicts a real row;+ * bounded by the same cap, and a key leaves it the moment a state is indexed for it.+ */+ private readonly absent = new Set<string>();private readonly maxEntries: number;constructor(@@ -66,6 +72,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {take: this.maxEntries > 0 ? this.maxEntries : undefined,});this.states.clear();+ this.absent.clear();for (const row of rows) {this.index(this.key(row.sessionId, row.chatId), {muteEndTime: row.muteEndTime,@@ -89,7 +96,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {this.states.set(k, value);return value;}- this.warmFromTable(k, sessionId, chatId);+ if (!this.absent.has(k)) this.warmFromTable(k, sessionId, chatId);return undefined;}@@ -109,7 +116,9 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {try {row = await this.repo.findOne({ where: { sessionId, chatId } });} catch {- return this.persist(sessionId, chatId, patch);+ await this.persist(sessionId, chatId, patch);+ this.absent.delete(k);+ return;}existing = row ? { muteEndTime: row.muteEndTime, archived: row.archived, pinned: row.pinned } : DEFAULT_STATE;}@@ -152,8 +161,14 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {void this.repo.findOne({ where: { sessionId, chatId } }).then(row => {- if (row && !this.states.has(k)) {+ if (this.states.has(k)) return;+ if (row) {this.index(k, { muteEndTime: row.muteEndTime, archived: row.archived, pinned: row.pinned });+ } else {+ this.absent.add(k);+ if (this.maxEntries && this.absent.size > this.maxEntries) {+ this.absent.delete(this.absent.values().next().value!);+ }}}).catch(() => undefined)@@ -161,6 +176,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {}private index(k: string, value: ChatStateValue): void {+ this.absent.delete(k);this.states.delete(k); // re-insert so the entry moves to the most-recent end even on updatethis.states.set(k, value);this.evictIfOverCap();