Skip to content

Commit

Permalink
net,http2: refactor _write and _writev
Browse files Browse the repository at this point in the history
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: nodejs#20643
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
ryzokuken authored and kjin committed Sep 25, 2018
1 parent c27e2b8 commit f2b38b9
Showing 1 changed file with 24 additions and 43 deletions.
67 changes: 24 additions & 43 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down

0 comments on commit f2b38b9

Please sign in to comment.