From c9f8d6f45e2173bca2327fca53fb0b5de204aebe Mon Sep 17 00:00:00 2001 From: Ujjwal Sharma Date: Thu, 10 May 2018 02:47:56 +0530 Subject: [PATCH] net,http2: refactor _write and _writev Refactor writable part (the _write and _writev functions) in net.Socket and http2.Http2Stream classes. Also involves adding a generic "WriteGeneric" method to the Http2Stream class based on net.Socket._writeGeneric, but behind a symbol. PR-URL: https://github.com/nodejs/node/pull/20643 Reviewed-By: Anatoli Papirovski Reviewed-By: Ruben Bridgewater --- lib/internal/http2/core.js | 67 ++++++++++++++------------------------ 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 5d70f436bbc676..106991dbd4224d 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -95,6 +95,7 @@ const kSession = Symbol('session'); const kState = Symbol('state'); const kType = Symbol('type'); const kUpdateTimer = Symbol('update-timer'); +const kWriteGeneric = Symbol('write-generic'); const kDefaultSocketTimeout = 2 * 60 * 1000; @@ -1635,13 +1636,16 @@ class Http2Stream extends Duplex { 'bug in Node.js'); } - _write(data, encoding, cb) { + [kWriteGeneric](writev, data, encoding, cb) { // When the Http2Stream is first created, it is corked until the // handle and the stream ID is assigned. However, if the user calls // uncork() before that happens, the Duplex will attempt to pass // writes through. Those need to be queued up here. if (this.pending) { - this.once('ready', this._write.bind(this, data, encoding, cb)); + this.once( + 'ready', + this[kWriteGeneric].bind(this, writev, data, encoding, cb) + ); return; } @@ -1665,53 +1669,30 @@ class Http2Stream extends Duplex { req.callback = cb; req.oncomplete = afterDoStreamWrite; req.async = false; - const err = createWriteReq(req, handle, data, encoding); + + let err; + if (writev) { + const chunks = new Array(data.length << 1); + for (var i = 0; i < data.length; i++) { + const entry = data[i]; + chunks[i * 2] = entry.chunk; + chunks[i * 2 + 1] = entry.encoding; + } + err = handle.writev(req, chunks); + } else { + err = createWriteReq(req, handle, data, encoding); + } if (err) return this.destroy(errors.errnoException(err, 'write', req.error), cb); trackWriteState(this, req.bytes); } - _writev(data, cb) { - // When the Http2Stream is first created, it is corked until the - // handle and the stream ID is assigned. However, if the user calls - // uncork() before that happens, the Duplex will attempt to pass - // writes through. Those need to be queued up here. - if (this.pending) { - this.once('ready', this._writev.bind(this, data, cb)); - return; - } - - // If the stream has been destroyed, there's nothing else we can do - // because the handle has been destroyed. This should only be an - // issue if a write occurs before the 'ready' event in the case where - // the duplex is uncorked before the stream is ready to go. In that - // case, drop the data on the floor. An error should have already been - // emitted. - if (this.destroyed) - return; - - this[kUpdateTimer](); - - if (!this.headersSent) - this[kProceed](); + _write(data, encoding, cb) { + this[kWriteGeneric](false, data, encoding, cb); + } - const handle = this[kHandle]; - const req = new WriteWrap(); - req.stream = this[kID]; - req.handle = handle; - req.callback = cb; - req.oncomplete = afterDoStreamWrite; - req.async = false; - const chunks = new Array(data.length << 1); - for (var i = 0; i < data.length; i++) { - const entry = data[i]; - chunks[i * 2] = entry.chunk; - chunks[i * 2 + 1] = entry.encoding; - } - const err = handle.writev(req, chunks); - if (err) - return this.destroy(errors.errnoException(err, 'write', req.error), cb); - trackWriteState(this, req.bytes); + _writev(data, cb) { + this[kWriteGeneric](true, data, '', cb); } _final(cb) {