Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/internal/stream_base_commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {
const { owner_symbol } = require('internal/async_hooks').symbols;
const {
kTimeout,
setUnrefTimeout,
reuseOrCreateUnrefTimeout,
getTimerDuration,
} = require('internal/timers');
const { isUint8Array } = require('internal/util/types');
Expand Down Expand Up @@ -233,6 +233,10 @@ function onStreamRead(arrayBuffer) {
}
}

function onStreamTimeout(stream) {
stream._onTimeout();
}

function setStreamTimeout(msecs, callback) {
if (this.destroyed)
return this;
Expand All @@ -244,6 +248,8 @@ function setStreamTimeout(msecs, callback) {

// Attempt to clear an existing timer in both cases -
// even if it will be rescheduled we don't want to leak an existing timer.
// The cleared Timeout stays referenced in this[kTimeout] so that it can be
// reused the next time a timeout is set, e.g. on keep-alive connections.
clearTimeout(this[kTimeout]);

if (msecs === 0) {
Expand All @@ -252,7 +258,8 @@ function setStreamTimeout(msecs, callback) {
this.removeListener('timeout', callback);
}
} else {
this[kTimeout] = setUnrefTimeout(this._onTimeout.bind(this), msecs);
this[kTimeout] =
reuseOrCreateUnrefTimeout(this[kTimeout], onStreamTimeout, msecs, this);
if (this[kSession]) this[kSession][kUpdateTimer]();
if (this[kBoundSession]) this[kBoundSession][kUpdateTimer]();

Expand Down
29 changes: 29 additions & 0 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,34 @@ function setUnrefTimeout(callback, after) {
return timer;
}

// Just like setUnrefTimeout() but reuses `timer` if it is a Timeout that is
// no longer scheduled (fired or cleared), avoiding the allocation of a new
// Timeout on rearm. This is the internal reuse path anticipated by the
// TODO in unenroll() (lib/timers.js), used by hot paths such as the
// keep-alive timeout handling in the HTTP server. `arg` is passed to
// `callback` when the timer fires.
function reuseOrCreateUnrefTimeout(timer, callback, after, arg) {
if (timer !== undefined && timer !== null &&
timer._destroyed && timer._repeat === null && !timer[kHasPrimitive]) {
timer._idleTimeout = after;
timer._onTimeout = callback;
const args = timer._timerArgs;
if (args === undefined || args[0] !== arg) {
timer._timerArgs = [arg];
}
Comment on lines +431 to +434

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const args = timer._timerArgs;
if (args === undefined || args[0] !== arg) {
timer._timerArgs = [arg];
}
const args = timer._timerArgs;
if (args === undefined) {
timer._timerArgs = [arg];
} else {
args[0] = arg;
}

Nit: Instead of assigning a new array, update _timerArgs directly.

// Re-inserts the timer and re-initializes its async resource, so
// async_hooks observes the same init/destroy sequence as if a new
// Timeout had been allocated.
unrefActive(timer);
return timer;
}

timer = new Timeout(callback, after, [arg], false, false);
insert(timer, timer._idleTimeout);

return timer;
}

// Type checking used by timers.enroll() and Socket#setTimeout()
function getTimerDuration(msecs, name) {
validateNumber(msecs, name);
Expand Down Expand Up @@ -704,6 +732,7 @@ module.exports = {
kHasPrimitive,
initAsyncResource,
setUnrefTimeout,
reuseOrCreateUnrefTimeout,
getTimerDuration,
immediateQueue,
getTimerCallbacks,
Expand Down
Loading