Skip to content

Commit e48e45d

Browse files
committed
Remove unnecessary _serviceableNodeIds from PoolCluster
1 parent f64dbad commit e48e45d

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

lib/PoolCluster.js

+15-21
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ function PoolCluster(config) {
1515

1616
config = config || {};
1717
this._canRetry = typeof config.canRetry === 'undefined' ? true : config.canRetry;
18-
this._removeNodeErrorCount = config.removeNodeErrorCount || 5;
1918
this._defaultSelector = config.defaultSelector || 'RR';
19+
this._removeNodeErrorCount = config.removeNodeErrorCount || 5;
2020

2121
this._closed = false;
2222
this._findCaches = Object.create(null);
2323
this._lastId = 0;
2424
this._namespaces = Object.create(null);
2525
this._nodes = Object.create(null);
26-
this._serviceableNodeIds = [];
2726
}
2827

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

54-
this._serviceableNodeIds.push(nodeId);
55-
5653
this._clearFindCaches();
5754
};
5855

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

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

126-
if (index !== -1) {
127-
this._serviceableNodeIds.splice(index, 1);
122+
if (node) {
128123
delete this._nodes[node.id];
129124

130125
this._clearFindCaches();
@@ -161,16 +156,19 @@ PoolCluster.prototype._findNodeIds = function(pattern) {
161156
}
162157

163158
var foundNodeIds;
159+
var nodeIds = Object.keys(this._nodes);
164160

165-
if (pattern === '*') { // all
166-
foundNodeIds = this._serviceableNodeIds;
167-
} else if (this._serviceableNodeIds.indexOf(pattern) != -1) { // one
161+
if (pattern === '*') {
162+
// all
163+
foundNodeIds = nodeIds;
164+
} else if (nodeIds.indexOf(pattern) != -1) {
165+
// one
168166
foundNodeIds = [pattern];
169167
} else if (pattern[pattern.length - 1] === '*') {
170-
// wild matching
168+
// wild-card matching
171169
var keyword = pattern.substring(pattern.length - 1, 0);
172170

173-
foundNodeIds = this._serviceableNodeIds.filter(function (id) {
171+
foundNodeIds = nodeIds.filter(function (id) {
174172
return id.indexOf(keyword) === 0;
175173
});
176174
} else {
@@ -186,19 +184,15 @@ PoolCluster.prototype._getNode = function(id) {
186184
return this._nodes[id] || null;
187185
};
188186

189-
PoolCluster.prototype._increaseErrorCount = function(node) {
187+
PoolCluster.prototype._increaseErrorCount = function _increaseErrorCount(node) {
190188
if (++node.errorCount >= this._removeNodeErrorCount) {
191-
var index = this._serviceableNodeIds.indexOf(node.id);
192-
if (index !== -1) {
193-
this._serviceableNodeIds.splice(index, 1);
194-
delete this._nodes[node.id];
189+
delete this._nodes[node.id];
195190

196-
this._clearFindCaches();
191+
this._clearFindCaches();
197192

198-
node.pool.end(_noop);
193+
node.pool.end(_noop);
199194

200-
this.emit('remove', node.id);
201-
}
195+
this.emit('remove', node.id);
202196
}
203197
};
204198

test/unit/pool-cluster/test-internals.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ server.listen(common.fakeServerPort, function(err) {
1313
assert.ifError(err);
1414

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

1818
// _findNodeIds
1919
assert.deepEqual(cluster._findNodeIds('MASTER'), ['MASTER']);

0 commit comments

Comments
 (0)