Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue for subscriptions where prevCondition is a subscriber #5

Open
wants to merge 3 commits into
base: peakon-release-latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions built/cluster/ClusterSubscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ClusterSubscriber {
if (!this.started || !this.subscriber) {
return;
}
// @ts-expect-error
if ((0, util_1.getNodeKey)(this.subscriber.options) === key) {
debug("subscriber has left, selecting a new one...");
this.selectSubscriber();
Expand Down Expand Up @@ -63,6 +64,7 @@ class ClusterSubscriber {
debug("stopped");
}
selectSubscriber() {
var _a, _b;
const lastActiveSubscriber = this.lastActiveSubscriber;
// Disconnect the previous subscriber even if there
// will not be a new one.
Expand Down Expand Up @@ -116,13 +118,12 @@ class ClusterSubscriber {
// Re-subscribe previous channels
const previousChannels = { subscribe: [], psubscribe: [], ssubscribe: [] };
if (lastActiveSubscriber) {
const condition = lastActiveSubscriber.condition || lastActiveSubscriber.prevCondition;
if (condition && condition.subscriber) {
previousChannels.subscribe = condition.subscriber.channels("subscribe");
previousChannels.psubscribe =
condition.subscriber.channels("psubscribe");
previousChannels.ssubscribe =
condition.subscriber.channels("ssubscribe");
const subscriber = ((_a = lastActiveSubscriber.condition) === null || _a === void 0 ? void 0 : _a.subscriber) ||
((_b = lastActiveSubscriber.prevCondition) === null || _b === void 0 ? void 0 : _b.subscriber);
if (subscriber) {
previousChannels.subscribe = subscriber.channels("subscribe");
previousChannels.psubscribe = subscriber.channels("psubscribe");
previousChannels.ssubscribe = subscriber.channels("ssubscribe");
}
}
if (previousChannels.subscribe.length ||
Expand Down
22 changes: 12 additions & 10 deletions lib/cluster/ClusterSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import ConnectionPool from "./ConnectionPool";
import { getConnectionName, getNodeKey } from "./util";
import { sample, noop, Debug } from "../utils";
import Redis from "../Redis";
import { Condition } from "../DataHandler";

const debug = Debug("cluster:subscriber");

export default class ClusterSubscriber {
private started = false;
private subscriber: any = null;
private lastActiveSubscriber: any;
private subscriber: Redis | null = null;
private lastActiveSubscriber: Redis & { prevCondition?: Condition };

constructor(
private connectionPool: ConnectionPool,
Expand All @@ -27,6 +28,7 @@ export default class ClusterSubscriber {
if (!this.started || !this.subscriber) {
return;
}
// @ts-expect-error
if (getNodeKey(this.subscriber.options) === key) {
debug("subscriber has left, selecting a new one...");
this.selectSubscriber();
Expand Down Expand Up @@ -140,14 +142,14 @@ export default class ClusterSubscriber {
// Re-subscribe previous channels
const previousChannels = { subscribe: [], psubscribe: [], ssubscribe: [] };
if (lastActiveSubscriber) {
const condition =
lastActiveSubscriber.condition || lastActiveSubscriber.prevCondition;
if (condition && condition.subscriber) {
previousChannels.subscribe = condition.subscriber.channels("subscribe");
previousChannels.psubscribe =
condition.subscriber.channels("psubscribe");
previousChannels.ssubscribe =
condition.subscriber.channels("ssubscribe");
const subscriber =
lastActiveSubscriber.condition?.subscriber ||
lastActiveSubscriber.prevCondition?.subscriber;

if (subscriber) {
previousChannels.subscribe = subscriber.channels("subscribe");
previousChannels.psubscribe = subscriber.channels("psubscribe");
previousChannels.ssubscribe = subscriber.channels("ssubscribe");
}
}
if (
Expand Down