Skip to content

Commit 755520a

Browse files
authored
Merge pull request #422 from lutovich/1.7-so-keep-alive
Enable TCP keep-alive for Node sockets
2 parents 5fed2fa + 0f52ccf commit 755520a

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/v1/internal/node/node-channel.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ const TrustStrategy = {
139139
}
140140
});
141141
socket.on('error', onFailure);
142-
return socket;
142+
return configureSocket(socket);
143143
},
144144
TRUST_SYSTEM_CA_SIGNED_CERTIFICATES : function( config, onSuccess, onFailure ) {
145145
const tlsOpts = newTlsOptions(config.url.host);
@@ -157,7 +157,7 @@ const TrustStrategy = {
157157
}
158158
});
159159
socket.on('error', onFailure);
160-
return socket;
160+
return configureSocket(socket);
161161
},
162162
/**
163163
* @deprecated in 1.1 in favour of {@link #TRUST_ALL_CERTIFICATES}. Will be deleted in a future version.
@@ -212,7 +212,7 @@ const TrustStrategy = {
212212
});
213213
});
214214
socket.on('error', onFailure);
215-
return socket;
215+
return configureSocket(socket);
216216
},
217217

218218
TRUST_ALL_CERTIFICATES: function (config, onSuccess, onFailure) {
@@ -230,7 +230,7 @@ const TrustStrategy = {
230230
}
231231
});
232232
socket.on('error', onFailure);
233-
return socket;
233+
return configureSocket(socket);
234234
}
235235
};
236236

@@ -244,9 +244,9 @@ const TrustStrategy = {
244244
function connect( config, onSuccess, onFailure=(()=>null) ) {
245245
const trustStrategy = trustStrategyName(config);
246246
if (!isEncrypted(config)) {
247-
var conn = net.connect(config.url.port, config.url.host, onSuccess);
248-
conn.on('error', onFailure);
249-
return conn;
247+
const socket = net.connect(config.url.port, config.url.host, onSuccess);
248+
socket.on('error', onFailure);
249+
return configureSocket(socket);
250250
} else if (TrustStrategy[trustStrategy]) {
251251
return TrustStrategy[trustStrategy](config, onSuccess, onFailure);
252252
} else {
@@ -290,6 +290,16 @@ function newTlsOptions(hostname, ca = undefined) {
290290
};
291291
}
292292

293+
/**
294+
* Update socket options for the newly created socket. Accepts either `net.Socket` or its subclass `tls.TLSSocket`.
295+
* @param {net.Socket} socket the socket to configure.
296+
* @return {net.Socket} the given socket.
297+
*/
298+
function configureSocket(socket) {
299+
socket.setKeepAlive(true);
300+
return socket;
301+
}
302+
293303
/**
294304
* In a Node.js environment the 'net' module is used
295305
* as transport.

0 commit comments

Comments
 (0)