Skip to content

Commit 802ea05

Browse files
ZYSzysaddaleax
authored andcommitted
net,http2: merge setTimeout code
PR-URL: #25084 Refs: #19060 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent bb774b1 commit 802ea05

File tree

3 files changed

+55
-65
lines changed

3 files changed

+55
-65
lines changed

lib/internal/http2/core.js

+5-36
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ const {
110110
onStreamRead,
111111
kAfterAsyncWrite,
112112
kMaybeDestroy,
113-
kUpdateTimer
113+
kUpdateTimer,
114+
kSession,
115+
setStreamTimeout
114116
} = require('internal/stream_base_commons');
115-
const {
116-
kTimeout,
117-
setUnrefTimeout,
118-
validateTimerDuration
119-
} = require('internal/timers');
117+
const { kTimeout } = require('internal/timers');
120118
const { isArrayBufferView } = require('internal/util/types');
121119

122120
const { FileHandle } = internalBinding('fs');
@@ -163,7 +161,6 @@ const kSelectPadding = Symbol('select-padding');
163161
const kSentHeaders = Symbol('sent-headers');
164162
const kSentTrailers = Symbol('sent-trailers');
165163
const kServer = Symbol('server');
166-
const kSession = Symbol('session');
167164
const kState = Symbol('state');
168165
const kType = Symbol('type');
169166
const kWriteGeneric = Symbol('write-generic');
@@ -2546,35 +2543,7 @@ const setTimeout = {
25462543
configurable: true,
25472544
enumerable: true,
25482545
writable: true,
2549-
value: function(msecs, callback) {
2550-
if (this.destroyed)
2551-
return;
2552-
2553-
// Type checking identical to timers.enroll()
2554-
msecs = validateTimerDuration(msecs);
2555-
2556-
// Attempt to clear an existing timer lear in both cases -
2557-
// even if it will be rescheduled we don't want to leak an existing timer.
2558-
clearTimeout(this[kTimeout]);
2559-
2560-
if (msecs === 0) {
2561-
if (callback !== undefined) {
2562-
if (typeof callback !== 'function')
2563-
throw new ERR_INVALID_CALLBACK();
2564-
this.removeListener('timeout', callback);
2565-
}
2566-
} else {
2567-
this[kTimeout] = setUnrefTimeout(this._onTimeout.bind(this), msecs);
2568-
if (this[kSession]) this[kSession][kUpdateTimer]();
2569-
2570-
if (callback !== undefined) {
2571-
if (typeof callback !== 'function')
2572-
throw new ERR_INVALID_CALLBACK();
2573-
this.once('timeout', callback);
2574-
}
2575-
}
2576-
return this;
2577-
}
2546+
value: setStreamTimeout
25782547
};
25792548
Object.defineProperty(Http2Stream.prototype, 'setTimeout', setTimeout);
25802549
Object.defineProperty(Http2Session.prototype, 'setTimeout', setTimeout);

lib/internal/stream_base_commons.js

+46-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@ const {
1111
streamBaseState
1212
} = internalBinding('stream_wrap');
1313
const { UV_EOF } = internalBinding('uv');
14-
const { errnoException } = require('internal/errors');
14+
const {
15+
codes: {
16+
ERR_INVALID_CALLBACK
17+
},
18+
errnoException
19+
} = require('internal/errors');
1520
const { owner_symbol } = require('internal/async_hooks').symbols;
21+
const {
22+
kTimeout,
23+
setUnrefTimeout,
24+
validateTimerDuration
25+
} = require('internal/timers');
1626

1727
const kMaybeDestroy = Symbol('kMaybeDestroy');
1828
const kUpdateTimer = Symbol('kUpdateTimer');
1929
const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
30+
const kSession = Symbol('session');
2031

2132
function handleWriteReq(req, data, encoding) {
2233
const { handle } = req;
@@ -178,6 +189,38 @@ function onStreamRead(arrayBuffer) {
178189
}
179190
}
180191

192+
function setStreamTimeout(msecs, callback) {
193+
if (this.destroyed)
194+
return;
195+
196+
this.timeout = msecs;
197+
198+
// Type checking identical to timers.enroll()
199+
msecs = validateTimerDuration(msecs);
200+
201+
// Attempt to clear an existing timer in both cases -
202+
// even if it will be rescheduled we don't want to leak an existing timer.
203+
clearTimeout(this[kTimeout]);
204+
205+
if (msecs === 0) {
206+
if (callback !== undefined) {
207+
if (typeof callback !== 'function')
208+
throw new ERR_INVALID_CALLBACK();
209+
this.removeListener('timeout', callback);
210+
}
211+
} else {
212+
this[kTimeout] = setUnrefTimeout(this._onTimeout.bind(this), msecs);
213+
if (this[kSession]) this[kSession][kUpdateTimer]();
214+
215+
if (callback !== undefined) {
216+
if (typeof callback !== 'function')
217+
throw new ERR_INVALID_CALLBACK();
218+
this.once('timeout', callback);
219+
}
220+
}
221+
return this;
222+
}
223+
181224
module.exports = {
182225
createWriteWrap,
183226
writevGeneric,
@@ -186,4 +229,6 @@ module.exports = {
186229
kAfterAsyncWrite,
187230
kMaybeDestroy,
188231
kUpdateTimer,
232+
kSession,
233+
setStreamTimeout
189234
};

lib/net.js

+4-28
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ const {
6363
writeGeneric,
6464
onStreamRead,
6565
kAfterAsyncWrite,
66-
kUpdateTimer
66+
kUpdateTimer,
67+
setStreamTimeout
6768
} = require('internal/stream_base_commons');
6869
const {
6970
codes: {
@@ -89,11 +90,7 @@ const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
8990
let cluster;
9091
let dns;
9192

92-
const {
93-
kTimeout,
94-
setUnrefTimeout,
95-
validateTimerDuration
96-
} = require('internal/timers');
93+
const { kTimeout } = require('internal/timers');
9794

9895
function noop() {}
9996

@@ -405,28 +402,7 @@ function writeAfterFIN(chunk, encoding, cb) {
405402
}
406403
}
407404

408-
Socket.prototype.setTimeout = function(msecs, callback) {
409-
this.timeout = msecs;
410-
// Type checking identical to timers.enroll()
411-
msecs = validateTimerDuration(msecs);
412-
413-
// Attempt to clear an existing timer in both cases -
414-
// even if it will be rescheduled we don't want to leak an existing timer.
415-
clearTimeout(this[kTimeout]);
416-
417-
if (msecs === 0) {
418-
if (callback) {
419-
this.removeListener('timeout', callback);
420-
}
421-
} else {
422-
this[kTimeout] = setUnrefTimeout(this._onTimeout.bind(this), msecs);
423-
424-
if (callback) {
425-
this.once('timeout', callback);
426-
}
427-
}
428-
return this;
429-
};
405+
Socket.prototype.setTimeout = setStreamTimeout;
430406

431407

432408
Socket.prototype._onTimeout = function() {

0 commit comments

Comments
 (0)