Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
perf(baileys): answer chat-state misses without a query per chat
Yudhi Armyndharisb6aecc13565f
diff --git a/src/engine/adapters/baileys-chat-state-store.service.spec.ts b/src/engine/adapters/baileys-chat-state-store.service.spec.tsindex 7bce53c26..5a106067f 100644--- a/src/engine/adapters/baileys-chat-state-store.service.spec.ts+++ b/src/engine/adapters/baileys-chat-state-store.service.spec.ts@@ -223,6 +223,131 @@ describe('ChatStateStoreService', () => {});});+ describe('a session whose rows all fit in the cache answers a miss without a query', () => {+ const seeded = () =>+ makeRepo([+ { sessionId: 's', chatId: 'a', archived: true },+ { sessionId: 's', chatId: 'b', pinned: true },+ ]);++ it('after refreshSession, an unknown chat reads undefined and costs no query', async () => {+ const repo = seeded();+ const svc = svcWith(repo);+ await svc.refreshSession('s');+ expect(svc.get('s', 'unknown')).toBeUndefined();+ await tick();+ expect(svc.get('s', 'unknown')).toBeUndefined();+ expect(svc.get('s', 'a')).toEqual(expect.objectContaining({ archived: true }));+ expect(repo.findOne).not.toHaveBeenCalled();+ });++ it('a write on a complete session is persisted and read back', async () => {+ const repo = seeded();+ const svc = svcWith(repo);+ await svc.refreshSession('s');+ await svc.remember('s', 'new', { archived: true });+ expect(repo.upsert).toHaveBeenCalledTimes(1);+ expect(svc.get('s', 'new')).toEqual(expect.objectContaining({ archived: true }));+ });++ it('forget and clearSession keep it complete', async () => {+ const repo = seeded();+ const svc = svcWith(repo);+ await svc.refreshSession('s');+ await svc.forget('s', ['a']);+ expect(svc.get('s', 'a')).toBeUndefined();+ await svc.clearSession('s');+ expect(svc.get('s', 'b')).toBeUndefined();+ expect(repo.findOne).not.toHaveBeenCalled();+ });++ it('reads through again once one of its rows is evicted', async () => {+ process.env[ENV] = '2';+ const repo = makeRepo([{ sessionId: 's', chatId: 'a', archived: true }]);+ const svc = svcWith(repo);+ await svc.refreshSession('s');+ await svc.remember('t', 'x', { pinned: true });+ await svc.remember('t', 'y', { pinned: true }); // cap 2: evicts s/a+ repo.findOne.mockClear();+ expect(svc.get('s', 'a')).toBeUndefined();+ await tick();+ expect(repo.findOne).toHaveBeenCalledTimes(1);+ expect(svc.get('s', 'a')).toEqual(expect.objectContaining({ archived: true }));+ });++ it('is not complete when the preload reached the cap', async () => {+ process.env[ENV] = '2';+ const repo = seeded();+ const svc = svcWith(repo);+ await svc.refreshSession('s');+ svc.get('s', 'unknown');+ expect(repo.findOne).toHaveBeenCalledTimes(1);+ });++ it('is not complete when the refresh read fails', async () => {+ const repo = seeded();+ const svc = svcWith(repo);+ await svc.refreshSession('s');+ repo.find.mockRejectedValueOnce(new Error('SQLITE_BUSY'));+ await svc.refreshSession('s');+ svc.get('s', 'unknown');+ expect(repo.findOne).toHaveBeenCalledTimes(1);+ });++ it('is not complete when a write overlapped the refresh read', async () => {+ const repo = seeded();+ let release!: () => void;+ repo.upsert.mockImplementationOnce(+ (v: ChatState) =>+ new Promise(resolve => {+ release = () => {+ repo.rows.set(KEY(v.sessionId, v.chatId), { ...v });+ resolve(undefined);+ };+ }),+ );+ const svc = svcWith(repo);+ const pending = svc.remember('s', 'late', { pinned: true });+ await tick(); // the write is indexed and waiting on its upsert+ await svc.refreshSession('s'); // the row is not in the table yet, so the refresh drops it+ release();+ await pending;+ expect(svc.get('s', 'late')).toBeUndefined();+ await tick();+ expect(svc.get('s', 'late')).toEqual(expect.objectContaining({ pinned: true }));+ });++ it('a write whose read-through failed leaves the session incomplete, so the row is read back', async () => {+ const repo = seeded();+ const svc = svcWith(repo);+ await svc.refreshSession('s');+ repo.findOne.mockRejectedValueOnce(new Error('SQLITE_BUSY'));+ // 'c' is not cached, so the write reads through first; that read fails and nothing is indexed.+ await svc.remember('s', 'c', { pinned: true });+ expect(svc.get('s', 'c')).toBeUndefined();+ await tick();+ expect(svc.get('s', 'c')).toEqual(expect.objectContaining({ pinned: true }));+ });++ it('reload marks every session of an untruncated preload complete, and none of a truncated one', async () => {+ const repo = makeRepo([+ { sessionId: 's', chatId: 'a', archived: true },+ { sessionId: 't', chatId: 'b', pinned: true },+ ]);+ const svc = svcWith(repo);+ await svc.reload();+ svc.get('s', 'unknown');+ svc.get('t', 'unknown');+ expect(repo.findOne).not.toHaveBeenCalled();++ process.env[ENV] = '2';+ const capped = svcWith(repo);+ await capped.reload();+ capped.get('s', 'unknown');+ expect(repo.findOne).toHaveBeenCalledTimes(1);+ });+ });+it('forget drops the named chats of one session from the table and the cache, after their pending writes', async () => {const repo = makeRepo([{ sessionId: 's', chatId: 'd', pinned: true },diff --git a/src/engine/adapters/baileys-chat-state-store.service.ts b/src/engine/adapters/baileys-chat-state-store.service.tsindex c24d23a26..22595a5f7 100644--- a/src/engine/adapters/baileys-chat-state-store.service.ts+++ b/src/engine/adapters/baileys-chat-state-store.service.ts@@ -35,7 +35,9 @@ const SEP = '\u0000'; // a null byte never appears in a session name or JID, so// One global LRU across all sessions, default 5000, matching the other engine maps. A// many-session deployment with large chat lists should raise BAILEYS_CHAT_STATE_CACHE_MAX; an evicted// row stays persisted and both paths read-through on a miss (the read warms lazily, the write merges-// the patch onto the persisted row), so eviction costs a re-read, never data loss.+// the patch onto the persisted row), so eviction costs a re-read, never data loss. The first read of an+// evicted row still answers undefined, so the chat list shows that chat's live record once; the first+// eviction is logged for that reason.export const CHAT_STATE_CACHE_DEFAULT = 5000;/**@@ -54,9 +56,22 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {/*** 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.+ * bounded by the same cap, and a key leaves it the moment a state is indexed for it. Only consulted+ * for a session that is not in {@link completeSessions}.*/private readonly absent = new Set<string>();+ /**+ * Sessions whose every persisted row is in `states`, so a miss there means "no row" without a query.+ * Proving absence key by key instead cost one query per never-muted chat per start, and once the+ * chats across sessions outgrew the cap the FIFO `absent` set cycled and every chat list queried+ * once per chat. A session joins after an untruncated load no write overlapped, and leaves when one+ * of its rows is evicted or a write could not be indexed; every other write indexes before it+ * persists, so the mark stays true.+ */+ private readonly completeSessions = new Set<string>();+ /** Bumped by every write that indexes or persists, so a load can tell a write overlapped its read. */+ private writeSeq = 0;+ private warnedEviction = false;private readonly maxEntries: number;/** One write chain per chat, so each remember() merges onto the state the previous one left. */private readonly writes = new KeyedMutationQueue();@@ -73,6 +88,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {}async reload(): Promise<void> {+ const seq = this.writeSeq;try {const rows = await this.repo.find({order: { updatedAt: 'DESC' },@@ -80,6 +96,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {});this.states.clear();this.absent.clear();+ this.completeSessions.clear();// Oldest first, so the newest row ends at the most-recent end of the LRU rather than the first// one evicted (the query is DESC only so `take` keeps the newest rows).for (const row of [...rows].reverse()) {@@ -89,6 +106,9 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {pinned: row.pinned,});}+ if (this.loadWasWhole(rows.length, seq)) {+ for (const row of rows) this.completeSessions.add(row.sessionId);+ }this.logger.log(`Loaded ${rows.length} chat states into cache${this.maxEntries ? ` (cap ${this.maxEntries})` : ''}`,);@@ -105,7 +125,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {this.states.set(k, value);return value;}- if (!this.absent.has(k)) this.warmFromTable(k, sessionId, chatId);+ if (!this.completeSessions.has(sessionId) && !this.absent.has(k)) this.warmFromTable(k, sessionId, chatId);return undefined;}@@ -140,6 +160,9 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {try {row = await this.repo.findOne({ where: { sessionId, chatId } });} catch {+ // Persisted but not indexed, so the session's cache no longer holds all of its rows.+ this.writeSeq++;+ this.completeSessions.delete(sessionId);await this.persist(sessionId, chatId, patch);this.absent.delete(k);return;@@ -155,6 +178,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {this.index(k, next); // warm the cache even on a no-op so the next read is a hitreturn; // nothing changed against the current state; skip the write that would just churn updatedAt}+ this.writeSeq++;this.index(k, next);await this.persist(sessionId, chatId, next);}@@ -210,6 +234,8 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {*/async refreshSession(sessionId: string): Promise<void> {const prefix = `${sessionId}${SEP}`;+ const seq = this.writeSeq;+ this.completeSessions.delete(sessionId);let rows: ChatState[] | undefined;try {rows = await this.repo.find({@@ -237,6 +263,17 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {pinned: row.pinned,});}+ // Marked after the loop: indexing these rows can evict other sessions' keys, never this one's.+ if (this.loadWasWhole(rows.length, seq)) this.completeSessions.add(sessionId);+ }++ /**+ * True when a load holds every row it asked for and no write ran meanwhile. A write in flight during+ * the read may be missing from `rows` while its key was just dropped from the cache, so the session+ * cannot be vouched for; skipping the mark only keeps the read-through path.+ */+ private loadWasWhole(count: number, seq: number): boolean {+ return (this.maxEntries === 0 || count < this.maxEntries) && this.writeSeq === seq && this.writes.size === 0;}/** Warm a cache miss from the table. This lookup still returns undefined (the read cannot await); the next hits. */@@ -273,6 +310,13 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {const oldest = this.states.keys().next().value;if (oldest === undefined) break;this.states.delete(oldest);+ this.completeSessions.delete(oldest.slice(0, oldest.indexOf(SEP)));+ if (!this.warnedEviction) {+ this.warnedEviction = true;+ this.logger.warn(+ `Chat state cache is full (${this.maxEntries}); raise BAILEYS_CHAT_STATE_CACHE_MAX so pin, mute and archive stay cached`,+ );+ }}}