Skip to content

Commit b0e3ce9

Browse files
ryzokukenBethGriggs
authored andcommitted
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. Backport-PR-URL: #22850 PR-URL: #20643 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent b0c92ca commit b0e3ce9

File tree

1 file changed

+24
-43
lines changed

1 file changed

+24
-43
lines changed

lib/internal/http2/core.js

+24-43
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const kSession = Symbol('session');
9595
const kState = Symbol('state');
9696
const kType = Symbol('type');
9797
const kUpdateTimer = Symbol('update-timer');
98+
const kWriteGeneric = Symbol('write-generic');
9899

99100
const kDefaultSocketTimeout = 2 * 60 * 1000;
100101

@@ -1635,13 +1636,16 @@ class Http2Stream extends Duplex {
16351636
'bug in Node.js');
16361637
}
16371638

1638-
_write(data, encoding, cb) {
1639+
[kWriteGeneric](writev, data, encoding, cb) {
16391640
// When the Http2Stream is first created, it is corked until the
16401641
// handle and the stream ID is assigned. However, if the user calls
16411642
// uncork() before that happens, the Duplex will attempt to pass
16421643
// writes through. Those need to be queued up here.
16431644
if (this.pending) {
1644-
this.once('ready', this._write.bind(this, data, encoding, cb));
1645+
this.once(
1646+
'ready',
1647+
this[kWriteGeneric].bind(this, writev, data, encoding, cb)
1648+
);
16451649
return;
16461650
}
16471651

@@ -1665,53 +1669,30 @@ class Http2Stream extends Duplex {
16651669
req.callback = cb;
16661670
req.oncomplete = afterDoStreamWrite;
16671671
req.async = false;
1668-
const err = createWriteReq(req, handle, data, encoding);
1672+
1673+
let err;
1674+
if (writev) {
1675+
const chunks = new Array(data.length << 1);
1676+
for (var i = 0; i < data.length; i++) {
1677+
const entry = data[i];
1678+
chunks[i * 2] = entry.chunk;
1679+
chunks[i * 2 + 1] = entry.encoding;
1680+
}
1681+
err = handle.writev(req, chunks);
1682+
} else {
1683+
err = createWriteReq(req, handle, data, encoding);
1684+
}
16691685
if (err)
16701686
return this.destroy(errors.errnoException(err, 'write', req.error), cb);
16711687
trackWriteState(this, req.bytes);
16721688
}
16731689

1674-
_writev(data, cb) {
1675-
// When the Http2Stream is first created, it is corked until the
1676-
// handle and the stream ID is assigned. However, if the user calls
1677-
// uncork() before that happens, the Duplex will attempt to pass
1678-
// writes through. Those need to be queued up here.
1679-
if (this.pending) {
1680-
this.once('ready', this._writev.bind(this, data, cb));
1681-
return;
1682-
}
1683-
1684-
// If the stream has been destroyed, there's nothing else we can do
1685-
// because the handle has been destroyed. This should only be an
1686-
// issue if a write occurs before the 'ready' event in the case where
1687-
// the duplex is uncorked before the stream is ready to go. In that
1688-
// case, drop the data on the floor. An error should have already been
1689-
// emitted.
1690-
if (this.destroyed)
1691-
return;
1692-
1693-
this[kUpdateTimer]();
1694-
1695-
if (!this.headersSent)
1696-
this[kProceed]();
1690+
_write(data, encoding, cb) {
1691+
this[kWriteGeneric](false, data, encoding, cb);
1692+
}
16971693

1698-
const handle = this[kHandle];
1699-
const req = new WriteWrap();
1700-
req.stream = this[kID];
1701-
req.handle = handle;
1702-
req.callback = cb;
1703-
req.oncomplete = afterDoStreamWrite;
1704-
req.async = false;
1705-
const chunks = new Array(data.length << 1);
1706-
for (var i = 0; i < data.length; i++) {
1707-
const entry = data[i];
1708-
chunks[i * 2] = entry.chunk;
1709-
chunks[i * 2 + 1] = entry.encoding;
1710-
}
1711-
const err = handle.writev(req, chunks);
1712-
if (err)
1713-
return this.destroy(errors.errnoException(err, 'write', req.error), cb);
1714-
trackWriteState(this, req.bytes);
1694+
_writev(data, cb) {
1695+
this[kWriteGeneric](true, data, '', cb);
17151696
}
17161697

17171698
_final(cb) {

0 commit comments

Comments
 (0)