Skip to content

Commit 73d92f7

Browse files
committed
Accept wildcard anywhere in pool cluster pattern
closes #1570
1 parent e35523f commit 73d92f7

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

Changes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ you spot any mistakes.
66

77
## HEAD
88

9-
* Accept regular expression as pool cluster pattern #1570 #1572
9+
* Accept regular expression as pool cluster pattern #1572
10+
* Accept wildcard anywhere in pool cluster pattern #1570
1011
* Add new error codes up to MySQL 5.7.17
1112
* Fix memory leak when using long-running domains #1619 #1620
1213
* Update `bignumber.js` to 3.1.2

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ poolCluster.on('remove', function (nodeId) {
468468
console.log('REMOVED NODE : ' + nodeId); // nodeId = SLAVE1
469469
});
470470

471-
// A pattern can be passed with * as wildcard at end of string
471+
// A pattern can be passed with * as wildcard
472472
poolCluster.getConnection('SLAVE*', 'ORDER', function (err, connection) {});
473473

474474
// The pattern can also be a regular expression

lib/PoolCluster.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,12 @@ PoolCluster.prototype._findNodeIds = function _findNodeIds(pattern, includeOffli
173173
var foundNodeIds = this._findCaches[pattern];
174174

175175
if (foundNodeIds === undefined) {
176-
var nodeIds = Object.keys(this._nodes);
176+
var expression = patternRegExp(pattern);
177+
var nodeIds = Object.keys(this._nodes);
177178

178-
if (isRegExp(pattern)) {
179-
foundNodeIds = nodeIds.filter(function (id) { return id.match(pattern); });
180-
} else {
181-
var wildcard = pattern.substr(-1) === '*';
182-
var keyword = wildcard
183-
? pattern.substr(0, pattern.length - 1)
184-
: pattern;
185-
186-
if (wildcard) {
187-
foundNodeIds = keyword.length !== 0
188-
? nodeIds.filter(function (id) { return id.substr(0, keyword.length) === keyword; })
189-
: nodeIds;
190-
} else {
191-
var index = nodeIds.indexOf(keyword);
192-
foundNodeIds = nodeIds.slice(index, index + 1);
193-
}
194-
}
179+
foundNodeIds = nodeIds.filter(function (id) {
180+
return id.match(expression);
181+
});
195182

196183
this._findCaches[pattern] = foundNodeIds;
197184
}
@@ -280,6 +267,18 @@ function isRegExp(val) {
280267
&& Object.prototype.toString.call(val) === '[object RegExp]';
281268
}
282269

270+
function patternRegExp(pattern) {
271+
if (isRegExp(pattern)) {
272+
return pattern;
273+
}
274+
275+
var source = pattern
276+
.replace(/([.+?^=!:${}()|\[\]\/\\])/g, '\\$1')
277+
.replace(/\*/g, '.*');
278+
279+
return new RegExp('^' + source + '$');
280+
}
281+
283282
function _cb(err) {
284283
if (err) {
285284
throw err;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ server.listen(common.fakeServerPort, function(err) {
1717

1818
// _findNodeIds
1919
assert.deepEqual(cluster._findNodeIds('MASTER'), ['MASTER']);
20+
assert.deepEqual(cluster._findNodeIds('MA*ER'), ['MASTER']);
21+
assert.deepEqual(cluster._findNodeIds('*TER*'), ['CLUSTER::1', 'MASTER']);
2022
assert.deepEqual(cluster._findNodeIds('SLAVE*'), ['SLAVE1', 'SLAVE2']);
2123
assert.deepEqual(cluster._findNodeIds(/slave[1-2]/i), ['SLAVE1', 'SLAVE2']);
2224

0 commit comments

Comments
 (0)