Skip to content

Commit e5f9fe4

Browse files
committed
Fix poolCluster.add to throw if PoolCluster has been closed
1 parent 540e4f2 commit e5f9fe4

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

Changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ you spot any mistakes.
88

99
* Add `poolCluster.remove` to remove pools from the cluster #1006 #1007
1010
* Add optional callback to `poolCluster.end`
11+
* Fix `poolCluster.add` to throw if `PoolCluster` has been closed
1112
* Fix un-catchable error from `PoolCluster` when MySQL server offline #1033
1213
* Improve speed formatting SQL #1019
1314
* Support io.js

lib/PoolCluster.js

+23-19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ function PoolCluster(config) {
2828

2929
Util.inherits(PoolCluster, EventEmitter);
3030

31+
PoolCluster.prototype.add = function add(id, config) {
32+
if (this._closed) {
33+
throw new Error('PoolCluster is closed.');
34+
}
35+
36+
if (typeof id === 'object') {
37+
config = id;
38+
id = 'CLUSTER::' + (++this._lastId);
39+
}
40+
41+
if (typeof this._nodes[id] === 'undefined') {
42+
this._nodes[id] = {
43+
id: id,
44+
errorCount: 0,
45+
pool: new Pool({config: new PoolConfig(config)})
46+
};
47+
48+
this._serviceableNodeIds.push(id);
49+
50+
this._clearFindCaches();
51+
}
52+
};
53+
3154
PoolCluster.prototype.end = function end(callback) {
3255
var cb = callback !== undefined
3356
? callback
@@ -88,25 +111,6 @@ PoolCluster.prototype.of = function(pattern, selector) {
88111
return this._namespaces[key];
89112
};
90113

91-
PoolCluster.prototype.add = function(id, config) {
92-
if (typeof id === 'object') {
93-
config = id;
94-
id = 'CLUSTER::' + (++this._lastId);
95-
}
96-
97-
if (typeof this._nodes[id] === 'undefined') {
98-
this._nodes[id] = {
99-
id: id,
100-
errorCount: 0,
101-
pool: new Pool({config: new PoolConfig(config)})
102-
};
103-
104-
this._serviceableNodeIds.push(id);
105-
106-
this._clearFindCaches();
107-
}
108-
};
109-
110114
PoolCluster.prototype.remove = function(pattern) {
111115
var foundNodeIds = this._findNodeIds(pattern);
112116

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var cluster = common.createPoolCluster();
4+
var poolConfig = common.getTestConfig();
5+
6+
assert.doesNotThrow(cluster.add.bind(cluster, 'SLAVE1', poolConfig));
7+
8+
cluster.end(function (err) {
9+
assert.ifError(err);
10+
11+
assert.throws(cluster.add.bind(cluster, 'SLAVE3', poolConfig), /PoolCluster is closed/);
12+
});
13+
14+
assert.throws(cluster.add.bind(cluster, 'SLAVE2', poolConfig), /PoolCluster is closed/);

0 commit comments

Comments
 (0)