Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(stats): drop expired memo entries as results are stored

Yudhi Armyndharis7962359bcf09
diff --git a/src/modules/stats/stats-cache-lifetime.spec.ts b/src/modules/stats/stats-cache-lifetime.spec.ts
new file mode 100644
index 000000000..108217e3e
--- /dev/null
+++ b/src/modules/stats/stats-cache-lifetime.spec.ts
@@ -0,0 +1,47 @@
+import { DataSource } from 'typeorm';
+import { StatsService } from './stats.service';
+import { Session } from '../session/entities/session.entity';
+import { Message } from '../message/entities/message.entity';
+
+describe('StatsService memo lifetime', () => {
+ let ds: DataSource;
+ let service: StatsService;
+ const keys = () => [...(service as unknown as { memo: Map<string, unknown> }).memo.keys()];
+
+ beforeEach(async () => {
+ ds = new DataSource({
+ type: 'better-sqlite3',
+ database: ':memory:',
+ entities: [Session, Message],
+ synchronize: true,
+ });
+ await ds.initialize();
+ const config = { get: (_key: string, fallback: number) => (_key === 'stats.cacheTtlMs' ? 1000 : fallback) };
+ service = new StatsService(ds.getRepository(Session), ds.getRepository(Message), config as never);
+ for (const id of ['a', 'b']) await ds.getRepository(Session).save({ id, name: id, config: {} });
+ });
+
+ afterEach(async () => {
+ jest.restoreAllMocks();
+ await ds.destroy();
+ });
+
+ it('drops the expired entry of a deleted session once another one is computed', async () => {
+ const start = Date.now();
+ await service.getSessionStats('a');
+ await ds.getRepository(Session).delete({ id: 'a' });
+ expect(keys()).toEqual(['session:a']);
+
+ jest.spyOn(Date, 'now').mockReturnValue(start + 5000);
+ await service.getSessionStats('b');
+
+ expect(keys()).toEqual(['session:b']);
+ });
+
+ it('keeps an entry that has not expired', async () => {
+ await service.getSessionStats('a');
+ await service.getSessionStats('b');
+
+ expect(keys().sort()).toEqual(['session:a', 'session:b']);
+ });
+});
diff --git a/src/modules/stats/stats.service.ts b/src/modules/stats/stats.service.ts
index c386366a0..9e0424136 100644
--- a/src/modules/stats/stats.service.ts
+++ b/src/modules/stats/stats.service.ts
@@ -91,8 +91,8 @@ export class StatsService {
* dashboard polling would otherwise re-run them on every request. TTL-only invalidation:
* entries expire after stats.cacheTtlMs; there is no write-path hook. Session-scoped entries
* are additionally re-validated on serve (getSessionStats), so a deleted session is not
- * resurrected from the memo. Key cardinality is
- * bounded (4 global shapes + one per session), so no size eviction is needed. Concurrent misses
+ * resurrected from the memo. Every finished computation also drops the entries that have expired, so
+ * the entries of deleted sessions nobody asks for again do not accumulate. Concurrent misses
* on one key share a single computation (`inflight`), so an expired entry costs one scan, not one
* per waiting request; a rejection is handed to every waiter and never stored.
*/
@@ -134,7 +134,9 @@ export class StatsService {
value => {
if (this.inflight.get(key) === flight) {
this.inflight.delete(key);
- this.memo.set(key, { expiresAt: Date.now() + ttl, value });
+ const now = Date.now();
+ for (const [memoKey, entry] of this.memo) if (entry.expiresAt <= now) this.memo.delete(memoKey);
+ this.memo.set(key, { expiresAt: now + ttl, value });
}
return value;
},