From b3a900cd38f2538237e40a772b8dc29204d5bea9 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 7 Feb 2018 01:38:45 +0100 Subject: [PATCH] http2: use `_final` instead of `on('finish')` --- lib/internal/http2/core.js | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index f735e2fcc92f62..7e1b0b3dff17ca 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -1415,18 +1415,6 @@ function afterDoStreamWrite(status, handle, req) { req.handle = undefined; } -function onHandleFinish() { - const handle = this[kHandle]; - if (this[kID] === undefined) { - this.once('ready', onHandleFinish); - } else if (handle !== undefined) { - const req = new ShutdownWrap(); - req.oncomplete = () => {}; - req.handle = handle; - handle.shutdown(req); - } -} - function streamOnResume() { if (!this.destroyed && !this.pending) this[kHandle].readStart(); @@ -1447,6 +1435,13 @@ function abort(stream) { } } +function afterShutdown() { + this.callback(); + const stream = this.handle[kOwner]; + if (stream) + stream[kMaybeDestroy](); +} + // An Http2Stream is a Duplex stream that is backed by a // node::http2::Http2Stream handle implementing StreamBase. class Http2Stream extends Duplex { @@ -1471,7 +1466,6 @@ class Http2Stream extends Duplex { writeQueueSize: 0 }; - this.once('finish', onHandleFinish); this.on('resume', streamOnResume); this.on('pause', streamOnPause); } @@ -1678,6 +1672,23 @@ class Http2Stream extends Duplex { trackWriteState(this, req.bytes); } + _final(cb) { + const handle = this[kHandle]; + if (this[kID] === undefined) { + this.once('ready', () => this._final(cb)); + } else if (handle !== undefined) { + debug(`Http2Stream ${this[kID]} [Http2Session ` + + `${sessionName(this[kSession][kType])}]: _final shutting down`); + const req = new ShutdownWrap(); + req.oncomplete = afterShutdown; + req.callback = cb; + req.handle = handle; + handle.shutdown(req); + } else { + cb(); + } + } + _read(nread) { if (this.destroyed) { this.push(null);