Skip to content

Commit

Permalink
fix: remove end event listener before disconnecting the old subscribe…
Browse files Browse the repository at this point in the history
…r to prevent dead loop
  • Loading branch information
headlessme committed Jul 13, 2022
1 parent 68457a6 commit 71adbb8
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions lib/cluster/ClusterSubscriber.ts
Expand Up @@ -62,6 +62,18 @@ export default class ClusterSubscriber {
debug("stopped");
}

private onSubscriberEnd = () => {
if (!this.started) {
debug("subscriber has disconnected, but ClusterSubscriber is not started, so not reconnecting.");
return;
}
// If the subscriber closes whilst it's still the active connection,
// we might as well try to connecting to a new node if possible to
// minimise the number of missed publishes.
debug("subscriber has disconnected, selecting a new one...");
this.selectSubscriber();
}

private selectSubscriber() {
const lastActiveSubscriber = this.lastActiveSubscriber;

Expand All @@ -72,6 +84,7 @@ export default class ClusterSubscriber {
}

if (this.subscriber) {
this.subscriber.off("end", this.onSubscriberEnd);
this.subscriber.disconnect();
}

Expand Down Expand Up @@ -119,20 +132,7 @@ export default class ClusterSubscriber {
// for maintainence), we could potentially miss many published
// messages so we should reconnect as quickly as possible, to
// a different node if needed.
const currentSub = this.subscriber;
this.subscriber.once("end", () => {
if (!this.started) {
debug("subscriber has disconnected, but ClusterSubscriber is not started, so not reconnecting.");
return;
}
// If the subscriber closes whilst it's still the active connection,
// we might as well try to connecting to a new node if possible to
// minimise the number of missed publishes.
if (currentSub === this.subscriber) {
debug("subscriber has disconnected, selecting a new one...");
this.selectSubscriber();
}
});
this.subscriber.once("end", this.onSubscriberEnd);

// Re-subscribe previous channels
const previousChannels = { subscribe: [], psubscribe: [] };
Expand Down

0 comments on commit 71adbb8

Please sign in to comment.