Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net,http2: refactor _write and _writev in net.Socket and http2.Http2Stream #20643

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 15 additions & 32 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,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 @@ -1657,13 +1658,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 @@ -1683,41 +1687,20 @@ class Http2Stream extends Duplex {
const req = createWriteWrap(this[kHandle], afterDoStreamWrite);
req.stream = this[kID];

writeGeneric(this, req, data, encoding, cb);
if (writev)
writevGeneric(this, req, data, cb);
else
writeGeneric(this, req, data, encoding, 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]();

var req = createWriteWrap(this[kHandle], afterDoStreamWrite);
req.stream = this[kID];

writevGeneric(this, req, data, cb);
_write(data, encoding, cb) {
this[kWriteGeneric](false, data, encoding, cb);
}

trackWriteState(this, req.bytes);
_writev(data, cb) {
this[kWriteGeneric](true, data, '', cb);
}

_final(cb) {
Expand Down
4 changes: 2 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,13 +745,13 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
this._pendingData = null;
this._pendingEncoding = '';

this._unrefTimer();

if (!this._handle) {
this.destroy(new ERR_SOCKET_CLOSED(), cb);
return false;
}

this._unrefTimer();

var req = createWriteWrap(this._handle, afterWrite);
if (writev)
writevGeneric(this, req, data, cb);
Expand Down