Skip to content

Commit adaa340

Browse files
mlshvdvseangarner
authored andcommitted
Add poolCluster.remove to remove pools from the cluster
closes mysqljs#1006 closes mysqljs#1007
1 parent b8176fc commit adaa340

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

Changes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This file is a manually maintained list of changes for each release. Feel free
44
to add your changes here when sending pull requests. Also send corrections if
55
you spot any mistakes.
66

7+
## HEAD
8+
9+
* Add `poolCluster.remove` to remove pools from the cluster #1006 #1007
10+
711
## v2.5.5 (2015-02-23)
812

913
* Store SSL presets in JS instead of JSON #959

Readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,16 @@ PoolCluster provides multiple hosts connection. (group & retry & selector)
438438
// create
439439
var poolCluster = mysql.createPoolCluster();
440440

441+
// add configurations
441442
poolCluster.add(config); // anonymous group
442443
poolCluster.add('MASTER', masterConfig);
443444
poolCluster.add('SLAVE1', slave1Config);
444445
poolCluster.add('SLAVE2', slave2Config);
445446

447+
// remove configurations
448+
poolCluster.remove('SLAVE2'); // By nodeId
449+
poolCluster.remove('SLAVE*'); // By target group : SLAVE1-2
450+
446451
// Target Group : ALL(anonymous, MASTER, SLAVE1-2), Selector : round-robin(default)
447452
poolCluster.getConnection(function (err, connection) {});
448453

lib/PoolCluster.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ PoolCluster.prototype.add = function(id, config) {
6363
}
6464
};
6565

66+
PoolCluster.prototype.remove = function(pattern) {
67+
var foundNodeIds = this._findNodeIds(pattern);
68+
69+
for (var i = 0; i < foundNodeIds.length; i++) {
70+
var node = this._getNode(foundNodeIds[i]);
71+
var index = this._serviceableNodeIds.indexOf(node.id);
72+
if (index !== -1) {
73+
this._serviceableNodeIds.splice(index, 1);
74+
delete this._nodes[node.id];
75+
76+
this._clearFindCaches();
77+
78+
node.pool.end();
79+
}
80+
}
81+
};
82+
6683
PoolCluster.prototype.getConnection = function(pattern, selector, cb) {
6784
var namespace;
6885
if (typeof pattern === 'function') {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var cluster = common.createPoolCluster();
4+
var server = common.createFakeServer();
5+
6+
var poolConfig = common.getTestConfig({port: common.fakeServerPort});
7+
cluster.add('SLAVE1', poolConfig);
8+
cluster.add('SLAVE2', poolConfig);
9+
10+
server.listen(common.fakeServerPort, function (err) {
11+
assert.ifError(err);
12+
13+
var pool = cluster.of('SLAVE*', 'ORDER');
14+
15+
pool.getConnection(function (err, connection) {
16+
assert.ifError(err);
17+
assert.strictEqual(connection._clusterId, 'SLAVE1');
18+
19+
connection.release();
20+
cluster.remove('SLAVE1');
21+
22+
pool.getConnection(function (err, connection) {
23+
assert.ifError(err);
24+
assert.strictEqual(connection._clusterId, 'SLAVE2');
25+
26+
connection.release();
27+
cluster.remove('SLAVE2');
28+
29+
pool.getConnection(function (err, connection) {
30+
assert.ok(err);
31+
assert.equal(err.code, 'POOL_NOEXIST');
32+
33+
cluster.remove('SLAVE1');
34+
cluster.remove('SLAVE2');
35+
36+
server._server.close();
37+
});
38+
});
39+
});
40+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var cluster = common.createPoolCluster();
4+
var server = common.createFakeServer();
5+
6+
var poolConfig = common.getTestConfig({port: common.fakeServerPort});
7+
cluster.add('SLAVE1', poolConfig);
8+
cluster.add('SLAVE2', poolConfig);
9+
10+
server.listen(common.fakeServerPort, function (err) {
11+
assert.ifError(err);
12+
13+
var pool = cluster.of('SLAVE*', 'ORDER');
14+
15+
pool.getConnection(function (err, connection) {
16+
assert.ifError(err);
17+
assert.strictEqual(connection._clusterId, 'SLAVE1');
18+
19+
connection.release();
20+
cluster.remove('SLAVE*');
21+
22+
pool.getConnection(function (err, connection) {
23+
assert.ok(err);
24+
assert.equal(err.code, 'POOL_NOEXIST');
25+
26+
cluster.remove('SLAVE*');
27+
28+
server._server.close();
29+
});
30+
});
31+
});

0 commit comments

Comments
 (0)