Commit ca1d12cf authored by indexzero's avatar indexzero

[fix] Memory leak hunting.

parent e1c41d06
...@@ -174,6 +174,14 @@ exports.buffer = function (obj) { ...@@ -174,6 +174,14 @@ exports.buffer = function (obj) {
obj.removeListener('data', onData); obj.removeListener('data', onData);
obj.removeListener('end', onEnd); obj.removeListener('end', onEnd);
}, },
destroy: function () {
this.end();
this.resume = function () {
console.error("Cannot resume buffer after destroying it.");
};
onData = onEnd = events = obj = null;
},
resume: function () { resume: function () {
this.end(); this.end();
for (var i = 0, len = events.length; i < len; ++i) { for (var i = 0, len = events.length; i < len; ++i) {
......
...@@ -279,9 +279,13 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) { ...@@ -279,9 +279,13 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
} }
}); });
//
// If we have been passed buffered data, resume it. // If we have been passed buffered data, resume it.
if (buffer && !errState) { //
buffer.resume(); if (buffer) {
return !errState
? buffer.resume()
: buffer.destroy();
} }
}; };
...@@ -419,6 +423,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) ...@@ -419,6 +423,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
reverseProxy.incoming.socket.write(data); reverseProxy.incoming.socket.write(data);
} }
catch (ex) { catch (ex) {
detach();
reverseProxy.incoming.socket.end(); reverseProxy.incoming.socket.end();
proxySocket.end(); proxySocket.end();
} }
...@@ -429,12 +434,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) ...@@ -429,12 +434,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// Any outgoing data on this Websocket from the proxy target // Any outgoing data on this Websocket from the proxy target
// will be written to the `proxySocket` socket. // will be written to the `proxySocket` socket.
// //
reverseProxy.incoming.socket.on('data', listeners.onOutgoing = function(data) { reverseProxy.incoming.socket.on('data', listeners.onOutgoing = function (data) {
try { try {
self.emit('websocket:incoming', reverseProxy, reverseProxy.incoming, head, data); self.emit('websocket:incoming', reverseProxy, reverseProxy.incoming, head, data);
proxySocket.write(data); proxySocket.write(data);
} }
catch (ex) { catch (ex) {
detach();
proxySocket.end(); proxySocket.end();
socket.end(); socket.end();
} }
...@@ -625,7 +631,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer) ...@@ -625,7 +631,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// //
// If we have been passed buffered data, resume it. // If we have been passed buffered data, resume it.
// //
if (buffer && !errState) { if (buffer) {
buffer.resume(); return !errState
? buffer.resume()
: buffer.destroy();
} }
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment