From 726b4f3661e685dec9459547f165d3ee56231fda Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel <lloyd@hilaiel.com> Date: Wed, 20 Jun 2012 11:05:58 +0300 Subject: [PATCH] for __heartbeat__ use an application level global timeout to catch the situation where the other end is not reachable and ensure total wait time is <= 5s (in this case, no 'connect' event is ever emitted) --- lib/heartbeat.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/heartbeat.js b/lib/heartbeat.js index 027fc9661..209bc9ed3 100644 --- a/lib/heartbeat.js +++ b/lib/heartbeat.js @@ -92,11 +92,16 @@ var check = exports.check = function(url, cb) { var shortname = url.host + ':' + url.port; + var timeoutHandle = setTimeout(function() { + req.abort(); + }, checkTimeout); + var req = require(url.scheme).get({ host: url.host, port: url.port, path: exports.path }, function (res) { + clearTimeout(timeoutHandle); if (res.statusCode === 200) cb(true); else { logger.error("non-200 response from " + shortname + ". fatal! (" + res.statusCode + ")"); @@ -104,14 +109,8 @@ var check = exports.check = function(url, cb) { } }); req.on('error', function (e) { + clearTimeout(timeoutHandle); logger.error("can't communicate with " + shortname + ". fatal: " + e); cb(false, e); }); - req.on('connect', function (socket) { - socket.setTimeout(checkTimeout); - socket.on('timeout', function() { - req.abort(); - cb(false, "timeout"); - }); - }); }; -- GitLab