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 <apapirovski@mac.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
9349e15daa
commit
a7fa0dba88
@ -146,6 +146,7 @@ const kSession = Symbol('session');
|
|||||||
const kState = Symbol('state');
|
const kState = Symbol('state');
|
||||||
const kType = Symbol('type');
|
const kType = Symbol('type');
|
||||||
const kUpdateTimer = Symbol('update-timer');
|
const kUpdateTimer = Symbol('update-timer');
|
||||||
|
const kWriteGeneric = Symbol('write-generic');
|
||||||
|
|
||||||
const kDefaultSocketTimeout = 2 * 60 * 1000;
|
const kDefaultSocketTimeout = 2 * 60 * 1000;
|
||||||
|
|
||||||
@ -1657,13 +1658,16 @@ class Http2Stream extends Duplex {
|
|||||||
'bug in Node.js');
|
'bug in Node.js');
|
||||||
}
|
}
|
||||||
|
|
||||||
_write(data, encoding, cb) {
|
[kWriteGeneric](writev, data, encoding, cb) {
|
||||||
// When the Http2Stream is first created, it is corked until the
|
// When the Http2Stream is first created, it is corked until the
|
||||||
// handle and the stream ID is assigned. However, if the user calls
|
// handle and the stream ID is assigned. However, if the user calls
|
||||||
// uncork() before that happens, the Duplex will attempt to pass
|
// uncork() before that happens, the Duplex will attempt to pass
|
||||||
// writes through. Those need to be queued up here.
|
// writes through. Those need to be queued up here.
|
||||||
if (this.pending) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1683,41 +1687,20 @@ class Http2Stream extends Duplex {
|
|||||||
const req = createWriteWrap(this[kHandle], afterDoStreamWrite);
|
const req = createWriteWrap(this[kHandle], afterDoStreamWrite);
|
||||||
req.stream = this[kID];
|
req.stream = this[kID];
|
||||||
|
|
||||||
|
if (writev)
|
||||||
|
writevGeneric(this, req, data, cb);
|
||||||
|
else
|
||||||
writeGeneric(this, req, data, encoding, cb);
|
writeGeneric(this, req, data, encoding, cb);
|
||||||
|
|
||||||
trackWriteState(this, req.bytes);
|
trackWriteState(this, req.bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
_writev(data, cb) {
|
_write(data, encoding, cb) {
|
||||||
// When the Http2Stream is first created, it is corked until the
|
this[kWriteGeneric](false, data, encoding, cb);
|
||||||
// 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
|
_writev(data, cb) {
|
||||||
// because the handle has been destroyed. This should only be an
|
this[kWriteGeneric](true, data, '', cb);
|
||||||
// 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);
|
|
||||||
|
|
||||||
trackWriteState(this, req.bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_final(cb) {
|
_final(cb) {
|
||||||
|
@ -744,13 +744,13 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) {
|
|||||||
this._pendingData = null;
|
this._pendingData = null;
|
||||||
this._pendingEncoding = '';
|
this._pendingEncoding = '';
|
||||||
|
|
||||||
this._unrefTimer();
|
|
||||||
|
|
||||||
if (!this._handle) {
|
if (!this._handle) {
|
||||||
this.destroy(new ERR_SOCKET_CLOSED(), cb);
|
this.destroy(new ERR_SOCKET_CLOSED(), cb);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._unrefTimer();
|
||||||
|
|
||||||
var req = createWriteWrap(this._handle, afterWrite);
|
var req = createWriteWrap(this._handle, afterWrite);
|
||||||
if (writev)
|
if (writev)
|
||||||
writevGeneric(this, req, data, cb);
|
writevGeneric(this, req, data, cb);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user