net,http2: merge setTimeout code
PR-URL: https://github.com/nodejs/node/pull/25084 Refs: https://github.com/nodejs/node/issues/19060 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
bb774b1554
commit
802ea05a37
@ -110,13 +110,11 @@ const {
|
|||||||
onStreamRead,
|
onStreamRead,
|
||||||
kAfterAsyncWrite,
|
kAfterAsyncWrite,
|
||||||
kMaybeDestroy,
|
kMaybeDestroy,
|
||||||
kUpdateTimer
|
kUpdateTimer,
|
||||||
|
kSession,
|
||||||
|
setStreamTimeout
|
||||||
} = require('internal/stream_base_commons');
|
} = require('internal/stream_base_commons');
|
||||||
const {
|
const { kTimeout } = require('internal/timers');
|
||||||
kTimeout,
|
|
||||||
setUnrefTimeout,
|
|
||||||
validateTimerDuration
|
|
||||||
} = require('internal/timers');
|
|
||||||
const { isArrayBufferView } = require('internal/util/types');
|
const { isArrayBufferView } = require('internal/util/types');
|
||||||
|
|
||||||
const { FileHandle } = internalBinding('fs');
|
const { FileHandle } = internalBinding('fs');
|
||||||
@ -163,7 +161,6 @@ const kSelectPadding = Symbol('select-padding');
|
|||||||
const kSentHeaders = Symbol('sent-headers');
|
const kSentHeaders = Symbol('sent-headers');
|
||||||
const kSentTrailers = Symbol('sent-trailers');
|
const kSentTrailers = Symbol('sent-trailers');
|
||||||
const kServer = Symbol('server');
|
const kServer = Symbol('server');
|
||||||
const kSession = Symbol('session');
|
|
||||||
const kState = Symbol('state');
|
const kState = Symbol('state');
|
||||||
const kType = Symbol('type');
|
const kType = Symbol('type');
|
||||||
const kWriteGeneric = Symbol('write-generic');
|
const kWriteGeneric = Symbol('write-generic');
|
||||||
@ -2546,35 +2543,7 @@ const setTimeout = {
|
|||||||
configurable: true,
|
configurable: true,
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
writable: true,
|
writable: true,
|
||||||
value: function(msecs, callback) {
|
value: setStreamTimeout
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
Object.defineProperty(Http2Stream.prototype, 'setTimeout', setTimeout);
|
Object.defineProperty(Http2Stream.prototype, 'setTimeout', setTimeout);
|
||||||
Object.defineProperty(Http2Session.prototype, 'setTimeout', setTimeout);
|
Object.defineProperty(Http2Session.prototype, 'setTimeout', setTimeout);
|
||||||
|
@ -11,12 +11,23 @@ const {
|
|||||||
streamBaseState
|
streamBaseState
|
||||||
} = internalBinding('stream_wrap');
|
} = internalBinding('stream_wrap');
|
||||||
const { UV_EOF } = internalBinding('uv');
|
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 { owner_symbol } = require('internal/async_hooks').symbols;
|
||||||
|
const {
|
||||||
|
kTimeout,
|
||||||
|
setUnrefTimeout,
|
||||||
|
validateTimerDuration
|
||||||
|
} = require('internal/timers');
|
||||||
|
|
||||||
const kMaybeDestroy = Symbol('kMaybeDestroy');
|
const kMaybeDestroy = Symbol('kMaybeDestroy');
|
||||||
const kUpdateTimer = Symbol('kUpdateTimer');
|
const kUpdateTimer = Symbol('kUpdateTimer');
|
||||||
const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
|
const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
|
||||||
|
const kSession = Symbol('session');
|
||||||
|
|
||||||
function handleWriteReq(req, data, encoding) {
|
function handleWriteReq(req, data, encoding) {
|
||||||
const { handle } = req;
|
const { handle } = req;
|
||||||
@ -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();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createWriteWrap,
|
createWriteWrap,
|
||||||
writevGeneric,
|
writevGeneric,
|
||||||
@ -186,4 +229,6 @@ module.exports = {
|
|||||||
kAfterAsyncWrite,
|
kAfterAsyncWrite,
|
||||||
kMaybeDestroy,
|
kMaybeDestroy,
|
||||||
kUpdateTimer,
|
kUpdateTimer,
|
||||||
|
kSession,
|
||||||
|
setStreamTimeout
|
||||||
};
|
};
|
||||||
|
32
lib/net.js
32
lib/net.js
@ -63,7 +63,8 @@ const {
|
|||||||
writeGeneric,
|
writeGeneric,
|
||||||
onStreamRead,
|
onStreamRead,
|
||||||
kAfterAsyncWrite,
|
kAfterAsyncWrite,
|
||||||
kUpdateTimer
|
kUpdateTimer,
|
||||||
|
setStreamTimeout
|
||||||
} = require('internal/stream_base_commons');
|
} = require('internal/stream_base_commons');
|
||||||
const {
|
const {
|
||||||
codes: {
|
codes: {
|
||||||
@ -89,11 +90,7 @@ const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
|
|||||||
let cluster;
|
let cluster;
|
||||||
let dns;
|
let dns;
|
||||||
|
|
||||||
const {
|
const { kTimeout } = require('internal/timers');
|
||||||
kTimeout,
|
|
||||||
setUnrefTimeout,
|
|
||||||
validateTimerDuration
|
|
||||||
} = require('internal/timers');
|
|
||||||
|
|
||||||
function noop() {}
|
function noop() {}
|
||||||
|
|
||||||
@ -405,28 +402,7 @@ function writeAfterFIN(chunk, encoding, cb) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket.prototype.setTimeout = function(msecs, callback) {
|
Socket.prototype.setTimeout = setStreamTimeout;
|
||||||
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._onTimeout = function() {
|
Socket.prototype._onTimeout = function() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user