-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
http: coalesce chunked writes during auto-corking #64987
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
Open
GetThatCookie
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
GetThatCookie:pr64980_write_path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+65
−18
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ const { | |
| ERR_STREAM_DESTROYED, | ||
| ERR_STREAM_NULL_VALUES, | ||
| ERR_STREAM_WRITE_AFTER_END, | ||
| ERR_UNKNOWN_ENCODING, | ||
| }, | ||
| hideStackFrames, | ||
| } = require('internal/errors'); | ||
|
|
@@ -228,7 +229,11 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', { | |
| ObjectDefineProperty(OutgoingMessage.prototype, 'writableLength', { | ||
| __proto__: null, | ||
| get() { | ||
| return this.outputSize + this[kChunkedLength] + (this[kSocket] ? this[kSocket].writableLength : 0); | ||
| let bufferedLength = this[kChunkedLength]; | ||
| if (bufferedLength !== 0) { | ||
| bufferedLength += bufferedLength.toString(16).length + 4 + (!this._headerSent && this._header !== null ? this._header.length : 0); | ||
| } | ||
| return this.outputSize + bufferedLength + (this[kSocket] ? this[kSocket].writableLength : 0); | ||
| }, | ||
| }); | ||
|
|
||
|
|
@@ -299,17 +304,26 @@ OutgoingMessage.prototype.cork = function cork() { | |
|
|
||
| OutgoingMessage.prototype.uncork = function uncork() { | ||
| this[kCorked]--; | ||
| if (this[kSocket]) { | ||
| this[kSocket].uncork(); | ||
| } | ||
|
|
||
| const socket = this[kSocket]; | ||
| if (this[kCorked] || this[kChunkedBuffer].length === 0) { | ||
| socket?.uncork(); | ||
| return; | ||
| } | ||
|
|
||
| const len = this[kChunkedLength]; | ||
| const buf = this[kChunkedBuffer]; | ||
|
|
||
| if (this.destroyed || socket?.destroyed) { | ||
| const error = this[kErrored] || socket?._writableState?.errored || new ERR_STREAM_DESTROYED('write'); | ||
| for (let n = 2; n < buf.length; n += 3) { | ||
| if (buf[n] !== nop) { | ||
| process.nextTick(buf[n], error); | ||
| } | ||
| } | ||
| buf.length = 0; | ||
| this[kChunkedLength] = 0; | ||
| socket?.uncork(); | ||
| return; | ||
| } | ||
|
Comment on lines
+315
to
+326
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems unrelated? |
||
| assert(this.chunkedEncoding); | ||
|
|
||
| let callbacks; | ||
|
|
@@ -330,6 +344,7 @@ OutgoingMessage.prototype.uncork = function uncork() { | |
|
|
||
| this[kChunkedBuffer].length = 0; | ||
| this[kChunkedLength] = 0; | ||
| socket?.uncork(); | ||
|
|
||
| // If we had a pending drain and flushed all data, emit the drain event. | ||
| if (this[kNeedDrain] && this.writableLength === 0) { | ||
|
|
@@ -365,6 +380,10 @@ OutgoingMessage.prototype.destroy = function destroy(error) { | |
| this.destroyed = true; | ||
|
|
||
| this[kErrored] = error; | ||
| if (this[kChunkedBuffer].length !== 0) { | ||
| this[kCorked] = 1; | ||
| this.uncork(); | ||
| } | ||
|
|
||
| if (this[kSocket]) { | ||
| this[kSocket].destroy(error); | ||
|
|
@@ -1004,24 +1023,34 @@ function write_(msg, chunk, encoding, callback, fromEnd) { | |
| } | ||
| } | ||
|
|
||
| if (msg.chunkedEncoding && typeof chunk === 'string' && encoding && !Buffer.isEncoding(encoding)) { | ||
| throw new ERR_UNKNOWN_ENCODING(encoding); | ||
| } | ||
|
|
||
| if (!fromEnd && msg.socket && !msg.socket.writableCorked) { | ||
| msg.socket.cork(); | ||
| process.nextTick(connectionCorkNT, msg.socket); | ||
| msg.cork(); | ||
| process.nextTick(connectionCorkNT, msg, msg.socket); | ||
| } | ||
|
|
||
| let ret; | ||
| if (msg.chunkedEncoding && chunk.length !== 0) { | ||
| len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength; | ||
| if (msg[kCorked] && msg._headerSent) { | ||
| if (msg[kCorked]) { | ||
| if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) { | ||
| chunk = Stream._uint8ArrayToBuffer(chunk); | ||
| } | ||
| msg[kChunkedBuffer].push(chunk, encoding, callback); | ||
| msg[kChunkedLength] += len; | ||
| ret = msg[kChunkedLength] < msg[kHighWaterMark]; | ||
| ret = msg.writableLength < msg.writableHighWaterMark; | ||
| } else { | ||
| msg._send(len.toString(16), 'latin1', null); | ||
| msg._send(crlf_buf, null, null); | ||
| msg._send(chunk, encoding, null, len); | ||
| ret = msg._send(crlf_buf, null, callback); | ||
| } | ||
| } else if (msg.chunkedEncoding && msg[kCorked] && msg[kChunkedBuffer].length !== 0) { | ||
| msg[kChunkedBuffer].push(chunk, encoding, callback); | ||
| ret = msg.writableLength < msg.writableHighWaterMark; | ||
| } else { | ||
| ret = msg._send(chunk, encoding, callback, len); | ||
| } | ||
|
|
@@ -1031,8 +1060,13 @@ function write_(msg, chunk, encoding, callback, fromEnd) { | |
| } | ||
|
|
||
|
|
||
| function connectionCorkNT(conn) { | ||
| conn.uncork(); | ||
| function connectionCorkNT(msg, conn) { | ||
| if (msg[kCorked]) { | ||
| msg.uncork(); | ||
| } | ||
| if (msg[kSocket] !== conn) { | ||
| conn.uncork(); | ||
| } | ||
| } | ||
|
|
||
| OutgoingMessage.prototype.addTrailers = function addTrailers(headers) { | ||
|
|
@@ -1138,6 +1172,13 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { | |
| throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(this[kBytesWritten], this._contentLength); | ||
| } | ||
|
|
||
| // Flush buffered chunks before terminating the response. | ||
| if (this[kChunkedBuffer].length !== 0) { | ||
| this[kSocket]?.cork(); | ||
| this[kCorked] = 1; | ||
| this.uncork(); | ||
| } | ||
|
|
||
| const finish = onFinish.bind(undefined, this); | ||
|
|
||
| if (this._hasBody && this.chunkedEncoding) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will break the cork count? We always call socket.cork on cork, we need to do the same for uncork?