Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

perf(webhook): run the delivery Worker with concurrency > 1 (#511)

Yudhi Armyndharis4f55cb66f2cb
diff --git a/src/modules/queue/processors/webhook.processor.ts b/src/modules/queue/processors/webhook.processor.ts
index 118730ac1..05f67f9e0 100644
--- a/src/modules/queue/processors/webhook.processor.ts
+++ b/src/modules/queue/processors/webhook.processor.ts
@@ -5,7 +5,7 @@ import { ConfigService } from '@nestjs/config';
import { Repository } from 'typeorm';
import { createLogger } from '../../../common/services/logger.service';
import { QUEUE_NAMES } from '../queue-names';
-import { workerConnectionOptions } from '../redis-connection';
+import { workerConnectionOptions, webhookWorkerConcurrency } from '../redis-connection';
import { WebhookJobData } from '../../webhook/webhook.service';
import { Webhook } from '../../webhook/entities/webhook.entity';
import { HookManager } from '../../../core/hooks';
@@ -19,8 +19,10 @@ export interface WebhookJobResult {
}
// Override the Worker's connection so it does NOT inherit the producer's `enableOfflineQueue: false`
-// from the shared BullModule connection — the Worker must tolerate a brief Redis reconnect.
-@Processor(QUEUE_NAMES.WEBHOOK, { connection: workerConnectionOptions() })
+// from the shared BullModule connection — the Worker must tolerate a brief Redis reconnect. Set an
+// explicit concurrency: BullMQ defaults a Worker to 1, which serializes every session's webhook
+// deliveries behind one slow/timing-out receiver.
+@Processor(QUEUE_NAMES.WEBHOOK, { connection: workerConnectionOptions(), concurrency: webhookWorkerConcurrency() })
export class WebhookProcessor extends WorkerHost {
private readonly logger = createLogger('WebhookProcessor');
diff --git a/src/modules/queue/redis-connection.spec.ts b/src/modules/queue/redis-connection.spec.ts
index 7cb87b4ee..aee6945bd 100644
--- a/src/modules/queue/redis-connection.spec.ts
+++ b/src/modules/queue/redis-connection.spec.ts
@@ -1,4 +1,4 @@
-import { workerConnectionOptions } from './redis-connection';
+import { workerConnectionOptions, webhookWorkerConcurrency } from './redis-connection';
describe('workerConnectionOptions (webhook Worker connection)', () => {
const ORIGINAL_ENV = process.env;
@@ -40,3 +40,28 @@ describe('workerConnectionOptions (webhook Worker connection)', () => {
});
});
});
+
+describe('webhookWorkerConcurrency', () => {
+ const ORIGINAL_ENV = process.env;
+ afterEach(() => {
+ process.env = ORIGINAL_ENV;
+ });
+
+ it('defaults to 10 so deliveries do not serialize behind one slow receiver', () => {
+ process.env = { ...ORIGINAL_ENV };
+ delete process.env.WEBHOOK_WORKER_CONCURRENCY;
+ expect(webhookWorkerConcurrency()).toBe(10);
+ });
+
+ it('honors a positive override', () => {
+ process.env = { ...ORIGINAL_ENV, WEBHOOK_WORKER_CONCURRENCY: '25' };
+ expect(webhookWorkerConcurrency()).toBe(25);
+ });
+
+ it('falls back to the default for a non-positive/garbage override', () => {
+ process.env = { ...ORIGINAL_ENV, WEBHOOK_WORKER_CONCURRENCY: '0' };
+ expect(webhookWorkerConcurrency()).toBe(10);
+ process.env = { ...ORIGINAL_ENV, WEBHOOK_WORKER_CONCURRENCY: 'abc' };
+ expect(webhookWorkerConcurrency()).toBe(10);
+ });
+});
diff --git a/src/modules/queue/redis-connection.ts b/src/modules/queue/redis-connection.ts
index c0d9acf38..598ad4fc9 100644
--- a/src/modules/queue/redis-connection.ts
+++ b/src/modules/queue/redis-connection.ts
@@ -28,3 +28,18 @@ export function workerConnectionOptions(): WorkerConnectionOptions {
connectTimeout: parseInt(process.env.REDIS_CONNECT_TIMEOUT_MS || '5000', 10),
};
}
+
+/** Default number of webhook deliveries the Worker processes in parallel. */
+const DEFAULT_WEBHOOK_WORKER_CONCURRENCY = 10;
+
+/**
+ * Webhook Worker concurrency. BullMQ defaults a Worker to 1, which serializes ALL webhook deliveries
+ * process-wide: one slow or timing-out receiver head-of-line-blocks every other session's webhooks
+ * until it finishes (up to WEBHOOK_TIMEOUT + retries). Running several in parallel decouples healthy
+ * receivers from a stuck one. Override via WEBHOOK_WORKER_CONCURRENCY; a non-positive/garbage value
+ * falls back to the default. (Read at module import like workerConnectionOptions above.)
+ */
+export function webhookWorkerConcurrency(): number {
+ const parsed = parseInt(process.env.WEBHOOK_WORKER_CONCURRENCY || '', 10);
+ return Number.isInteger(parsed) && parsed > 0 ? parsed : DEFAULT_WEBHOOK_WORKER_CONCURRENCY;
+}