http: always cork outgoing writes
PR-URL: https://github.com/nodejs/node/pull/13522 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
This commit is contained in:
parent
77d575d005
commit
c4fc7d90ed
@ -27,13 +27,14 @@ module.exports = http.createServer(function(req, res) {
|
|||||||
dom.add(res);
|
dom.add(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
// URL format: /<type>/<length>/<chunks>/<responseBehavior>
|
// URL format: /<type>/<length>/<chunks>/<responseBehavior>/chunkedEnc
|
||||||
var params = req.url.split('/');
|
var params = req.url.split('/');
|
||||||
var command = params[1];
|
var command = params[1];
|
||||||
var body = '';
|
var body = '';
|
||||||
var arg = params[2];
|
var arg = params[2];
|
||||||
var n_chunks = parseInt(params[3], 10);
|
var n_chunks = parseInt(params[3], 10);
|
||||||
var resHow = (params.length >= 5 ? params[4] : 'normal');
|
var resHow = (params.length >= 5 ? params[4] : 'normal');
|
||||||
|
var chunkedEnc = (params.length >= 6 && params[5] === 'false' ? false : true);
|
||||||
var status = 200;
|
var status = 200;
|
||||||
|
|
||||||
var n, i;
|
var n, i;
|
||||||
@ -95,48 +96,43 @@ module.exports = http.createServer(function(req, res) {
|
|||||||
|
|
||||||
// example: http://localhost:port/bytes/512/4
|
// example: http://localhost:port/bytes/512/4
|
||||||
// sends a 512 byte body in 4 chunks of 128 bytes
|
// sends a 512 byte body in 4 chunks of 128 bytes
|
||||||
if (n_chunks > 0) {
|
var len = body.length;
|
||||||
switch (resHow) {
|
switch (resHow) {
|
||||||
case 'setHeader':
|
case 'setHeader':
|
||||||
res.statusCode = status;
|
res.statusCode = status;
|
||||||
res.setHeader('Content-Type', 'text/plain');
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
if (chunkedEnc)
|
||||||
res.setHeader('Transfer-Encoding', 'chunked');
|
res.setHeader('Transfer-Encoding', 'chunked');
|
||||||
break;
|
else
|
||||||
case 'setHeaderWH':
|
res.setHeader('Content-Length', len.toString());
|
||||||
res.setHeader('Content-Type', 'text/plain');
|
break;
|
||||||
|
case 'setHeaderWH':
|
||||||
|
res.setHeader('Content-Type', 'text/plain');
|
||||||
|
if (chunkedEnc)
|
||||||
res.writeHead(status, { 'Transfer-Encoding': 'chunked' });
|
res.writeHead(status, { 'Transfer-Encoding': 'chunked' });
|
||||||
break;
|
else
|
||||||
default:
|
res.writeHead(status, { 'Content-Length': len.toString() });
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (chunkedEnc) {
|
||||||
res.writeHead(status, {
|
res.writeHead(status, {
|
||||||
'Content-Type': 'text/plain',
|
'Content-Type': 'text/plain',
|
||||||
'Transfer-Encoding': 'chunked'
|
'Transfer-Encoding': 'chunked'
|
||||||
});
|
});
|
||||||
}
|
} else {
|
||||||
// send body in chunks
|
|
||||||
var len = body.length;
|
|
||||||
var step = Math.floor(len / n_chunks) || 1;
|
|
||||||
|
|
||||||
for (i = 0, n = (n_chunks - 1); i < n; ++i) {
|
|
||||||
res.write(body.slice(i * step, i * step + step));
|
|
||||||
}
|
|
||||||
res.end(body.slice((n_chunks - 1) * step));
|
|
||||||
} else {
|
|
||||||
switch (resHow) {
|
|
||||||
case 'setHeader':
|
|
||||||
res.statusCode = status;
|
|
||||||
res.setHeader('Content-Type', 'text/plain');
|
|
||||||
res.setHeader('Content-Length', body.length.toString());
|
|
||||||
break;
|
|
||||||
case 'setHeaderWH':
|
|
||||||
res.setHeader('Content-Type', 'text/plain');
|
|
||||||
res.writeHead(status, { 'Content-Length': body.length.toString() });
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
res.writeHead(status, {
|
res.writeHead(status, {
|
||||||
'Content-Type': 'text/plain',
|
'Content-Type': 'text/plain',
|
||||||
'Content-Length': body.length.toString()
|
'Content-Length': len.toString()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// send body in chunks
|
||||||
|
if (n_chunks > 1) {
|
||||||
|
var step = Math.floor(len / n_chunks) || 1;
|
||||||
|
for (i = 0, n = (n_chunks - 1); i < n; ++i)
|
||||||
|
res.write(body.slice(i * step, i * step + step));
|
||||||
|
res.end(body.slice((n_chunks - 1) * step));
|
||||||
|
} else {
|
||||||
res.end(body);
|
res.end(body);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -6,8 +6,9 @@ var bench = common.createBenchmark(main, {
|
|||||||
// unicode confuses ab on os x.
|
// unicode confuses ab on os x.
|
||||||
type: ['bytes', 'buffer'],
|
type: ['bytes', 'buffer'],
|
||||||
len: [4, 1024, 102400],
|
len: [4, 1024, 102400],
|
||||||
chunks: [0, 1, 4], // chunks=0 means 'no chunked encoding'.
|
chunks: [1, 4],
|
||||||
c: [50, 500],
|
c: [50, 500],
|
||||||
|
chunkedEnc: ['true', 'false'],
|
||||||
res: ['normal', 'setHeader', 'setHeaderWH']
|
res: ['normal', 'setHeader', 'setHeaderWH']
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -16,7 +17,8 @@ function main(conf) {
|
|||||||
var server = require('../fixtures/simple-http-server.js')
|
var server = require('../fixtures/simple-http-server.js')
|
||||||
.listen(process.env.PORT || common.PORT)
|
.listen(process.env.PORT || common.PORT)
|
||||||
.on('listening', function() {
|
.on('listening', function() {
|
||||||
var path = `/${conf.type}/${conf.len}/${conf.chunks}/${conf.res}`;
|
var path =
|
||||||
|
`/${conf.type}/${conf.len}/${conf.chunks}/${conf.res}/${conf.chunkedEnc}`;
|
||||||
|
|
||||||
bench.http({
|
bench.http({
|
||||||
path: path,
|
path: path,
|
||||||
|
@ -660,6 +660,11 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
|
|||||||
// signal the user to keep writing.
|
// signal the user to keep writing.
|
||||||
if (chunk.length === 0) return true;
|
if (chunk.length === 0) return true;
|
||||||
|
|
||||||
|
if (!fromEnd && msg.connection && !msg.connection.corked) {
|
||||||
|
msg.connection.cork();
|
||||||
|
process.nextTick(connectionCorkNT, msg.connection);
|
||||||
|
}
|
||||||
|
|
||||||
var len, ret;
|
var len, ret;
|
||||||
if (msg.chunkedEncoding) {
|
if (msg.chunkedEncoding) {
|
||||||
if (typeof chunk === 'string')
|
if (typeof chunk === 'string')
|
||||||
@ -667,11 +672,6 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
|
|||||||
else
|
else
|
||||||
len = chunk.length;
|
len = chunk.length;
|
||||||
|
|
||||||
if (msg.connection && !msg.connection.corked) {
|
|
||||||
msg.connection.cork();
|
|
||||||
process.nextTick(connectionCorkNT, msg.connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
msg._send(len.toString(16), 'latin1', null);
|
msg._send(len.toString(16), 'latin1', null);
|
||||||
msg._send(crlf_buf, null, null);
|
msg._send(crlf_buf, null, null);
|
||||||
msg._send(chunk, encoding, null);
|
msg._send(chunk, encoding, null);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user