Skip to content

Commit

Permalink
fix: Hook up the keepAlive after a successful connect
Browse files Browse the repository at this point in the history
Fixes #1339
  • Loading branch information
marcbachmann authored and luin committed Mar 31, 2022
1 parent a89a900 commit 14f03a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
19 changes: 13 additions & 6 deletions lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions lib/connectors/StandaloneConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 14f03a4

Please sign in to comment.