ioredis 5.2.0 → 5.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -10,6 +10,25 @@ class ClusterSubscriber {
|
|
|
10
10
|
this.emitter = emitter;
|
|
11
11
|
this.started = false;
|
|
12
12
|
this.subscriber = null;
|
|
13
|
+
this.onSubscriberEnd = () => {
|
|
14
|
+
if (!this.started) {
|
|
15
|
+
debug("subscriber has disconnected, but ClusterSubscriber is not started, so not reconnecting.");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
// If the subscriber closes whilst it's still the active connection,
|
|
19
|
+
// we might as well try to connecting to a new node if possible to
|
|
20
|
+
// minimise the number of missed publishes.
|
|
21
|
+
debug("subscriber has disconnected, selecting a new one...");
|
|
22
|
+
this.selectSubscriber();
|
|
23
|
+
};
|
|
24
|
+
// If the current node we're using as the subscriber disappears
|
|
25
|
+
// from the node pool for some reason, we will select a new one
|
|
26
|
+
// to connect to.
|
|
27
|
+
// Note that this event is only triggered if the connection to
|
|
28
|
+
// the node has been used; cluster subscriptions are setup with
|
|
29
|
+
// lazyConnect = true. It's possible for the subscriber node to
|
|
30
|
+
// disappear without this method being called!
|
|
31
|
+
// See https://github.com/luin/ioredis/pull/1589
|
|
13
32
|
this.connectionPool.on("-node", (_, key) => {
|
|
14
33
|
if (!this.started || !this.subscriber) {
|
|
15
34
|
return;
|
|
@@ -48,9 +67,11 @@ class ClusterSubscriber {
|
|
|
48
67
|
// Disconnect the previous subscriber even if there
|
|
49
68
|
// will not be a new one.
|
|
50
69
|
if (lastActiveSubscriber) {
|
|
70
|
+
lastActiveSubscriber.off("end", this.onSubscriberEnd);
|
|
51
71
|
lastActiveSubscriber.disconnect();
|
|
52
72
|
}
|
|
53
73
|
if (this.subscriber) {
|
|
74
|
+
this.subscriber.off("end", this.onSubscriberEnd);
|
|
54
75
|
this.subscriber.disconnect();
|
|
55
76
|
}
|
|
56
77
|
const sampleNode = (0, utils_1.sample)(this.connectionPool.getNodes());
|
|
@@ -79,9 +100,19 @@ class ClusterSubscriber {
|
|
|
79
100
|
connectionName: (0, util_1.getConnectionName)("subscriber", options.connectionName),
|
|
80
101
|
lazyConnect: true,
|
|
81
102
|
tls: options.tls,
|
|
103
|
+
// Don't try to reconnect the subscriber connection. If the connection fails
|
|
104
|
+
// we will get an end event (handled below), at which point we'll pick a new
|
|
105
|
+
// node from the pool and try to connect to that as the subscriber connection.
|
|
106
|
+
retryStrategy: null
|
|
82
107
|
});
|
|
83
108
|
// Ignore the errors since they're handled in the connection pool.
|
|
84
109
|
this.subscriber.on("error", utils_1.noop);
|
|
110
|
+
// The node we lost connection to may not come back up in a
|
|
111
|
+
// reasonable amount of time (e.g. a slave that's taken down
|
|
112
|
+
// for maintainence), we could potentially miss many published
|
|
113
|
+
// messages so we should reconnect as quickly as possible, to
|
|
114
|
+
// a different node if needed.
|
|
115
|
+
this.subscriber.once("end", this.onSubscriberEnd);
|
|
85
116
|
// Re-subscribe previous channels
|
|
86
117
|
const previousChannels = { subscribe: [], psubscribe: [] };
|
|
87
118
|
if (lastActiveSubscriber) {
|