Skip to content

Commit d614511

Browse files
ryzokukenMylesBorins
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. PR-URL: #20643 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 010ad8c commit d614511

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

lib/internal/http2/core.js

+15-32
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ const kSession = Symbol('session');
146146
const kState = Symbol('state');
147147
const kType = Symbol('type');
148148
const kUpdateTimer = Symbol('update-timer');
149+
const kWriteGeneric = Symbol('write-generic');
149150

150151
const kDefaultSocketTimeout = 2 * 60 * 1000;
151152

@@ -1657,13 +1658,16 @@ class Http2Stream extends Duplex {
16571658
'bug in Node.js');
16581659
}
16591660

1660-
_write(data, encoding, cb) {
1661+
[kWriteGeneric](writev, data, encoding, cb) {
16611662
// When the Http2Stream is first created, it is corked until the
16621663
// handle and the stream ID is assigned. However, if the user calls
16631664
// uncork() before that happens, the Duplex will attempt to pass
16641665
// writes through. Those need to be queued up here.
16651666
if (this.pending) {
1666-
this.once('ready', this._write.bind(this, data, encoding, cb));
1667+
this.once(
1668+
'ready',
1669+
this[kWriteGeneric].bind(this, writev, data, encoding, cb)
1670+
);
16671671
return;
16681672
}
16691673

@@ -1683,41 +1687,20 @@ class Http2Stream extends Duplex {
16831687
const req = createWriteWrap(this[kHandle], afterDoStreamWrite);
16841688
req.stream = this[kID];
16851689

1686-
writeGeneric(this, req, data, encoding, cb);
1690+
if (writev)
1691+
writevGeneric(this, req, data, cb);
1692+
else
1693+
writeGeneric(this, req, data, encoding, cb);
16871694

16881695
trackWriteState(this, req.bytes);
16891696
}
16901697

1691-
_writev(data, cb) {
1692-
// When the Http2Stream is first created, it is corked until the
1693-
// handle and the stream ID is assigned. However, if the user calls
1694-
// uncork() before that happens, the Duplex will attempt to pass
1695-
// writes through. Those need to be queued up here.
1696-
if (this.pending) {
1697-
this.once('ready', this._writev.bind(this, data, cb));
1698-
return;
1699-
}
1700-
1701-
// If the stream has been destroyed, there's nothing else we can do
1702-
// because the handle has been destroyed. This should only be an
1703-
// issue if a write occurs before the 'ready' event in the case where
1704-
// the duplex is uncorked before the stream is ready to go. In that
1705-
// case, drop the data on the floor. An error should have already been
1706-
// emitted.
1707-
if (this.destroyed)
1708-
return;
1709-
1710-
this[kUpdateTimer]();
1711-
1712-
if (!this.headersSent)
1713-
this[kProceed]();
1714-
1715-
var req = createWriteWrap(this[kHandle], afterDoStreamWrite);
1716-
req.stream = this[kID];
1717-
1718-
writevGeneric(this, req, data, cb);
1698+
_write(data, encoding, cb) {
1699+
this[kWriteGeneric](false, data, encoding, cb);
1700+
}
17191701

1720-
trackWriteState(this, req.bytes);
1702+
_writev(data, cb) {
1703+
this[kWriteGeneric](true, data, '', cb);
17211704
}
17221705

17231706
_final(cb) {

lib/net.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -744,13 +744,13 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
744744
this._pendingData = null;
745745
this._pendingEncoding = '';
746746

747-
this._unrefTimer();
748-
749747
if (!this._handle) {
750748
this.destroy(new ERR_SOCKET_CLOSED(), cb);
751749
return false;
752750
}
753751

752+
this._unrefTimer();
753+
754754
var req = createWriteWrap(this._handle, afterWrite);
755755
if (writev)
756756
writevGeneric(this, req, data, cb);

0 commit comments

Comments
 (0)