Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): evict an expired chat and contact list on a timer

Yudhi Armyndharis038e976c8b26
diff --git a/src/common/utils/listing-memo.spec.ts b/src/common/utils/listing-memo.spec.ts
index b4afb9008..3ca1155aa 100644
--- a/src/common/utils/listing-memo.spec.ts
+++ b/src/common/utils/listing-memo.spec.ts
@@ -88,6 +88,87 @@ describe('ListingMemo', () => {
expect(load).toHaveBeenCalledTimes(4);
});
+ describe('eviction of an idle list', () => {
+ const entryCount = (memo: ListingMemo<{ id: string }>) => memo['entries'].size;
+
+ beforeEach(() => jest.useFakeTimers());
+ afterEach(() => jest.useRealTimers());
+
+ it('releases an expired list although no session reads it again', async () => {
+ const memo = new ListingMemo<{ id: string }>();
+ const load = list('a');
+ await memo.read('s1', 0, load);
+ await memo.read('s2', 0, load);
+ jest.advanceTimersByTime(LISTING_MEMO_TTL_MS - 1);
+ expect(entryCount(memo)).toBe(2);
+ jest.advanceTimersByTime(1);
+ expect(entryCount(memo)).toBe(0);
+ await memo.read('s1', 1000, load);
+ expect(load).toHaveBeenCalledTimes(3);
+ });
+
+ it('starts the clock when the read ends, not when it starts', async () => {
+ const memo = new ListingMemo<{ id: string }>();
+ let finish: (items: { id: string }[]) => void = () => undefined;
+ const pending = memo.read('s1', 0, () => new Promise(resolve => (finish = resolve)));
+ jest.advanceTimersByTime(LISTING_MEMO_TTL_MS * 3);
+ expect(entryCount(memo)).toBe(1);
+ finish([{ id: 'a' }]);
+ await pending;
+ jest.advanceTimersByTime(LISTING_MEMO_TTL_MS - 1);
+ expect(entryCount(memo)).toBe(1);
+ jest.advanceTimersByTime(1);
+ expect(entryCount(memo)).toBe(0);
+ });
+
+ it('clears the timer when the entry is dropped', async () => {
+ const memo = new ListingMemo<{ id: string }>();
+ await memo.read('s1', 0, list('a'));
+ expect(jest.getTimerCount()).toBe(1);
+ memo.drop('s1');
+ expect(jest.getTimerCount()).toBe(0);
+ expect(entryCount(memo)).toBe(0);
+ });
+
+ it('leaves no timer behind for a drop in flight or a failed read', async () => {
+ const memo = new ListingMemo<{ id: string }>();
+ let finish: (items: { id: string }[]) => void = () => undefined;
+ const pending = memo.read('s1', 0, () => new Promise(resolve => (finish = resolve)));
+ memo.drop('s1');
+ finish([{ id: 'a' }]);
+ await pending;
+ await expect(memo.read('s2', 0, () => Promise.reject(new Error('page died')))).rejects.toThrow('page died');
+ expect(jest.getTimerCount()).toBe(0);
+ expect(entryCount(memo)).toBe(0);
+ });
+
+ it('does not let the timer of a replaced list evict the list that replaced it', async () => {
+ const memo = new ListingMemo<{ id: string }>();
+ const load = list('a');
+ await memo.read('s1', 0, load);
+ jest.advanceTimersByTime(LISTING_MEMO_TTL_MS - 1000);
+ await memo.read('s1', 0, load);
+ expect(jest.getTimerCount()).toBe(1);
+ jest.advanceTimersByTime(1000);
+ expect(entryCount(memo)).toBe(1);
+ await memo.read('s1', 1000, load);
+ expect(load).toHaveBeenCalledTimes(2);
+ jest.advanceTimersByTime(LISTING_MEMO_TTL_MS - 1000);
+ expect(entryCount(memo)).toBe(0);
+ });
+ });
+
+ it('does not let the eviction timer keep the process alive', async () => {
+ const set = jest.spyOn(global, 'setTimeout');
+ await new ListingMemo<{ id: string }>(12_345).read('s1', 0, list('a'));
+ const timers = set.mock.calls.flatMap(([, delay], i) =>
+ delay === 12_345 ? [set.mock.results[i].value as NodeJS.Timeout] : [],
+ );
+ expect(timers).toHaveLength(1);
+ expect(timers[0].hasRef()).toBe(false);
+ clearTimeout(timers[0]);
+ });
+
it('hands every caller its own copy of the list', async () => {
const memo = new ListingMemo<{ id: string }>();
const first = await memo.read('s1', 0, list('a', 'b'));
diff --git a/src/common/utils/listing-memo.ts b/src/common/utils/listing-memo.ts
index 4ad4360e2..7b680e411 100644
--- a/src/common/utils/listing-memo.ts
+++ b/src/common/utils/listing-memo.ts
@@ -1,6 +1,12 @@
/** How long a session's full listing stays reusable for the later pages of the walk that read it. */
export const LISTING_MEMO_TTL_MS = 5_000;
+interface Entry<T> {
+ items: Promise<T[]>;
+ expiresAt: number;
+ evictor?: NodeJS.Timeout;
+}
+
/**
* A session's last full engine listing (every chat, or every contact), kept a few seconds so the pages
* of one walk (offset 0, 1000, 2000 ...) share one engine read instead of each re-reading the whole
@@ -9,22 +15,28 @@ export const LISTING_MEMO_TTL_MS = 5_000;
* Only a later page reuses it. A read at offset 0 starts a walk and always goes to the engine, so a
* reload after a live event never starts from an old list. It holds the raw list of one session and
* nothing else: every request applies its own chat-scope filter and window after reading it. Reads in
- * flight are shared, a failed read is not kept, and the owner drops a session's entry when a write may
- * have changed the list or the session's engine stops being the live one.
+ * flight are shared, a failed read is not kept, an expired list is evicted by a timer that never keeps
+ * the process alive, and the owner drops a session's entry when a write may have changed the list or
+ * the session's engine stops being the live one.
*/
export class ListingMemo<T> {
- private readonly entries = new Map<string, { items: Promise<T[]>; expiresAt: number }>();
+ private readonly entries = new Map<string, Entry<T>>();
constructor(private readonly ttlMs = LISTING_MEMO_TTL_MS) {}
async read(sessionId: string, offset: number | undefined, load: () => Promise<T[]>): Promise<T[]> {
let entry = this.entries.get(sessionId);
if (!(entry && (offset ?? 0) > 0 && entry.expiresAt > Date.now())) {
- const fresh = { items: load(), expiresAt: Infinity };
+ clearTimeout(entry?.evictor);
+ const fresh: Entry<T> = { items: load(), expiresAt: Infinity };
this.entries.set(sessionId, fresh);
fresh.items.then(
() => {
fresh.expiresAt = Date.now() + this.ttlMs;
+ // An idle session is never read again, so an expired list has to leave the heap on its own.
+ if (this.entries.get(sessionId) === fresh) {
+ fresh.evictor = setTimeout(() => this.entries.delete(sessionId), this.ttlMs).unref();
+ }
},
() => {
if (this.entries.get(sessionId) === fresh) this.entries.delete(sessionId);
@@ -37,6 +49,7 @@ export class ListingMemo<T> {
}
drop(sessionId: string): void {
+ clearTimeout(this.entries.get(sessionId)?.evictor);
this.entries.delete(sessionId);
}
}