Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook up the keepAlive after a successful connect #1554

Merged
merged 2 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions lib/connectors/SentinelConnector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,16 @@ export default class SentinelConnector extends AbstractConnector {
resolved.port,
endpointAddress
);

if (this.options.enableTLSForSentinelMode && this.options.tls) {
Object.assign(resolved, this.options.tls);
this.stream = createTLSConnection(resolved);
this.stream.once("secureConnect", this.initFailoverDetector.bind(this));
} else {
this.stream = createConnection(resolved);
this.stream.once("connect", this.initFailoverDetector.bind(this));
}

this.stream.once("connect", () => this.initFailoverDetector());

this.stream.once("error", (err) => {
this.firstError = err;
});
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