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; }