-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for the TCP_USER_TIMEOUT option #103
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -213,3 +213,96 @@ module.exports.getKeepAliveProbes = function getKeepAliveProbes(socket) { | |||||||
|
||||||||
return cntVal.deref() | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* Sets the TCP_USER_TIMEOUT value for specified socket. | ||||||||
* | ||||||||
* Note: The msec will be rounded towards the closest integer | ||||||||
* | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest lightly mentioning the interaction between
Suggested change
|
||||||||
* @since v1.4.0 | ||||||||
* @param {Net.Socket} socket to set the value for | ||||||||
* @param {number} msecs to wait for unacknowledged data before closing the connection | ||||||||
* | ||||||||
* @returns {boolean} <code>true</code> on success | ||||||||
* | ||||||||
* @example <caption>Set user timeout to 30 seconds (<code>30000</code> milliseconds) for socket <code>s</code></caption> | ||||||||
* NetKeepAlive.setUserTimeout(s, 30000) | ||||||||
* | ||||||||
* @throws {AssertionError} setUserTimeout requires two arguments | ||||||||
* @throws {AssertionError} setUserTimeout expects an instance of socket as its first argument | ||||||||
* @throws {AssertionError} setUserTimeout requires msec to be a Number | ||||||||
* @throws {ErrnoException|Error} Unexpected error | ||||||||
*/ | ||||||||
module.exports.setUserTimeout = function setUserTimeout( | ||||||||
socket, | ||||||||
msecs | ||||||||
) { | ||||||||
Assert.strictEqual( | ||||||||
arguments.length, | ||||||||
2, | ||||||||
'setUserTimeout requires two arguments' | ||||||||
) | ||||||||
Assert( | ||||||||
_isSocket(socket), | ||||||||
'setUserTimeout expects an instance of socket as its first argument' | ||||||||
) | ||||||||
Assert.strictEqual( | ||||||||
msecs != null ? msecs.constructor : void 0, | ||||||||
Number, | ||||||||
'setUserTimeout requires msec to be a Number' | ||||||||
) | ||||||||
|
||||||||
const fd = _getSocketFD(socket), | ||||||||
msecInt = ~~msecs, | ||||||||
msecVal = Ref.alloc('int', msecInt), | ||||||||
msecValLn = msecVal.type.size | ||||||||
|
||||||||
return FFIBindings.setsockopt( | ||||||||
fd, | ||||||||
Constants.SOL_TCP, | ||||||||
Constants.TCP_USER_TIMEOUT, | ||||||||
msecVal, | ||||||||
msecValLn | ||||||||
) | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* Returns the TCP_USER_TIMEOUT value for specified socket. | ||||||||
* | ||||||||
* @since v1.4.0 | ||||||||
* @param {Net.Socket} socket to check the value for | ||||||||
* | ||||||||
* @returns {number} msecs to wait for unacknowledged data before closing the connection | ||||||||
* | ||||||||
* @example <caption>Get the current user timeout for socket <code>s</code></caption> | ||||||||
* NetKeepAlive.getUserTimeout(s) // returns 30000 based on setter example | ||||||||
* | ||||||||
* @throws {AssertionError} getUserTimeout requires one arguments | ||||||||
* @throws {AssertionError} getUserTimeout expects an instance of socket as its first argument | ||||||||
* @throws {ErrnoException|Error} Unexpected error | ||||||||
*/ | ||||||||
module.exports.getUserTimeout = function getUserTimeout(socket) { | ||||||||
Assert.strictEqual( | ||||||||
arguments.length, | ||||||||
1, | ||||||||
'getUserTimeout requires one arguments' | ||||||||
) | ||||||||
Assert( | ||||||||
_isSocket(socket), | ||||||||
'getUserTimeout expects an instance of socket as its first argument' | ||||||||
) | ||||||||
|
||||||||
const fd = _getSocketFD(socket), | ||||||||
msecVal = Ref.alloc(Ref.types.uint32), | ||||||||
msecValLn = Ref.alloc(Ref.types.uint32, msecVal.type.size) | ||||||||
|
||||||||
FFIBindings.getsockopt( | ||||||||
fd, | ||||||||
Constants.SOL_TCP, | ||||||||
Constants.TCP_USER_TIMEOUT, | ||||||||
msecVal, | ||||||||
msecValLn | ||||||||
) | ||||||||
|
||||||||
return msecVal.deref() | ||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
const Stream = require('stream') | ||
const Should = require('should') | ||
const OS = require('os') | ||
const Net = require('net') | ||
const Lib = require('../../lib') | ||
|
||
describe('TCP User Timeout', () => { | ||
const itSkipOS = (skipOs, ...args) => | ||
(skipOs.includes(OS.platform()) ? it.skip : it)(...args) | ||
|
||
it('should be a function', function () { | ||
Lib.setUserTimeout.should.be.type('function') | ||
}) | ||
|
||
it('should validate passed arguments', function () { | ||
;(() => Lib.setUserTimeout()).should.throw( | ||
'setUserTimeout requires two arguments' | ||
) | ||
;(() => Lib.setUserTimeout('')).should.throw( | ||
'setUserTimeout requires two arguments' | ||
) | ||
;(() => Lib.setUserTimeout('', '', '')).should.throw( | ||
'setUserTimeout requires two arguments' | ||
) | ||
;(() => Lib.setUserTimeout(null, 1)).should.throw( | ||
'setUserTimeout expects an instance of socket as its first argument' | ||
) | ||
;(() => Lib.setUserTimeout({}, 1)).should.throw( | ||
'setUserTimeout expects an instance of socket as its first argument' | ||
) | ||
;(() => Lib.setUserTimeout(new (class {})(), 1)).should.throw( | ||
'setUserTimeout expects an instance of socket as its first argument' | ||
) | ||
;(() => Lib.setUserTimeout(new Stream.PassThrough(), 1)).should.throw( | ||
'setUserTimeout expects an instance of socket as its first argument' | ||
) | ||
|
||
const socket = new Net.Socket() | ||
;(() => Lib.setUserTimeout(socket, null)).should.throw( | ||
'setUserTimeout requires msec to be a Number' | ||
) | ||
;(() => Lib.setUserTimeout(socket, '')).should.throw( | ||
'setUserTimeout requires msec to be a Number' | ||
) | ||
;(() => Lib.setUserTimeout(socket, true)).should.throw( | ||
'setUserTimeout requires msec to be a Number' | ||
) | ||
;(() => Lib.setUserTimeout(socket, {})).should.throw( | ||
'setUserTimeout requires msec to be a Number' | ||
) | ||
}) | ||
|
||
itSkipOS( | ||
['darwin', 'freebsd'], | ||
'should throw when setsockopt returns -1', | ||
(done) => { | ||
const srv = Net.createServer() | ||
srv.listen(0, () => { | ||
const socket = Net.createConnection(srv.address(), () => { | ||
;(() => Lib.setUserTimeout(socket, -1)).should.throw( | ||
/^setsockopt /i | ||
) | ||
socket.destroy() | ||
srv.close(done) | ||
}) | ||
}) | ||
} | ||
) | ||
|
||
itSkipOS(['darwin', 'freebsd'], 'should be able to set and get 4 second value', (done) => { | ||
const srv = Net.createServer() | ||
srv.listen(0, () => { | ||
const expected = 4000 | ||
|
||
const socket = Net.createConnection(srv.address(), () => { | ||
;(() => { | ||
Lib.setUserTimeout(socket, expected) | ||
}).should.not.throw() | ||
|
||
let actual | ||
;(() => { | ||
actual = Lib.getUserTimeout(socket) | ||
}).should.not.throw() | ||
|
||
expected.should.eql(actual) | ||
|
||
socket.destroy() | ||
srv.close(done) | ||
}) | ||
}) | ||
}) | ||
|
||
itSkipOS(['darwin', 'freebsd'], 'should throw when trying to get using invalid fd', (done) => { | ||
;(() => Lib.setUserTimeout(new Net.Socket(), 1)).should.throw( | ||
'Unable to get socket fd' | ||
) | ||
|
||
const srv = Net.createServer() | ||
srv.listen(0, function () { | ||
const socket = Net.createConnection(this.address(), () => { | ||
const oldHandle = socket._handle | ||
|
||
socket._handle = { fd: -99999 } | ||
;(() => Lib.getUserTimeout(socket)).should.throw( | ||
'getsockopt EBADF' | ||
) | ||
|
||
socket._handle = oldHandle | ||
socket.destroy() | ||
srv.close(done) | ||
}) | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this feature is actually not supported on
freebsd
anddarwin
(not tested, assumption based, see review comment for details).For those platforms the value would be
undefiend
which might cause segfault and I'm not sure what would happen in that case, most likely not what the user intended to do so I suggest throwing inset
andget
methods if this constant isnull
orundefiend
(==null
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes a lot of sense, yes. I'll do that when I find some time.