Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(events): catch failed Redis publishes from the WS adapter
Yudhi Armyndharisb0b28a205096
diff --git a/src/modules/events/redis-io.adapter.spec.ts b/src/modules/events/redis-io.adapter.spec.tsindex 99243d732..5d97f2f7a 100644--- a/src/modules/events/redis-io.adapter.spec.ts+++ b/src/modules/events/redis-io.adapter.spec.ts@@ -11,6 +11,10 @@ class FakeRedis {readonly handlers: Record<string, (err: Error) => void> = {};quit = jest.fn().mockResolvedValue('OK');disconnect = jest.fn();+ readonly sent = jest.fn().mockResolvedValue(1);+ publish(channel: string, message: string): Promise<number> {+ return this.sent(channel, message) as Promise<number>;+ }constructor(public readonly opts?: unknown) {redisInstances.push(this);}@@ -131,6 +135,23 @@ describe('RedisIoAdapter', () => {}});+ it('gives a failed publish an owner, since the adapter drops the promise it returns', async () => {+ process.env.REDIS_ENABLED = 'true';+ const { server } = fakeServer();+ const spy = withBaseServer(server);+ try {+ new RedisIoAdapter({} as never).createIOServer(2785);+ const [pub] = redisInstances;+ // What ioredis does to every publish queued during an outage once its retries run out.+ pub.sent.mockRejectedValueOnce(new Error('Reached the max retries per request limit (which is 20).'));+ await expect(pub.publish('socket.io#/events#', 'frame')).resolves.toBe(0);+ await expect(pub.publish('socket.io#/events#', 'frame')).resolves.toBe(1);+ expect(pub.sent).toHaveBeenCalledWith('socket.io#/events#', 'frame');+ } finally {+ spy.mockRestore();+ }+ });+it('falls back to the base server (no crash) if wiring the adapter throws', () => {process.env.REDIS_ENABLED = 'true';const { server, adapterFn } = fakeServer();diff --git a/src/modules/events/redis-io.adapter.ts b/src/modules/events/redis-io.adapter.tsindex 70bdcaec6..c5a55d5a1 100644--- a/src/modules/events/redis-io.adapter.ts+++ b/src/modules/events/redis-io.adapter.ts@@ -71,6 +71,16 @@ export class RedisIoAdapter extends IoAdapter {] as const) {client.on('error', err => logger.warn(`Redis ${name} client error: ${err.message}`));}+ // The adapter fires publish() on every broadcast and drops the promise. During an outage ioredis+ // queues those commands and, each time its retries run out, rejects the whole queue, so every+ // event emitted meanwhile surfaced as an unhandled rejection. The 'error' listener above already+ // reports the outage once; a lost fan-out frame is only worth a debug line.+ const publish = pubClient.publish.bind(pubClient);+ pubClient.publish = (channel: string, message: string | Buffer) =>+ publish(channel, message).catch((err: Error) => {+ logger.debug(`Redis publish dropped: ${err.message}`);+ return 0;+ });this.pubClient = pubClient;this.subClient = subClient;server.adapter(createAdapter(pubClient, subClient));