Skip to content

Commit 6a7fd14

Browse files
committed
Add flow control
1 parent ca1d12c commit 6a7fd14

File tree

1 file changed

+52
-10
lines changed

1 file changed

+52
-10
lines changed

lib/node-http-proxy/http-proxy.js

+52-10
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,22 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
224224
response.on('data', function (chunk) {
225225
if (req.method !== 'HEAD' && res.writable) {
226226
try {
227-
res.write(chunk);
227+
var flushed = res.write(chunk);
228228
}
229229
catch (ex) {
230230
console.error("res.write error: %s", ex.message);
231+
231232
try { res.end() }
232-
catch (ex) { console.error("res.write error: %s", ex.message) }
233+
catch (ex) { console.error("res.end error: %s", ex.message) }
234+
235+
return;
236+
}
237+
238+
if (!flushed) {
239+
response.pause();
240+
res.once('drain', function () {
241+
response.resume();
242+
});
233243
}
234244
}
235245
});
@@ -265,7 +275,13 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
265275
//
266276
req.on('data', function (chunk) {
267277
if (!errState) {
268-
reverseProxy.write(chunk);
278+
var flushed = reverseProxy.write(chunk);
279+
if (!flushed) {
280+
req.pause();
281+
reverseProxy.once('drain', function () {
282+
req.resume();
283+
});
284+
}
269285
}
270286
});
271287

@@ -334,8 +350,14 @@ HttpProxy.prototype._forwardRequest = function (req) {
334350
// the proxied request come in
335351
//
336352
req.on('data', function (chunk) {
337-
forwardProxy.write(chunk);
338-
})
353+
var flushed = forwardProxy.write(chunk);
354+
if (!flushed) {
355+
req.pause();
356+
forwardProxy.once('drain', function () {
357+
req.resume();
358+
});
359+
}
360+
});
339361

340362
//
341363
// At the end of the client request, we are going to
@@ -357,7 +379,7 @@ HttpProxy.prototype._forwardRequest = function (req) {
357379
//
358380
HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) {
359381
var self = this,
360-
outgoing = new(this.target.base);
382+
outgoing = new(this.target.base),
361383
listeners = {},
362384
errState = false,
363385
CRLF = '\r\n';
@@ -420,7 +442,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
420442
if (reverseProxy.incoming.socket.writable) {
421443
try {
422444
self.emit('websocket:outgoing', req, socket, head, data);
423-
reverseProxy.incoming.socket.write(data);
445+
var flushed = reverseProxy.incoming.socket.write(data);
446+
if (!flushed) {
447+
proxySocket.pause();
448+
reverseProxy.incoming.socket.once('drain', function () {
449+
proxySocket.resume();
450+
});
451+
}
424452
}
425453
catch (ex) {
426454
detach();
@@ -437,7 +465,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
437465
reverseProxy.incoming.socket.on('data', listeners.onOutgoing = function (data) {
438466
try {
439467
self.emit('websocket:incoming', reverseProxy, reverseProxy.incoming, head, data);
440-
proxySocket.write(data);
468+
var flushed = proxySocket.write(data);
469+
if (!flushed) {
470+
reverseProxy.incoming.socket.pause();
471+
proxySocket.once('drain', function () {
472+
reverseProxy.incoming.socket.resume();
473+
});
474+
}
441475
}
442476
catch (ex) {
443477
detach();
@@ -595,7 +629,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
595629
//
596630
self.emit('websocket:handshake', req, socket, head, sdata, data);
597631
socket.write(sdata);
598-
socket.write(data);
632+
var flushed = socket.write(data);
633+
if (!flushed) {
634+
reverseProxy.socket.pause();
635+
socket.once('drain', function () {
636+
reverseProxy.socket.resume();
637+
});
638+
}
599639
}
600640
catch (ex) {
601641
//
@@ -620,7 +660,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
620660

621661
try {
622662
//
623-
// Attempt to write the upgrade-head to the reverseProxy request.
663+
// Attempt to write the upgrade-head to the reverseProxy
664+
// request. This is small, and there's only ever one of
665+
// it; no need for pause/resume.
624666
//
625667
reverseProxy.write(head);
626668
}

0 commit comments

Comments
 (0)