Skip to content

Commit

Permalink
Fixed edge case when health check comes in post-close()
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed Oct 15, 2015
1 parent 0ff75c0 commit dde2dc8
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions lib/TileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
*
Expand All @@ -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();
Expand Down

0 comments on commit dde2dc8

Please sign in to comment.