Skip to content

Enable TCP keep-alive for Node sockets #422

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/v1/internal/node/node-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const TrustStrategy = {
}
});
socket.on('error', onFailure);
return socket;
return configureSocket(socket);
},
TRUST_SYSTEM_CA_SIGNED_CERTIFICATES : function( config, onSuccess, onFailure ) {
const tlsOpts = newTlsOptions(config.url.host);
Expand All @@ -157,7 +157,7 @@ const TrustStrategy = {
}
});
socket.on('error', onFailure);
return socket;
return configureSocket(socket);
},
/**
* @deprecated in 1.1 in favour of {@link #TRUST_ALL_CERTIFICATES}. Will be deleted in a future version.
Expand Down Expand Up @@ -212,7 +212,7 @@ const TrustStrategy = {
});
});
socket.on('error', onFailure);
return socket;
return configureSocket(socket);
},

TRUST_ALL_CERTIFICATES: function (config, onSuccess, onFailure) {
Expand All @@ -230,7 +230,7 @@ const TrustStrategy = {
}
});
socket.on('error', onFailure);
return socket;
return configureSocket(socket);
}
};

Expand All @@ -244,9 +244,9 @@ const TrustStrategy = {
function connect( config, onSuccess, onFailure=(()=>null) ) {
const trustStrategy = trustStrategyName(config);
if (!isEncrypted(config)) {
var conn = net.connect(config.url.port, config.url.host, onSuccess);
conn.on('error', onFailure);
return conn;
const socket = net.connect(config.url.port, config.url.host, onSuccess);
socket.on('error', onFailure);
return configureSocket(socket);
} else if (TrustStrategy[trustStrategy]) {
return TrustStrategy[trustStrategy](config, onSuccess, onFailure);
} else {
Expand Down Expand Up @@ -290,6 +290,16 @@ function newTlsOptions(hostname, ca = undefined) {
};
}

/**
* Update socket options for the newly created socket. Accepts either `net.Socket` or its subclass `tls.TLSSocket`.
* @param {net.Socket} socket the socket to configure.
* @return {net.Socket} the given socket.
*/
function configureSocket(socket) {
socket.setKeepAlive(true);
return socket;
}

/**
* In a Node.js environment the 'net' module is used
* as transport.
Expand Down