Skip to content

Commit b97c28f

Browse files
isaacstrevnorris
authored andcommitted
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. Backport of 085dd30
1 parent f051b89 commit b97c28f

2 files changed

Lines changed: 151 additions & 5 deletions

File tree

lib/http.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ if (process.env.NODE_DEBUG && /http/.test(process.env.NODE_DEBUG)) {
3737
}
3838

3939
function readStart(socket) {
40-
if (!socket || !socket._handle || !socket._handle.readStart) return;
40+
if (!socket || !socket._handle || !socket._handle.readStart || socket._paused)
41+
return;
4142
socket._handle.readStart();
4243
}
4344

@@ -172,10 +173,8 @@ function parserOnMessageComplete() {
172173
stream.push(null);
173174
}
174175

175-
if (parser.socket.readable) {
176-
// force to read the next incoming message
177-
readStart(parser.socket);
178-
}
176+
// force to read the next incoming message
177+
readStart(parser.socket);
179178
}
180179

181180

@@ -1963,6 +1962,7 @@ function connectionListener(socket) {
19631962
});
19641963

19651964
socket.ondata = function(d, start, end) {
1965+
assert(!socket._paused);
19661966
var ret = parser.execute(d, start, end - start);
19671967
if (ret instanceof Error) {
19681968
debug('parse error');
@@ -1989,6 +1989,12 @@ function connectionListener(socket) {
19891989
socket.destroy();
19901990
}
19911991
}
1992+
1993+
if (socket._paused) {
1994+
// onIncoming paused the socket, we should pause the parser as well
1995+
debug('pause parser');
1996+
socket.parser.pause();
1997+
}
19921998
};
19931999

19942000
socket.onend = function() {
@@ -2017,9 +2023,35 @@ function connectionListener(socket) {
20172023
// The following callback is issued after the headers have been read on a
20182024
// new message. In this callback we setup the response object and pass it
20192025
// to the user.
2026+
2027+
socket._paused = false;
2028+
function socketOnDrain() {
2029+
// If we previously paused, then start reading again.
2030+
if (socket._paused) {
2031+
socket._paused = false;
2032+
socket.parser.resume();
2033+
readStart(socket);
2034+
}
2035+
}
2036+
socket.on('drain', socketOnDrain);
2037+
20202038
parser.onIncoming = function(req, shouldKeepAlive) {
20212039
incoming.push(req);
20222040

2041+
// If the writable end isn't consuming, then stop reading
2042+
// so that we don't become overwhelmed by a flood of
2043+
// pipelined requests that may never be resolved.
2044+
if (!socket._paused) {
2045+
var needPause = socket._writableState.needDrain;
2046+
if (needPause) {
2047+
socket._paused = true;
2048+
// We also need to pause the parser, but don't do that until after
2049+
// the call to execute, because we may still be processing the last
2050+
// chunk.
2051+
readStop(socket);
2052+
}
2053+
}
2054+
20232055
var res = new ServerResponse(req);
20242056

20252057
res.shouldKeepAlive = shouldKeepAlive;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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)
37+
bigResponse.fill('x');
38+
var gotTimeout = false;
39+
var childClosed = false;
40+
var requests = 0;
41+
var connections = 0;
42+
43+
var server = http.createServer(function(req, res) {
44+
requests++;
45+
res.setHeader('content-length', bigResponse.length);
46+
res.end(bigResponse);
47+
});
48+
49+
server.on('connection', function(conn) {
50+
connections++;
51+
});
52+
53+
// kill the connection after a bit, verifying that the
54+
// flood of requests was eventually halted.
55+
server.setTimeout(200, function(conn) {
56+
gotTimeout = true;
57+
conn.destroy();
58+
});
59+
60+
server.listen(common.PORT, function() {
61+
var spawn = require('child_process').spawn;
62+
var args = [__filename, 'child'];
63+
var child = spawn(process.execPath, args, { stdio: 'inherit' });
64+
child.on('close', function(code) {
65+
assert(!code);
66+
childClosed = true;
67+
server.close();
68+
});
69+
});
70+
71+
process.on('exit', function() {
72+
assert(gotTimeout);
73+
assert(childClosed);
74+
assert.equal(connections, 1);
75+
// 1213 works out to be the number of requests we end up processing
76+
// before the outgoing connection backs up and requires a drain.
77+
// however, to avoid being unnecessarily tied to a specific magic number,
78+
// and making the test brittle, just assert that it's "a lot", which we
79+
// can safely assume is more than 500.
80+
assert(requests >= 500);
81+
console.log('ok');
82+
});
83+
}
84+
85+
function child() {
86+
var net = require('net');
87+
88+
var gotEpipe = false;
89+
var conn = net.connect({ port: common.PORT });
90+
91+
var req = 'GET / HTTP/1.1\r\nHost: localhost:' +
92+
common.PORT + '\r\nAccept: */*\r\n\r\n';
93+
94+
req = new Array(10241).join(req);
95+
96+
conn.on('connect', function() {
97+
write();
98+
});
99+
100+
conn.on('drain', write);
101+
102+
conn.on('error', function(er) {
103+
gotEpipe = true;
104+
});
105+
106+
process.on('exit', function() {
107+
assert(gotEpipe);
108+
console.log('ok - child');
109+
});
110+
111+
function write() {
112+
while (false !== conn.write(req, 'ascii'));
113+
}
114+
}

0 commit comments

Comments
 (0)