From aa9c5b1fee5daa24f35b3ff0d3556ecfb86db251 Mon Sep 17 00:00:00 2001 From: Leibale Eidelman Date: Mon, 3 May 2021 12:20:51 -0400 Subject: [PATCH] fix(cluster): avoid ClusterAllFailedError in certain cases Closes #1330 Revert 8524eea. Before 4.24.1, ioredis asked cluster nodes for cluster slot information when connecting and periodically after connected. If all cluster nodes failed to provide the information (ex all nodes were down), ioredis would raise the "Failed to refresh slots cache" error and reconnect to the cluster (and print debug log Reset with [] ) if it hadn't connected, otherwise (when running periodically) it would just ignore. After 4.24.1, ioredis will raise and reconnect to the cluster even the cluster has already connected. This change is introduced to make failover detection faster. However, the commit causes `ClusterAllFailedError` in certain cases so we'll revert this and find other solutions. --- lib/cluster/index.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index 914cb87f..86bdae5f 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -266,7 +266,14 @@ class Cluster extends EventEmitter { this.once("close", closeListener); this.once("close", this.handleCloseEvent.bind(this)); - this.refreshSlotsCache(); + this.refreshSlotsCache( + function (err) { + if (err && err.message === "Failed to refresh slots cache.") { + Redis.prototype.silentEmit.call(this, "error", err); + this.connectionPool.reset([]); + } + }.bind(this) + ); this.subscriber.start(); }) .catch((err) => { @@ -513,8 +520,6 @@ class Cluster extends EventEmitter { "Failed to refresh slots cache.", lastNodeError ); - Redis.prototype.silentEmit.call(_this, "error", error); - _this.connectionPool.reset([]); return wrapper(error); } const node = nodes[index];