From e3f89067d837e00ad390d4f97f5ababc0df4a768 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Tue, 9 Jun 2026 15:33:52 -0700 Subject: [PATCH 1/2] net: support TCP_KEEPINTVL and TCP_KEEPCNT in setKeepAlive Signed-off-by: Guy Bedford --- doc/api/net.md | 30 ++++++++-- lib/internal/net.js | 2 + lib/net.js | 21 +++++-- src/tcp_wrap.cc | 9 ++- .../test-net-keepalive-interval-count.js | 56 +++++++++++++++++++ 5 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 test/parallel/test-net-keepalive-interval-count.js diff --git a/doc/api/net.md b/doc/api/net.md index e1c77130a26ac6..6e35b33d5e79d3 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -1380,11 +1380,15 @@ added: v0.1.90 Set the encoding for the socket as a [Readable Stream][]. See [`readable.setEncoding()`][] for more information. -### `socket.setKeepAlive([enable][, initialDelay])` +### `socket.setKeepAlive([enable][, initialDelay][, interval][, count])` +Enable/disable keep-alive functionality, and optionally configure the +keepalive probe timing. Returns the socket itself. -* `enable` {boolean} **Default:** `false` -* `initialDelay` {number} **Default:** `0` -* `interval` {number} **Default:** `1000` -* `count` {number} **Default:** `10` -* Returns: {net.Socket} The socket itself. +Possible signatures: + +* [`socket.setKeepAlive([options])`][`socket.setKeepAlive(options)`] +* [`socket.setKeepAlive([enable][, initialDelay][, interval][, count])`][`socket.setKeepAlive(enable)`] -Enable/disable keep-alive functionality, and optionally set the initial -delay before the first keepalive probe is sent on an idle socket. +Enabling keep-alive sets the initial delay before the first keepalive probe is +sent on an idle socket. Set `initialDelay` (in milliseconds) to set the delay between the last data packet received and the first keepalive probe. Setting `0` for @@ -1433,6 +1421,51 @@ On Windows versions older than build 1709, keep-alive is configured through `SIO_KEEPALIVE_VALS`, which has no probe-count field, so `count` is ignored on those platforms. +#### `socket.setKeepAlive([options])` + + + +* `options` {Object} + * `enable` {boolean} **Default:** `false` + * `initialDelay` {number} **Default:** `0` + * `interval` {number} **Default:** `1000` + * `count` {number} **Default:** `10` +* Returns: {net.Socket} The socket itself. + +Configure keep-alive using an options object. See [`socket.setKeepAlive()`][] +for a description of each property. + +```js +socket.setKeepAlive({ enable: true, initialDelay: 1000, interval: 1000, count: 10 }); +``` + +#### `socket.setKeepAlive([enable][, initialDelay][, interval][, count])` + + + +* `enable` {boolean} **Default:** `false` +* `initialDelay` {number} **Default:** `0` +* `interval` {number} **Default:** `1000` +* `count` {number} **Default:** `10` +* Returns: {net.Socket} The socket itself. + +Configure keep-alive using positional arguments. See +[`socket.setKeepAlive()`][] for a description of each argument. + ### `socket.setNoDelay([noDelay])`