fix eventemitter madness, be careful around pausing here too #206

Closed
wants to merge 2 commits into
from
View
33 lib/node-http-proxy/http-proxy.js
@@ -259,7 +259,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// If the res socket has been killed already, then write()
// will throw. Nevertheless, try our best to end it nicely.
//
- var paused = false;
+ var paused_resp = false;
response.on('data', function (chunk) {
if (req.method !== 'HEAD' && res.writable) {
try {
@@ -274,11 +274,11 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
return;
}
- if (!flushed && !paused) {
- paused = true;
+ if (!flushed && !paused_resp) {
+ paused_resp = true;
response.pause();
res.once('drain', function () {
- paused = false;
+ paused_resp = false;
try { response.resume() }
catch (er) { console.error("response.resume error: %s", er.message) }
});
@@ -335,13 +335,16 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// For each data `chunk` received from the incoming
// `req` write it to the `reverseProxy` request.
//
+ var paused_rr = false;
req.on('data', function (chunk) {
if (!errState) {
var flushed = reverseProxy.write(chunk);
- if (!flushed) {
+ if (!flushed && !paused_rr) {
+ paused_rr = true;
req.pause();
reverseProxy.once('drain', function () {
+ paused_rr = false;
try { req.resume() }
catch (er) { console.error("req.resume error: %s", er.message) }
});
@@ -489,14 +492,17 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// Any incoming data on this WebSocket to the proxy target
// will be written to the `reverseProxy` socket.
//
+ var paused_ps = false;
proxySocket.on('data', listeners.onIncoming = function (data) {
if (reverseProxy.incoming.socket.writable) {
try {
self.emit('websocket:outgoing', req, socket, head, data);
var flushed = reverseProxy.incoming.socket.write(data);
- if (!flushed) {
+ if (!flushed && !paused_ps) {
+ paused_ps = true;
proxySocket.pause();
reverseProxy.incoming.socket.once('drain', function () {
+ paused_ps = false;
try { proxySocket.resume() }
catch (er) { console.error("proxySocket.resume error: %s", er.message) }
});
@@ -522,13 +528,16 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// Any outgoing data on this Websocket from the proxy target
// will be written to the `proxySocket` socket.
//
+ var paused_rpis = false;
reverseProxy.incoming.socket.on('data', listeners.onOutgoing = function (data) {
try {
self.emit('websocket:incoming', reverseProxy, reverseProxy.incoming, head, data);
var flushed = proxySocket.write(data);
- if (!flushed) {
+ if (!flushed && !paused_rpis) {
+ paused_rpis = true;
reverseProxy.incoming.socket.pause();
proxySocket.once('drain', function () {
+ paused_rpis = false;
try { reverseProxy.incoming.socket.resume() }
catch (er) { console.error("reverseProxy.incoming.socket.resume error: %s", er.message) }
});
@@ -666,6 +675,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// then execute the WebSocket handshake.
//
reverseProxy.once('socket', function (revSocket) {
+ var paused_rs = false;
revSocket.on('data', function handshake (data) {
//
// Ok, kind of harmfull part of code. Socket.IO sends a hash
@@ -698,9 +708,11 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
self.emit('websocket:handshake', req, socket, head, sdata, data);
socket.write(sdata);
var flushed = socket.write(data);
- if (!flushed) {
+ if (!flushed && !paused_rs) {
+ paused_rs = true;
revSocket.pause();
socket.once('drain', function () {
+ paused_rs = false;
try { revSocket.resume() }
catch (er) { console.error("reverseProxy.socket.resume error: %s", er.message) }
});
@@ -823,11 +835,14 @@ HttpProxy.prototype._forwardRequest = function (req) {
// Chunk the client request body as chunks from
// the proxied request come in
//
+ var paused_req = false;
req.on('data', function (chunk) {
var flushed = forwardProxy.write(chunk);
- if (!flushed) {
+ if (!flushed && !paused_req) {
+ paused_req = true;
req.pause();
forwardProxy.once('drain', function () {
+ paused_req = false;
try { req.resume() }
catch (er) { console.error("req.resume error: %s", er.message) }
});