-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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: fix bytesWritten during writev #14236
Closed
brendanashworth
wants to merge
4
commits into
nodejs:master
from
brendanashworth:net/fix-byteswritten
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8191c01
net: fix bytesWritten during writev
brendanashworth ab6917b
!fixup fix test nits, net.js collapse if/else
brendanashworth 1979f7f
!fixup avoid instanceof with data.allBuffers
brendanashworth 9aadbf3
!fixup replace instanceof with typeof !== 'string'
brendanashworth 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 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 |
---|---|---|
|
@@ -825,11 +825,20 @@ protoGetter('bytesWritten', function bytesWritten() { | |
bytes += Buffer.byteLength(el.chunk, el.encoding); | ||
}); | ||
|
||
if (data) { | ||
if (data instanceof Buffer) | ||
bytes += data.length; | ||
else | ||
bytes += Buffer.byteLength(data, encoding); | ||
if (Array.isArray(data)) { | ||
// was a writev, iterate over chunks to get total length | ||
for (var i = 0; i < data.length; i++) { | ||
const chunk = data[i]; | ||
|
||
if (data.allBuffers || chunk instanceof Buffer) | ||
bytes += chunk.length; | ||
else | ||
bytes += Buffer.byteLength(chunk.chunk, chunk.encoding); | ||
} | ||
} else if (data instanceof Buffer) { | ||
bytes += data.length; | ||
} else if (data) { | ||
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. Perhaps this can be simplified a bit by merging with the if/else below: if (Array.isArray(data)) {
// ...
} else if (data instanceof Buffer) {
// ...
} else if (data) {
// ...
} 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. Good tip! I just collapsed the branches. |
||
bytes += Buffer.byteLength(data, encoding); | ||
} | ||
|
||
return bytes; | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
|
||
const server = net.createServer(function(socket) { | ||
socket.end(); | ||
}); | ||
|
||
server.listen(0, common.mustCall(function() { | ||
const socket = net.connect(server.address().port); | ||
|
||
// Cork the socket, then write twice; this should cause a writev, which | ||
// previously caused an err in the bytesWritten count. | ||
socket.cork(); | ||
|
||
socket.write('one'); | ||
socket.write(new Buffer('twø', 'utf8')); | ||
|
||
socket.uncork(); | ||
|
||
// one = 3 bytes, twø = 4 bytes | ||
assert.strictEqual(socket.bytesWritten, 3 + 4); | ||
|
||
socket.on('connect', common.mustCall(function() { | ||
assert.strictEqual(socket.bytesWritten, 3 + 4); | ||
})); | ||
|
||
socket.on('end', common.mustCall(function() { | ||
assert.strictEqual(socket.bytesWritten, 3 + 4); | ||
|
||
server.close(); | ||
})); | ||
})); |
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.
These instanceof checks are going to be expensive on perf
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 specific check wasn't added in this PR, it was just down-indented (no perf change).
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.
I wonder if we could get away with using
data != undefined && typeof data !== 'string'
instead? If so, we could first branch on the first condition and then do aif (typeof data !== 'string') { ... } else { ... }
to avoid duplicateif (data)
-style checks.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.
@mscdex Yes,
.write()
will error if it's neither a Buffer nor a string, so this works! I've updated it with the optimization.