diff --git a/docs/api/Client.md b/docs/api/Client.md index 2b5f9e526bb..1aca7d09d31 100644 --- a/docs/api/Client.md +++ b/docs/api/Client.md @@ -38,6 +38,8 @@ Furthermore, the following options can be passed: * **maxCachedSessions** `number | null` (optional) - Default: `100` - Maximum number of TLS cached sessions. Use 0 to disable TLS session caching. Default: 100. * **timeout** `number | null` (optional) - Default `10e3` * **servername** `string | null` (optional) +* **keepAlive** `boolean | null` (optional) - Default: `true` - TCP keep-alive enabled +* **keepAliveInitialDelay** `number | null` (optional) - Default: `60000` - TCP keep-alive interval for the socket in milliseconds ### Example - Basic Client instantiation diff --git a/lib/core/connect.js b/lib/core/connect.js index 89561f16fbe..f3b5cc33edd 100644 --- a/lib/core/connect.js +++ b/lib/core/connect.js @@ -120,6 +120,12 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) { }) } + // Set TCP keep alive options on the socket here instead of in connect() for the case of assigning the socket + if (options.keepAlive == null || options.keepAlive) { + const keepAliveInitialDelay = options.keepAliveInitialDelay === undefined ? 60e3 : options.keepAliveInitialDelay + socket.setKeepAlive(true, keepAliveInitialDelay) + } + const cancelTimeout = setupTimeout(() => onConnectTimeout(socket), timeout) socket