-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
net/http2: use actual Timeout instances #17704
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
Merged
Fishrock123
merged 4 commits into
nodejs:master
from
Fishrock123:use-timeout-in-net-socket
Dec 20, 2017
+234
−107
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
24dd92e
net: use actual Timeout instance on Sockets
Fishrock123 593941a
timers: extract enroll() validation into a fn
Fishrock123 93eb68e
http2: use actual Timeout instances
Fishrock123 fc61ee3
http2: use session kUpdateTimer from kUpdateTimer
Fishrock123 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 hidden or 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 hidden or 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,131 @@ | ||
'use strict'; | ||
|
||
const async_wrap = process.binding('async_wrap'); | ||
// Two arrays that share state between C++ and JS. | ||
const { async_hook_fields, async_id_fields } = async_wrap; | ||
const { | ||
getDefaultTriggerAsyncId, | ||
// The needed emit*() functions. | ||
emitInit | ||
} = require('internal/async_hooks'); | ||
// Grab the constants necessary for working with internal arrays. | ||
const { kInit, kAsyncIdCounter } = async_wrap.constants; | ||
// Symbols for storing async id state. | ||
const async_id_symbol = Symbol('asyncId'); | ||
const trigger_async_id_symbol = Symbol('triggerId'); | ||
|
||
const errors = require('internal/errors'); | ||
|
||
// Timeout values > TIMEOUT_MAX are set to 1. | ||
const TIMEOUT_MAX = 2 ** 31 - 1; | ||
|
||
module.exports = { | ||
TIMEOUT_MAX, | ||
kTimeout: Symbol('timeout'), // For hiding Timeouts on other internals. | ||
async_id_symbol, | ||
trigger_async_id_symbol, | ||
Timeout, | ||
setUnrefTimeout, | ||
validateTimerDuration | ||
}; | ||
|
||
// Timer constructor function. | ||
// The entire prototype is defined in lib/timers.js | ||
function Timeout(callback, after, args, isRepeat) { | ||
after *= 1; // coalesce to number or NaN | ||
if (!(after >= 1 && after <= TIMEOUT_MAX)) { | ||
if (after > TIMEOUT_MAX) { | ||
process.emitWarning(`${after} does not fit into` + | ||
' a 32-bit signed integer.' + | ||
'\nTimeout duration was set to 1.', | ||
'TimeoutOverflowWarning'); | ||
} | ||
after = 1; // schedule on next tick, follows browser behavior | ||
} | ||
|
||
this._called = false; | ||
this._idleTimeout = after; | ||
this._idlePrev = this; | ||
this._idleNext = this; | ||
this._idleStart = null; | ||
// this must be set to null first to avoid function tracking | ||
// on the hidden class, revisit in V8 versions after 6.2 | ||
this._onTimeout = null; | ||
this._onTimeout = callback; | ||
this._timerArgs = args; | ||
this._repeat = isRepeat ? after : null; | ||
this._destroyed = false; | ||
|
||
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter]; | ||
this[trigger_async_id_symbol] = getDefaultTriggerAsyncId(); | ||
if (async_hook_fields[kInit] > 0) { | ||
emitInit(this[async_id_symbol], | ||
'Timeout', | ||
this[trigger_async_id_symbol], | ||
this); | ||
} | ||
} | ||
|
||
var timers; | ||
function getTimers() { | ||
if (timers === undefined) { | ||
timers = require('timers'); | ||
} | ||
return timers; | ||
} | ||
|
||
function setUnrefTimeout(callback, after, arg1, arg2, arg3) { | ||
// Type checking identical to setTimeout() | ||
if (typeof callback !== 'function') { | ||
throw new errors.TypeError('ERR_INVALID_CALLBACK'); | ||
} | ||
|
||
let i, args; | ||
switch (arguments.length) { | ||
// fast cases | ||
case 1: | ||
case 2: | ||
break; | ||
case 3: | ||
args = [arg1]; | ||
break; | ||
case 4: | ||
args = [arg1, arg2]; | ||
break; | ||
default: | ||
args = [arg1, arg2, arg3]; | ||
for (i = 5; i < arguments.length; i++) { | ||
// extend array dynamically, makes .apply run much faster in v6.0.0 | ||
args[i - 2] = arguments[i]; | ||
} | ||
break; | ||
} | ||
|
||
const timer = new Timeout(callback, after, args, false); | ||
getTimers()._unrefActive(timer); | ||
|
||
return timer; | ||
} | ||
|
||
// Type checking used by timers.enroll() and Socket#setTimeout() | ||
function validateTimerDuration(msecs) { | ||
if (typeof msecs !== 'number') { | ||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs', | ||
'number', msecs); | ||
} | ||
|
||
if (msecs < 0 || !isFinite(msecs)) { | ||
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs', | ||
'a non-negative finite number', msecs); | ||
} | ||
|
||
// Ensure that msecs fits into signed int32 | ||
if (msecs > TIMEOUT_MAX) { | ||
process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` + | ||
`\nTimer duration was truncated to ${TIMEOUT_MAX}.`, | ||
'TimeoutOverflowWarning'); | ||
return TIMEOUT_MAX; | ||
} | ||
|
||
return msecs; | ||
} |
This file contains hidden or 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
Oops, something went wrong.
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.
This might be enough code to factor it out into some internal function, wdyt?
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.
Might be a good idea yeah.