streams: support unlimited synchronous cork/uncork cycles
net streams can request multiple chunks to be written in a synchronous
fashion. If this is combined with cork/uncork, en error is currently
thrown because of a regression introduced in:
89aeab901a
(https://github.com/nodejs/node/pull/4354).
Fixes: https://github.com/nodejs/node/issues/6154
PR-URL: https://github.com/nodejs/node/pull/6164
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Mathias Buus <mathiasbuus@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
a11d506dec
commit
0b1d89f35a
@ -112,10 +112,9 @@ function WritableState(options, stream) {
|
|||||||
// count buffered requests
|
// count buffered requests
|
||||||
this.bufferedRequestCount = 0;
|
this.bufferedRequestCount = 0;
|
||||||
|
|
||||||
// create the two objects needed to store the corked requests
|
// allocate the first CorkedRequest, there is always
|
||||||
// they are not a linked list, as no new elements are inserted in there
|
// one allocated and free to use, and we maintain at most two
|
||||||
this.corkedRequestsFree = new CorkedRequest(this);
|
this.corkedRequestsFree = new CorkedRequest(this);
|
||||||
this.corkedRequestsFree.next = new CorkedRequest(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WritableState.prototype.getBuffer = function writableStateGetBuffer() {
|
WritableState.prototype.getBuffer = function writableStateGetBuffer() {
|
||||||
@ -387,12 +386,16 @@ function clearBuffer(stream, state) {
|
|||||||
|
|
||||||
doWrite(stream, state, true, state.length, buffer, '', holder.finish);
|
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
|
// as the hot path ends with doWrite
|
||||||
state.pendingcb++;
|
state.pendingcb++;
|
||||||
state.lastBufferedRequest = null;
|
state.lastBufferedRequest = null;
|
||||||
state.corkedRequestsFree = holder.next;
|
if (holder.next) {
|
||||||
holder.next = null;
|
state.corkedRequestsFree = holder.next;
|
||||||
|
holder.next = null;
|
||||||
|
} else {
|
||||||
|
state.corkedRequestsFree = new CorkedRequest(state);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Slow case, write chunks one-by-one
|
// Slow case, write chunks one-by-one
|
||||||
while (entry) {
|
while (entry) {
|
||||||
|
41
test/parallel/test-net-sync-cork.js
Normal file
41
test/parallel/test-net-sync-cork.js
Normal file
@ -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);
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user