Skip to content

Commit

Permalink
feat(user-timeout): use TCP_RXT_CONNDROPTIME on darwin (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
hertzg authored Jul 3, 2021
1 parent 37dbd94 commit dc15397
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ switch (OS.platform()) {
case 'darwin':
Constants.TCP_KEEPINTVL = 0x101
Constants.TCP_KEEPCNT = 0x102

// Alias for TCP_RXT_CONNDROPTIME
// Source https:/apple/darwin-xnu/blob/a1babec6b135d1f35b2590a1990af3c5c5393479/bsd/netinet/tcp.h#L221
Constants.TCP_USER_TIMEOUT = 0x80
break

case 'freebsd':
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 4 additions & 4 deletions test/unit/test-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('TCP User Timeout', () => {
})

itSkipOS(
['darwin', 'freebsd'],
['freebsd'],
'should throw when setsockopt returns -1',
(done) => {
const srv = Net.createServer()
Expand All @@ -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
Expand All @@ -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'
)
Expand Down

0 comments on commit dc15397

Please sign in to comment.