From 4a09dc114a5459fed34fc2b1bbeb341186b0a5d3 Mon Sep 17 00:00:00 2001 From: Roman Shtylman Date: Sat, 25 Oct 2014 16:59:16 -0700 Subject: [PATCH] better handling of requests that die --- proxy.js | 6 ++++++ server.js | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/proxy.js b/proxy.js index 018b49e..6c9cdff 100644 --- a/proxy.js +++ b/proxy.js @@ -139,8 +139,14 @@ Proxy.prototype.next_socket = function(cb) { return self.waiting.push(cb); } + var done_called = false; // put the socket back function done() { + if (done_called) { + throw new Error('done called multiple times'); + } + + done_called = true; if (!sock.destroyed) { debug('retuning socket'); self.sockets.push(sock); diff --git a/server.js b/server.js index 29bc22a..50270e5 100644 --- a/server.js +++ b/server.js @@ -49,6 +49,8 @@ function maybe_bounce(req, res, bounce) { return true; } + // flag if we already finished before we get a socket + // we can't respond to these requests var finished = false; on_finished(res, function(err) { finished = true; @@ -63,6 +65,7 @@ function maybe_bounce(req, res, bounce) { if (finished) { return done(); } + // happens when client upstream is disconnected // we gracefully inform the user and kill their conn // without this, the browser will leave some connections open @@ -79,12 +82,22 @@ function maybe_bounce(req, res, bounce) { stream.on('error', function(err) { socket.destroy(); + req.connection.destroy(); + done(); }); // return the socket to the client pool stream.once('end', function() { done(); }); + + on_finished(res, function(err) { + if (err) { + req.connection.destroy(); + socket.destroy(); + done(); + } + }); }); return true;