Skip to content

Commit 2d5050d

Browse files
uPaymeiFixitwellwelwel
andauthoredMar 11, 2025··
feat: add RegExp support to PoolCluster (#3451)
* add regex support to PoolCluster * Update lib/pool_cluster.js Co-authored-by: Weslley Araújo <46850407+wellwelwel@users.noreply.github.com> * fix linting issues * chore: add tests for regex pattern --------- Co-authored-by: Weslley Araújo <46850407+wellwelwel@users.noreply.github.com>
1 parent 9d6b474 commit 2d5050d

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed
 

‎lib/pool_cluster.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ const getMonotonicMilliseconds = function () {
3737
return Math.floor(ms);
3838
};
3939

40+
const patternRegExp = function (pattern) {
41+
if (pattern instanceof RegExp) {
42+
return pattern;
43+
}
44+
45+
const source = pattern
46+
.replace(/([.+?^=!:${}()|[\]/\\])/g, '\\$1')
47+
.replace(/\*/g, '.*');
48+
49+
return new RegExp(`^${source}$`);
50+
};
51+
4052
class PoolNamespace {
4153
constructor(cluster, pattern, selector) {
4254
this._cluster = cluster;
@@ -256,20 +268,12 @@ class PoolCluster extends EventEmitter {
256268
let currentTime = 0;
257269
let foundNodeIds = this._findCaches[pattern];
258270

259-
if (typeof this._findCaches[pattern] === 'undefined') {
260-
if (pattern === '*') {
261-
// all
262-
foundNodeIds = this._serviceableNodeIds;
263-
} else if (this._serviceableNodeIds.indexOf(pattern) !== -1) {
264-
// one
265-
foundNodeIds = [pattern];
266-
} else {
267-
// wild matching
268-
const keyword = pattern.substring(pattern.length - 1, 0);
269-
foundNodeIds = this._serviceableNodeIds.filter((id) =>
270-
id.startsWith(keyword)
271-
);
272-
}
271+
if (foundNodeIds === undefined) {
272+
const expression = patternRegExp(pattern);
273+
274+
foundNodeIds = this._serviceableNodeIds.filter((id) =>
275+
id.match(expression)
276+
);
273277
}
274278

275279
this._findCaches[pattern] = foundNodeIds;

‎test/esm/integration/pool-cluster/test-promise-wrapper.test.mjs

+36
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,40 @@ const { createPoolCluster } = require('../../../../promise.js');
110110

111111
poolCluster.end();
112112
});
113+
114+
await test(async () => {
115+
const poolCluster = createPoolCluster();
116+
poolCluster.add('SLAVE', common.config);
117+
118+
try {
119+
await poolCluster.getConnection('SLAVE1');
120+
assert.fail('An error was expected');
121+
} catch (error) {
122+
assert.equal(
123+
error.code,
124+
'POOL_NOEXIST',
125+
'should throw when PoolNamespace does not exist'
126+
);
127+
} finally {
128+
poolCluster.end();
129+
}
130+
});
131+
132+
await test(async () => {
133+
const poolCluster = createPoolCluster();
134+
poolCluster.add('SLAVE1', common.config);
135+
136+
try {
137+
const connection = await poolCluster.getConnection(/SLAVE[12]/);
138+
assert.equal(
139+
connection.connection._clusterId,
140+
'SLAVE1',
141+
'should match regex pattern'
142+
);
143+
} catch (error) {
144+
assert.fail('should not throw');
145+
} finally {
146+
poolCluster.end();
147+
}
148+
});
113149
})();

0 commit comments

Comments
 (0)
Please sign in to comment.