From f05258ef3788441f373c8973621a07205b5d2c6e Mon Sep 17 00:00:00 2001 From: jonrobins Date: Tue, 8 Nov 2016 14:21:00 +0000 Subject: [PATCH] increment version: 0.5.7 - cleaned up listener count --- lib/LeaderElection.js | 22 ++++++++++++++-------- package.json | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/LeaderElection.js b/lib/LeaderElection.js index 0ec45b7..92dfcf8 100644 --- a/lib/LeaderElection.js +++ b/lib/LeaderElection.js @@ -33,24 +33,26 @@ util.inherits(LeaderElection, EventEmitter); function LeaderElection(client, path, id) { var self = this; - self.watcher = function(){ self._watch() }; + self.watcher = function(){ self._watch() }; + self.disconnect = function () { + self._disconnect(); + self.emit('error', new Error('disconnected from zk')); + }; self.client = client; self.path = path + '/' + id; self.znode = null; - self._isGroupLeader = false; - - self.client.on('disconnected', function () { - self._disconnect(); - self.emit('error', new Error('disconnected from zk')); - }); } LeaderElection.prototype.start = function (callback) { var self = this; self._isWithdrawn = false; + // Make sure we only store one disconnect listener at a time. + self.client.removeListener('disconnected', self.disconnect); + self.client.once('disconnected', self.disconnect); + if (self.znode) { this._watch(); return callback(new Error('already part of an election')); @@ -108,6 +110,7 @@ LeaderElection.prototype.withdraw = function (callback) { .remove(serviceInstancePath) .commit(function(err){ self.znode = null; + self.client.removeListener('disconnected', self.disconnect); callback(err); }); }; @@ -134,7 +137,10 @@ LeaderElection.prototype.destroy = function (callback) { }, function(){ self.client .getClient() - .remove(self.path, callback); + .remove(self.path, function(err){ + self.client.removeListener('disconnected', self.disconnect); + callback(err); + }); }); }) }; diff --git a/package.json b/package.json index 1985885..fff03da 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zoologist", - "version": "0.5.6", + "version": "0.5.7", "description": "A Curator-esque framework for ZooKeeper", "main": "index.js", "keywords": [