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

streams: support unlimited synchronous cork/uncork cycles #6164

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
15 changes: 9 additions & 6 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@ function WritableState(options, stream) {
// count buffered requests
this.bufferedRequestCount = 0;

// create the two objects needed to store the corked requests
// they are not a linked list, as no new elements are inserted in there
// allocate the first CorkedRequest, there is always
// one allocated and free to use, and we maintain at most two
this.corkedRequestsFree = new CorkedRequest(this);
this.corkedRequestsFree.next = new CorkedRequest(this);
}

WritableState.prototype.getBuffer = function writableStateGetBuffer() {
Expand Down Expand Up @@ -387,12 +386,16 @@ function clearBuffer(stream, state) {

doWrite(stream, state, true, state.length, buffer, '', holder.finish);

// doWrite is always async, defer these to save a bit of time
// doWrite is almost always async, defer these to save a bit of time
// as the hot path ends with doWrite
state.pendingcb++;
state.lastBufferedRequest = null;
state.corkedRequestsFree = holder.next;
holder.next = null;
if (holder.next) {
state.corkedRequestsFree = holder.next;
holder.next = null;
} else {
state.corkedRequestsFree = new CorkedRequest(state);
}
} else {
// Slow case, write chunks one-by-one
while (entry) {
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-net-sync-cork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const net = require('net');

const server = net.createServer(handle);

const N = 100;
const buf = Buffer.alloc(2, 'a');

server.listen(common.PORT, function() {
const conn = net.connect(common.PORT);

conn.on('connect', () => {
let res = true;
let i = 0;
for (; i < N && res; i++) {
conn.cork();
conn.write(buf);
res = conn.write(buf);
conn.uncork();
}
assert.equal(i, N);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also assert res is still true, since it could have been false in the last iteration.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it's not really an assert on that. I was also afraid of putting the assert in i, as it could be spurious (it depends on OS buffers and the like). I would avoid to stretching luck, I picked 100 as a random number that was working on my box.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100 seems reasonable, a comment there would be nice but I guess it's pretty straightforward as is.

conn.end();
});
});

process.on('exit', function() {
assert.equal(server.connections, 0);
});

function handle(socket) {
socket.resume();

socket.on('error', function(err) {
socket.destroy();
}).on('close', function() {
server.close();
});
}