From 8457033d83fc5b1c3d9975815534a2c6c6bab5cc Mon Sep 17 00:00:00 2001 From: rickyes Date: Mon, 11 May 2020 23:08:44 +0800 Subject: [PATCH] http2: reuse ._onTimeout() in Http2Session and Http2Stream classes PR-URL: https://github.com/nodejs/node/pull/33354 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Ruben Bridgewater --- lib/internal/http2/core.js | 66 +++++++++++++++----------------------- 1 file changed, 26 insertions(+), 40 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 966bcbb5c9ece2..b89ac6a439230e 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -1460,27 +1460,7 @@ class Http2Session extends EventEmitter { } _onTimeout() { - // If the session is destroyed, this should never actually be invoked, - // but just in case... - if (this.destroyed) - return; - // This checks whether a write is currently in progress and also whether - // that write is actually sending data across the write. The kHandle - // stored `chunksSentSinceLastWrite` is only updated when a timeout event - // happens, meaning that if a write is ongoing it should never equal the - // newly fetched, updated value. - if (this[kState].writeQueueSize > 0) { - const handle = this[kHandle]; - const chunksSentSinceLastWrite = handle !== undefined ? - handle.chunksSentSinceLastWrite : null; - if (chunksSentSinceLastWrite !== null && - chunksSentSinceLastWrite !== handle.updateChunksSent()) { - this[kUpdateTimer](); - return; - } - } - - this.emit('timeout'); + callTimeout(this); } ref() { @@ -1906,25 +1886,7 @@ class Http2Stream extends Duplex { } _onTimeout() { - if (this.destroyed) - return; - // This checks whether a write is currently in progress and also whether - // that write is actually sending data across the write. The kHandle - // stored `chunksSentSinceLastWrite` is only updated when a timeout event - // happens, meaning that if a write is ongoing it should never equal the - // newly fetched, updated value. - if (this[kState].writeQueueSize > 0) { - const handle = this[kSession][kHandle]; - const chunksSentSinceLastWrite = handle !== undefined ? - handle.chunksSentSinceLastWrite : null; - if (chunksSentSinceLastWrite !== null && - chunksSentSinceLastWrite !== handle.updateChunksSent()) { - this[kUpdateTimer](); - return; - } - } - - this.emit('timeout'); + callTimeout(this, kSession); } // True if the HEADERS frame has been sent @@ -2202,6 +2164,30 @@ class Http2Stream extends Duplex { } } +function callTimeout(self, kSession) { + // If the session is destroyed, this should never actually be invoked, + // but just in case... + if (self.destroyed) + return; + // This checks whether a write is currently in progress and also whether + // that write is actually sending data across the write. The kHandle + // stored `chunksSentSinceLastWrite` is only updated when a timeout event + // happens, meaning that if a write is ongoing it should never equal the + // newly fetched, updated value. + if (self[kState].writeQueueSize > 0) { + const handle = kSession ? self[kSession][kHandle] : self[kHandle]; + const chunksSentSinceLastWrite = handle !== undefined ? + handle.chunksSentSinceLastWrite : null; + if (chunksSentSinceLastWrite !== null && + chunksSentSinceLastWrite !== handle.updateChunksSent()) { + self[kUpdateTimer](); + return; + } + } + + self.emit('timeout'); +} + function callStreamClose(stream) { stream.close(); }