Skip to content

Commit

Permalink
Remove unnecessary _serviceableNodeIds from PoolCluster
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Mar 22, 2015
1 parent f64dbad commit e48e45d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
36 changes: 15 additions & 21 deletions lib/PoolCluster.js
Expand Up @@ -15,15 +15,14 @@ function PoolCluster(config) {

config = config || {};
this._canRetry = typeof config.canRetry === 'undefined' ? true : config.canRetry;
this._removeNodeErrorCount = config.removeNodeErrorCount || 5;
this._defaultSelector = config.defaultSelector || 'RR';
this._removeNodeErrorCount = config.removeNodeErrorCount || 5;

this._closed = false;
this._findCaches = Object.create(null);
this._lastId = 0;
this._namespaces = Object.create(null);
this._nodes = Object.create(null);
this._serviceableNodeIds = [];
}

Util.inherits(PoolCluster, EventEmitter);
Expand Down Expand Up @@ -51,8 +50,6 @@ PoolCluster.prototype.add = function add(id, config) {
pool: new Pool({config: poolConfig})
};

this._serviceableNodeIds.push(nodeId);

this._clearFindCaches();
};

Expand Down Expand Up @@ -121,10 +118,8 @@ PoolCluster.prototype.remove = function remove(pattern) {

for (var i = 0; i < foundNodeIds.length; i++) {
var node = this._getNode(foundNodeIds[i]);
var index = this._serviceableNodeIds.indexOf(node.id);

if (index !== -1) {
this._serviceableNodeIds.splice(index, 1);
if (node) {
delete this._nodes[node.id];

this._clearFindCaches();
Expand Down Expand Up @@ -161,16 +156,19 @@ PoolCluster.prototype._findNodeIds = function(pattern) {
}

var foundNodeIds;
var nodeIds = Object.keys(this._nodes);

if (pattern === '*') { // all
foundNodeIds = this._serviceableNodeIds;
} else if (this._serviceableNodeIds.indexOf(pattern) != -1) { // one
if (pattern === '*') {
// all
foundNodeIds = nodeIds;
} else if (nodeIds.indexOf(pattern) != -1) {
// one
foundNodeIds = [pattern];
} else if (pattern[pattern.length - 1] === '*') {
// wild matching
// wild-card matching
var keyword = pattern.substring(pattern.length - 1, 0);

foundNodeIds = this._serviceableNodeIds.filter(function (id) {
foundNodeIds = nodeIds.filter(function (id) {
return id.indexOf(keyword) === 0;
});
} else {
Expand All @@ -186,19 +184,15 @@ PoolCluster.prototype._getNode = function(id) {
return this._nodes[id] || null;
};

PoolCluster.prototype._increaseErrorCount = function(node) {
PoolCluster.prototype._increaseErrorCount = function _increaseErrorCount(node) {
if (++node.errorCount >= this._removeNodeErrorCount) {
var index = this._serviceableNodeIds.indexOf(node.id);
if (index !== -1) {
this._serviceableNodeIds.splice(index, 1);
delete this._nodes[node.id];
delete this._nodes[node.id];

this._clearFindCaches();
this._clearFindCaches();

node.pool.end(_noop);
node.pool.end(_noop);

this.emit('remove', node.id);
}
this.emit('remove', node.id);
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/unit/pool-cluster/test-internals.js
Expand Up @@ -13,7 +13,7 @@ server.listen(common.fakeServerPort, function(err) {
assert.ifError(err);

// added nodes
assert.deepEqual(cluster._serviceableNodeIds, ['CLUSTER::1', 'MASTER', 'SLAVE1', 'SLAVE2']);
assert.deepEqual(Object.keys(cluster._nodes), ['CLUSTER::1', 'MASTER', 'SLAVE1', 'SLAVE2']);

// _findNodeIds
assert.deepEqual(cluster._findNodeIds('MASTER'), ['MASTER']);
Expand Down

0 comments on commit e48e45d

Please sign in to comment.