Skip to content

Commit 085dd30

Browse files
committed
http: provide backpressure for pipeline flood
If a client sends a lot more pipelined requests than we can handle, then we need to provide backpressure so that the client knows to back off. Do this by pausing both the stream and the parser itself when the responses are not being read by the downstream client. Fix GH-6214
1 parent ab03745 commit 085dd30

File tree

4 files changed

+148
-5
lines changed

4 files changed

+148
-5
lines changed

lib/_http_common.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,8 @@ function parserOnMessageComplete() {
157157
stream.push(null);
158158
}
159159

160-
if (parser.socket.readable) {
161-
// force to read the next incoming message
162-
readStart(parser.socket);
163-
}
160+
// force to read the next incoming message
161+
readStart(parser.socket);
164162
}
165163

166164

lib/_http_incoming.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var util = require('util');
2323
var Stream = require('stream');
2424

2525
function readStart(socket) {
26-
if (socket)
26+
if (socket && !socket._paused && socket.readable)
2727
socket.resume();
2828
}
2929
exports.readStart = readStart;

lib/_http_server.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ function connectionListener(socket) {
351351
}
352352

353353
function socketOnData(d) {
354+
assert(!socket._paused);
354355
debug('SERVER socketOnData %d', d.length);
355356
var ret = parser.execute(d);
356357
if (ret instanceof Error) {
@@ -382,6 +383,12 @@ function connectionListener(socket) {
382383
socket.destroy();
383384
}
384385
}
386+
387+
if (socket._paused) {
388+
// onIncoming paused the socket, we should pause the parser as well
389+
debug('pause parser');
390+
socket.parser.pause();
391+
}
385392
}
386393

387394
function socketOnEnd() {
@@ -411,9 +418,34 @@ function connectionListener(socket) {
411418
// new message. In this callback we setup the response object and pass it
412419
// to the user.
413420

421+
socket._paused = false;
422+
function socketOnDrain() {
423+
// If we previously paused, then start reading again.
424+
if (socket._paused) {
425+
socket._paused = false;
426+
socket.parser.resume();
427+
socket.resume();
428+
}
429+
}
430+
socket.on('drain', socketOnDrain);
431+
414432
function parserOnIncoming(req, shouldKeepAlive) {
415433
incoming.push(req);
416434

435+
// If the writable end isn't consuming, then stop reading
436+
// so that we don't become overwhelmed by a flood of
437+
// pipelined requests that may never be resolved.
438+
if (!socket._paused) {
439+
var needPause = socket._writableState.needDrain;
440+
if (needPause) {
441+
socket._paused = true;
442+
// We also need to pause the parser, but don't do that until after
443+
// the call to execute, because we may still be processing the last
444+
// chunk.
445+
socket.pause();
446+
}
447+
}
448+
417449
var res = new ServerResponse(req);
418450

419451
res.shouldKeepAlive = shouldKeepAlive;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
switch (process.argv[2]) {
26+
case undefined:
27+
return parent();
28+
case 'child':
29+
return child();
30+
default:
31+
throw new Error('wtf');
32+
}
33+
34+
function parent() {
35+
var http = require('http');
36+
var bigResponse = new Buffer(10240).fill('x');
37+
var gotTimeout = false;
38+
var childClosed = false;
39+
var requests = 0;
40+
var connections = 0;
41+
42+
var server = http.createServer(function(req, res) {
43+
requests++;
44+
res.setHeader('content-length', bigResponse.length);
45+
res.end(bigResponse);
46+
});
47+
48+
server.on('connection', function(conn) {
49+
connections++;
50+
});
51+
52+
// kill the connection after a bit, verifying that the
53+
// flood of requests was eventually halted.
54+
server.setTimeout(200, function(conn) {
55+
gotTimeout = true;
56+
conn.destroy();
57+
});
58+
59+
server.listen(common.PORT, function() {
60+
var spawn = require('child_process').spawn;
61+
var args = [__filename, 'child'];
62+
var child = spawn(process.execPath, args, { stdio: 'inherit' });
63+
child.on('close', function(code) {
64+
assert(!code);
65+
childClosed = true;
66+
server.close();
67+
});
68+
});
69+
70+
process.on('exit', function() {
71+
assert(gotTimeout);
72+
assert(childClosed);
73+
assert.equal(connections, 1);
74+
// 1213 works out to be the number of requests we end up processing
75+
// before the outgoing connection backs up and requires a drain.
76+
// however, to avoid being unnecessarily tied to a specific magic number,
77+
// and making the test brittle, just assert that it's "a lot", which we
78+
// can safely assume is more than 500.
79+
assert(requests >= 500);
80+
console.log('ok');
81+
});
82+
}
83+
84+
function child() {
85+
var net = require('net');
86+
87+
var gotEpipe = false;
88+
var conn = net.connect({ port: common.PORT });
89+
90+
var req = 'GET / HTTP/1.1\r\nHost: localhost:' +
91+
common.PORT + '\r\nAccept: */*\r\n\r\n';
92+
93+
req = new Array(10241).join(req);
94+
95+
conn.on('connect', function() {
96+
write();
97+
});
98+
99+
conn.on('drain', write);
100+
101+
conn.on('error', function(er) {
102+
gotEpipe = true;
103+
});
104+
105+
process.on('exit', function() {
106+
assert(gotEpipe);
107+
console.log('ok - child');
108+
});
109+
110+
function write() {
111+
while (false !== conn.write(req, 'ascii'));
112+
}
113+
}

0 commit comments

Comments
 (0)