From dc15397590d2240b9204e88bde146920ecc38e77 Mon Sep 17 00:00:00 2001 From: George Hertz Date: Sat, 3 Jul 2021 17:22:13 +0200 Subject: [PATCH] feat(user-timeout): use TCP_RXT_CONNDROPTIME on darwin (#263) --- README.md | 17 ++++++++++++++++- lib/constants.js | 4 ++++ lib/index.js | 4 ++-- test/unit/test-timeout.js | 8 ++++---- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ffad49aa..a0e7ece3 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,22 @@ The Missing (`TCP_KEEPINTVL` and `TCP_KEEPCNT`) `SO_KEEPALIVE` socket option set Tested on 🐧 `linux` & 🍏 `osx` (both `amd64` and `arm64`), should work on 😈 `freebsd` and others. Does not work on 🐄 `win32` (pull requests welcome). -There's also support for getting & setting the `TCP_USER_TIMEOUT` (linux 🐧 only) option, which is closely related to keep-alive. +There's also support for getting & setting the `TCP_USER_TIMEOUT` (🐧 `linux` and 🍏 `osx` only) option, which is closely related to keep-alive. + + +## Platform support + +| Platform | TCP_KEEPINTVL | TCP_KEEPCNT | TCP_USER_TIMEOUT | +| ------------ | ------------- | ----------- | --------------------------- | +| 🐧 `linux` | ✅ | ✅ | ✅ | +| 🍏 `osx` | ✅ | ✅ | ✅ (`TCP_RXT_CONNDROPTIME`) | +| 😈 `freebsd` | ✅ | ✅ | ❌ | +| 🐄 `win32` | ❌ | ❌ | ❌ | + +Legend: + +- ✅ - Supported +- ❌ - Unsupported (throws) ## Install diff --git a/lib/constants.js b/lib/constants.js index 18df9454..d5317cb7 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -13,6 +13,10 @@ switch (OS.platform()) { case 'darwin': Constants.TCP_KEEPINTVL = 0x101 Constants.TCP_KEEPCNT = 0x102 + + // Alias for TCP_RXT_CONNDROPTIME + // Source https://github.com/apple/darwin-xnu/blob/a1babec6b135d1f35b2590a1990af3c5c5393479/bsd/netinet/tcp.h#L221 + Constants.TCP_USER_TIMEOUT = 0x80 break case 'freebsd': diff --git a/lib/index.js b/lib/index.js index 09a584b6..3f21fc56 100644 --- a/lib/index.js +++ b/lib/index.js @@ -215,10 +215,10 @@ module.exports.getKeepAliveProbes = function getKeepAliveProbes(socket) { } /** - * Sets the TCP_USER_TIMEOUT value for specified socket. + * Sets the TCP_USER_TIMEOUT (TCP_RXT_CONNDROPTIME on `darwin`) value for specified socket. * * Notes: - * * This method is only supported on `linux`, will throw on `darwin` and `freebsd`. + * * This method is only supported on `linux` and `darwin`, will throw on `freebsd`. * * The msec will be rounded towards the closest integer. * * When used with the TCP keepalive options, will override them. * diff --git a/test/unit/test-timeout.js b/test/unit/test-timeout.js index e4106820..9ce56a6e 100644 --- a/test/unit/test-timeout.js +++ b/test/unit/test-timeout.js @@ -13,7 +13,7 @@ describe('TCP User Timeout', () => { }) itSkipOS( - ['darwin', 'freebsd'],'should validate passed arguments', function () { + ['freebsd'],'should validate passed arguments', function () { ;(() => Lib.setUserTimeout()).should.throw( 'setUserTimeout requires two arguments' ) @@ -52,7 +52,7 @@ describe('TCP User Timeout', () => { }) itSkipOS( - ['darwin', 'freebsd'], + ['freebsd'], 'should throw when setsockopt returns -1', (done) => { const srv = Net.createServer() @@ -68,7 +68,7 @@ describe('TCP User Timeout', () => { } ) - itSkipOS(['darwin', 'freebsd'], 'should be able to set and get 4 second value', (done) => { + itSkipOS(['freebsd'], 'should be able to set and get 4 second value', (done) => { const srv = Net.createServer() srv.listen(0, () => { const expected = 4000 @@ -91,7 +91,7 @@ describe('TCP User Timeout', () => { }) }) - itSkipOS(['darwin', 'freebsd'], 'should throw when trying to get using invalid fd', (done) => { + itSkipOS(['freebsd'], 'should throw when trying to get using invalid fd', (done) => { ;(() => Lib.setUserTimeout(new Net.Socket(), 1)).should.throw( 'Unable to get socket fd' )