Skip to content
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

net,http2: merge setTimeout code #25084

Closed
wants to merge 2 commits into from
Closed
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
41 changes: 5 additions & 36 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,11 @@ const {
onStreamRead,
kAfterAsyncWrite,
kMaybeDestroy,
kUpdateTimer
kUpdateTimer,
kSession,
setStreamTimeout
} = require('internal/stream_base_commons');
const {
kTimeout,
setUnrefTimeout,
validateTimerDuration
} = require('internal/timers');
const { kTimeout } = require('internal/timers');
const { isArrayBufferView } = require('internal/util/types');

const { FileHandle } = internalBinding('fs');
Expand Down Expand Up @@ -163,7 +161,6 @@ const kSelectPadding = Symbol('select-padding');
const kSentHeaders = Symbol('sent-headers');
const kSentTrailers = Symbol('sent-trailers');
const kServer = Symbol('server');
const kSession = Symbol('session');
const kState = Symbol('state');
const kType = Symbol('type');
const kWriteGeneric = Symbol('write-generic');
Expand Down Expand Up @@ -2544,35 +2541,7 @@ const setTimeout = {
configurable: true,
enumerable: true,
writable: true,
value: function(msecs, callback) {
if (this.destroyed)
return;

// Type checking identical to timers.enroll()
msecs = validateTimerDuration(msecs);

// Attempt to clear an existing timer lear in both cases -
// even if it will be rescheduled we don't want to leak an existing timer.
clearTimeout(this[kTimeout]);

if (msecs === 0) {
if (callback !== undefined) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
this.removeListener('timeout', callback);
}
} else {
this[kTimeout] = setUnrefTimeout(this._onTimeout.bind(this), msecs);
if (this[kSession]) this[kSession][kUpdateTimer]();

if (callback !== undefined) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
this.once('timeout', callback);
}
}
return this;
}
value: setStreamTimeout
};
Object.defineProperty(Http2Stream.prototype, 'setTimeout', setTimeout);
Object.defineProperty(Http2Session.prototype, 'setTimeout', setTimeout);
Expand Down
47 changes: 46 additions & 1 deletion lib/internal/stream_base_commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ const {
streamBaseState
} = internalBinding('stream_wrap');
const { UV_EOF } = internalBinding('uv');
const { errnoException } = require('internal/errors');
const {
codes: {
ERR_INVALID_CALLBACK
},
errnoException
} = require('internal/errors');
const { owner_symbol } = require('internal/async_hooks').symbols;
const {
kTimeout,
setUnrefTimeout,
validateTimerDuration
} = require('internal/timers');

const kMaybeDestroy = Symbol('kMaybeDestroy');
const kUpdateTimer = Symbol('kUpdateTimer');
const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
const kSession = Symbol('session');

function handleWriteReq(req, data, encoding) {
const { handle } = req;
Expand Down Expand Up @@ -178,6 +189,38 @@ function onStreamRead(arrayBuffer) {
}
}

function setStreamTimeout(msecs, callback) {
if (this.destroyed)
return;

this.timeout = msecs;

// Type checking identical to timers.enroll()
msecs = validateTimerDuration(msecs);

// Attempt to clear an existing timer in both cases -
// even if it will be rescheduled we don't want to leak an existing timer.
clearTimeout(this[kTimeout]);

if (msecs === 0) {
if (callback !== undefined) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
ZYSzys marked this conversation as resolved.
Show resolved Hide resolved
this.removeListener('timeout', callback);
}
} else {
this[kTimeout] = setUnrefTimeout(this._onTimeout.bind(this), msecs);
if (this[kSession]) this[kSession][kUpdateTimer]();

if (callback !== undefined) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
ZYSzys marked this conversation as resolved.
Show resolved Hide resolved
this.once('timeout', callback);
}
}
return this;
}

module.exports = {
createWriteWrap,
writevGeneric,
Expand All @@ -186,4 +229,6 @@ module.exports = {
kAfterAsyncWrite,
kMaybeDestroy,
kUpdateTimer,
kSession,
setStreamTimeout
};
32 changes: 4 additions & 28 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ const {
writeGeneric,
onStreamRead,
kAfterAsyncWrite,
kUpdateTimer
kUpdateTimer,
setStreamTimeout
} = require('internal/stream_base_commons');
const {
codes: {
Expand All @@ -89,11 +90,7 @@ const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
let cluster;
let dns;

const {
kTimeout,
setUnrefTimeout,
validateTimerDuration
} = require('internal/timers');
const { kTimeout } = require('internal/timers');

function noop() {}

Expand Down Expand Up @@ -403,28 +400,7 @@ function writeAfterFIN(chunk, encoding, cb) {
}
}

Socket.prototype.setTimeout = function(msecs, callback) {
this.timeout = msecs;
// Type checking identical to timers.enroll()
msecs = validateTimerDuration(msecs);

// Attempt to clear an existing timer in both cases -
// even if it will be rescheduled we don't want to leak an existing timer.
clearTimeout(this[kTimeout]);

if (msecs === 0) {
if (callback) {
this.removeListener('timeout', callback);
}
} else {
this[kTimeout] = setUnrefTimeout(this._onTimeout.bind(this), msecs);

if (callback) {
this.once('timeout', callback);
}
}
return this;
};
Socket.prototype.setTimeout = setStreamTimeout;


Socket.prototype._onTimeout = function() {
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-net-socket-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const nonNumericDelays = [
];
const badRangeDelays = [-0.001, -1, -Infinity, Infinity, NaN];
const validDelays = [0, 0.001, 1, 1e6];
const invalidCallbacks = [
1, '100', true, false, null, {}, [], Symbol('test')
];


for (let i = 0; i < nonNumericDelays.length; i++) {
Expand All @@ -49,6 +52,19 @@ for (let i = 0; i < validDelays.length; i++) {
s.setTimeout(validDelays[i], () => {});
}

for (let i = 0; i < invalidCallbacks.length; i++) {
[0, 1].forEach((mesc) =>
common.expectsError(
() => s.setTimeout(mesc, invalidCallbacks[i]),
{
code: 'ERR_INVALID_CALLBACK',
type: TypeError,
message: 'Callback must be a function'
}
)
);
}

const server = net.Server();
server.listen(0, common.mustCall(() => {
const socket = net.createConnection(server.address().port);
Expand Down