From 14f03a4d9416b32a912f3ab9eee4c004ccad8acc Mon Sep 17 00:00:00 2001 From: Marc Bachmann Date: Wed, 30 Mar 2022 14:26:26 +0200 Subject: [PATCH] fix: Hook up the keepAlive after a successful connect Fixes #1339 --- lib/Redis.ts | 19 +++++++++++++------ lib/connectors/StandaloneConnector.ts | 6 +++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/Redis.ts b/lib/Redis.ts index 2c654c12..5513392a 100644 --- a/lib/Redis.ts +++ b/lib/Redis.ts @@ -213,8 +213,19 @@ class Redis extends Commander { } _this.stream = stream; - if (typeof options.keepAlive === "number") { - stream.setKeepAlive(true, options.keepAlive); + + if (options.noDelay) { + stream.setNoDelay(true); + } + + // Node ignores setKeepAlive before connect, therefore we wait for the event: + // https://github.com/nodejs/node/issues/31663 + if (typeof options.keepAlive === 'number') { + if (stream.connecting) { + stream.once(CONNECT_EVENT, () => stream.setKeepAlive(true, options.keepAlive)); + } else { + stream.setKeepAlive(true, options.keepAlive); + } } if (stream.connecting) { @@ -266,10 +277,6 @@ class Redis extends Commander { stream.once("close", eventHandler.closeHandler(_this)); } - if (options.noDelay) { - stream.setNoDelay(true); - } - const connectionReadyHandler = function () { _this.removeListener("close", connectionCloseHandler); resolve(); diff --git a/lib/connectors/StandaloneConnector.ts b/lib/connectors/StandaloneConnector.ts index 2a32e398..d8d9c00a 100644 --- a/lib/connectors/StandaloneConnector.ts +++ b/lib/connectors/StandaloneConnector.ts @@ -19,13 +19,13 @@ export default class StandaloneConnector extends AbstractConnector { const { options } = this; this.connecting = true; - let connectionOptions: any; + let connectionOptions: TcpNetConnectOpts | IpcNetConnectOpts; if ("path" in options && options.path) { connectionOptions = { path: options.path, - }; + } as IpcNetConnectOpts; } else { - connectionOptions = {}; + connectionOptions = {} as TcpNetConnectOpts; if ("port" in options && options.port != null) { connectionOptions.port = options.port; }