Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(integration): yield between retention hand-off rows
Yudhi Armyndharis968fe70b19b5
diff --git a/src/modules/integration/ingress-reconciler.service.ts b/src/modules/integration/ingress-reconciler.service.tsindex 305af6fc1..5cdcb43c7 100644--- a/src/modules/integration/ingress-reconciler.service.ts+++ b/src/modules/integration/ingress-reconciler.service.ts@@ -1,3 +1,4 @@+import { setImmediate as yieldToEventLoop } from 'node:timers/promises';import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';import { isUUID } from 'class-validator';import { InjectRepository } from '@nestjs/typeorm';@@ -261,6 +262,9 @@ export class IngressReconcilerService implements OnModuleInit, OnModuleDestroy {take: batchSize,});for (const row of rows) {+ // A synchronous database driver (the SQLite default) answers every statement at once, so without+ // this the pass over a large backlog never lets a timer, request or keep-alive run.+ await yieldToEventLoop();if (this.stop.signal.aborted) return retired;if (!hasPayload(row)) {kept.push(row.id);diff --git a/src/modules/integration/integration-retention.service.spec.ts b/src/modules/integration/integration-retention.service.spec.tsindex ef437eb25..08901da67 100644--- a/src/modules/integration/integration-retention.service.spec.ts+++ b/src/modules/integration/integration-retention.service.spec.ts@@ -612,6 +612,26 @@ describe('IntegrationRetentionService.pruneOlderThan with undispatched events',expect(await failures.count()).toBe(250);});+ it('lets the event loop run between rows, which a synchronous driver would otherwise starve for the whole pass', async () => {+ for (let i = 0; i < 120; i++) await insertEvent(`y${i}`, {});+ // better-sqlite3 answers every statement at once, so without a yield the pass is one unbroken chain of+ // microtasks: no timer, request or keep-alive callback runs until it ends, however many rows it holds.+ let ticks = 0;+ let probing = true;+ const probe = (): void => {+ if (!probing) return;+ ticks += 1;+ setImmediate(probe);+ };+ setImmediate(probe);++ await service.pruneOlderThan(7, 90);+ probing = false;++ expect(await events.count({ where: { dispatchState: 'failed' } })).toBe(120);+ expect(ticks).toBeGreaterThan(60);+ });+it('prunes aged settled markers while the hand-off is still waiting', async () => {await insertEvent('dispatched', { dispatchState: 'dispatched', payload: null });await insertEvent('stranded', {});