From dde2dc8f9c8da32da2c7601f26964650007d5a65 Mon Sep 17 00:00:00 2001 From: Brian Reavis Date: Thu, 15 Oct 2015 09:36:41 -0600 Subject: [PATCH] Fixed edge case when health check comes in post-close() --- lib/TileServer.js | 58 ++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/lib/TileServer.js b/lib/TileServer.js index 13d9a0b..329d5ee 100755 --- a/lib/TileServer.js +++ b/lib/TileServer.js @@ -473,6 +473,10 @@ TileServer.prototype.bindBalancer = function() { * @return {void} */ TileServer.prototype.handleBalancerPing = function() { + // in very rare cases a valid balancer health check might come + // in after the server has exited the pool via strata.close() + if (!this.balancer) return; + var self = this; clearTimeout(this.balancer_timeout); this.balancer_timeout = setTimeout(function() { @@ -517,6 +521,38 @@ TileServer.prototype.listen = function(port) { return server; }; +/** + * Exits the load balancing pool. See https://github.com/naturalatlas/tilestrata-balancer + * + * @param {function} [callback] + * @return {void} + */ +TileServer.prototype.closeBalancer = function(callback) { + callback = callback || function() {}; + + if (!this.balancer) return callback(); + clearTimeout(this.balancer_timeout); + this.balancer.destroy(); + this.balancer = null; + + log.info('balancer', 'Server close() called, removing self from balancer...'); + request({ + method: 'DELETE', + url: 'http://' + this.options.balancer.host + '/nodes/' + this.uuid + }, function(err, res, body) { + if (err) { + log.warn('balancer', 'Unregistration failed (' + err.message + ')'); + } else { + if (res.statusCode === 200) { + log.info('balancer', 'Successfully removed from the balancer'); + } else { + log.warn('balancer', 'Received HTTP ' + res.statusCode + ' ' + HTTPStatus[res.statusCode] + ' when removing node'); + } + } + callback(); + }); +}; + /** * Stops listening and disconnects from the balancer (if needed) * @@ -533,27 +569,7 @@ TileServer.prototype.close = function(callback) { async.series([ function closeBalancer(callback) { - if (!self.balancer) return callback(); - clearTimeout(self.balancer_timeout); - self.balancer.destroy(); - self.balancer = null; - - log.info('balancer', 'Server close() called, removing self from balancer...'); - request({ - method: 'DELETE', - url: 'http://' + self.options.balancer.host + '/nodes/' + self.uuid - }, function(err, res, body) { - if (err) { - log.warn('balancer', 'Unregistration failed (' + err.message + ')'); - } else { - if (res.statusCode === 200) { - log.info('balancer', 'Successfully removed from the balancer'); - } else { - log.warn('balancer', 'Received HTTP ' + res.statusCode + ' ' + HTTPStatus[res.statusCode] + ' when removing node'); - } - } - callback(); - }); + self.closeBalancer(callback); }, function closePort(callback) { if (!self.http_server) return callback();