Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(events): keep the WS subscriber queued through a boot outage

Yudhi Armyndharis117d5e177a47
diff --git a/src/modules/events/redis-io.adapter.spec.ts b/src/modules/events/redis-io.adapter.spec.ts
index 5d97f2f7a..1078104b2 100644
--- a/src/modules/events/redis-io.adapter.spec.ts
+++ b/src/modules/events/redis-io.adapter.spec.ts
@@ -18,8 +18,9 @@ class FakeRedis {
constructor(public readonly opts?: unknown) {
redisInstances.push(this);
}
- duplicate(): FakeRedis {
- return new FakeRedis(this.opts);
+ // Like ioredis, a duplicate inherits the options and takes overrides on top.
+ duplicate(override?: object): FakeRedis {
+ return new FakeRedis({ ...(this.opts as object), ...override });
}
on(event: string, handler: (err: Error) => void): this {
this.handlers[event] = handler;
@@ -135,6 +136,21 @@ describe('RedisIoAdapter', () => {
}
});
+ it('keeps the subscriber commands queued for as long as Redis is down at boot', () => {
+ process.env.REDIS_ENABLED = 'true';
+ const spy = withBaseServer(fakeServer().server);
+ try {
+ new RedisIoAdapter({} as never).createIOServer(2785);
+ const [pub, sub] = redisInstances as [FakeRedis, FakeRedis];
+ // The adapter subscribes once; a flushed SUBSCRIBE is never sent again, so the sub client
+ // must not give up on it. The publisher stays bounded.
+ expect((sub.opts as { maxRetriesPerRequest?: unknown }).maxRetriesPerRequest).toBeNull();
+ expect((pub.opts as { maxRetriesPerRequest?: unknown }).maxRetriesPerRequest).toBeUndefined();
+ } finally {
+ spy.mockRestore();
+ }
+ });
+
it('gives a failed publish an owner, since the adapter drops the promise it returns', async () => {
process.env.REDIS_ENABLED = 'true';
const { server } = fakeServer();
diff --git a/src/modules/events/redis-io.adapter.ts b/src/modules/events/redis-io.adapter.ts
index c5a55d5a1..cfb1cde8f 100644
--- a/src/modules/events/redis-io.adapter.ts
+++ b/src/modules/events/redis-io.adapter.ts
@@ -48,7 +48,7 @@ export function wsRedisOptions(): RedisOptions {
*
* Failure posture: if the pub/sub clients cannot be created the server falls back to the in-memory
* adapter — the local node keeps working, only cross-node fan-out is lost — rather than refusing to
- * boot. A Redis outage after boot is ioredis's problem to retry; the adapter recovers on reconnect.
+ * boot. A Redis outage, at boot or after it, is ioredis's problem to retry; the adapter recovers on reconnect.
*/
export class RedisIoAdapter extends IoAdapter {
private pubClient?: Redis;
@@ -64,7 +64,11 @@ export class RedisIoAdapter extends IoAdapter {
try {
const pubClient = new Redis(wsRedisOptions());
- const subClient = pubClient.duplicate();
+ // The adapter issues its SUBSCRIBE/PSUBSCRIBE once, at construction. With a bounded per-request
+ // retry, an outage at boot flushes them before Redis is ever reached, and ioredis only replays
+ // subscriptions the server confirmed, so the replica would never subscribe. This connection
+ // carries nothing else, so keep its commands queued until the first connect.
+ const subClient = pubClient.duplicate({ maxRetriesPerRequest: null });
for (const [name, client] of [
['pub', pubClient],
['sub', subClient],