* Connect to a cluster
()
| 189 | * Connect to a cluster |
| 190 | */ |
| 191 | connect(): Promise<void> { |
| 192 | return new Promise((resolve, reject) => { |
| 193 | if ( |
| 194 | this.status === "connecting" || |
| 195 | this.status === "connect" || |
| 196 | this.status === "ready" |
| 197 | ) { |
| 198 | reject(new Error("Redis is already connecting/connected")); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | const epoch = ++this.connectionEpoch; |
| 203 | this.setStatus("connecting"); |
| 204 | |
| 205 | this.resolveStartupNodeHostnames() |
| 206 | .then((nodes) => { |
| 207 | if (this.connectionEpoch !== epoch) { |
| 208 | debug( |
| 209 | "discard connecting after resolving startup nodes because epoch not match: %d != %d", |
| 210 | epoch, |
| 211 | this.connectionEpoch |
| 212 | ); |
| 213 | reject( |
| 214 | new RedisError( |
| 215 | "Connection is discarded because a new connection is made" |
| 216 | ) |
| 217 | ); |
| 218 | return; |
| 219 | } |
| 220 | if (this.status !== "connecting") { |
| 221 | debug( |
| 222 | "discard connecting after resolving startup nodes because the status changed to %s", |
| 223 | this.status |
| 224 | ); |
| 225 | reject(new RedisError("Connection is aborted")); |
| 226 | return; |
| 227 | } |
| 228 | this.connectionPool.reset(nodes); |
| 229 | |
| 230 | if (this.options.shardedSubscribers) { |
| 231 | this.shardedSubscribers |
| 232 | .reset(this.slots, this.connectionPool.getNodes("all")) |
| 233 | .catch((err) => { |
| 234 | // TODO should we emit an error event here? |
| 235 | debug("Error while starting subscribers: %s", err); |
| 236 | }); |
| 237 | } |
| 238 | |
| 239 | const readyHandler = () => { |
| 240 | this.setStatus("ready"); |
| 241 | this.retryAttempts = 0; |
| 242 | this.executeOfflineCommands(); |
| 243 | this.resetNodesRefreshInterval(); |
| 244 | resolve(); |
| 245 | }; |
| 246 | |
| 247 | let closeListener: () => void = undefined; |
| 248 | const refreshListener = () => { |
no test coverage detected