Skip to content

Commit bd70e05

Browse files
committed
Return last error when PoolCluster exhausts connection retries
fixes #818
1 parent f03bf56 commit bd70e05

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

Changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ you spot any mistakes.
1111
* Clone connection config for new pool connections
1212
* Default `connectTimeout` to 2 minutes
1313
* Reject unauthorized SSL connections (use `ssl.rejectUnauthorized` to override) #816
14+
* Return last error when PoolCluster exhausts connection retries #818
1415
* Throw on unknown SSL profile name #817
1516
* User newer TLS functions when available #809
1617

lib/PoolCluster.js

+29-19
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,8 @@ PoolCluster.prototype._getConnection = function(node, cb) {
151151
node.pool.getConnection(function (err, connection) {
152152
if (err) {
153153
self._increaseErrorCount(node);
154-
155-
if (self._canRetry) {
156-
return cb(null, 'retry');
157-
} else {
158-
return cb(err);
159-
}
154+
cb(err);
155+
return;
160156
} else {
161157
self._decreaseErrorCount(node);
162158
}
@@ -182,34 +178,48 @@ function PoolNamespace(cluster, pattern, selector) {
182178

183179
PoolNamespace.prototype.getConnection = function(cb) {
184180
var clusterNode = this._getClusterNode();
181+
var cluster = this._cluster;
182+
var namespace = this;
185183

186184
if (clusterNode === null) {
187185
return cb(new Error('Pool does not exist.'));
188186
}
189187

190-
this._cluster._getConnection(clusterNode, function(err, connection) {
191-
if (err) {
192-
return cb(err);
188+
cluster._getConnection(clusterNode, function(err, connection) {
189+
var retry = err && cluster._canRetry
190+
&& cluster._findNodeIds(namespace._pattern).length !== 0;
191+
192+
if (retry) {
193+
return namespace.getConnection(cb);
193194
}
194195

195-
if (connection === 'retry') {
196-
return this.getConnection(cb);
196+
if (err) {
197+
return cb(err);
197198
}
198199

199200
cb(null, connection);
200-
}.bind(this));
201+
});
201202
};
202203

203-
PoolNamespace.prototype._getClusterNode = function() {
204+
PoolNamespace.prototype._getClusterNode = function _getClusterNode() {
204205
var foundNodeIds = this._cluster._findNodeIds(this._pattern);
205-
206-
if (foundNodeIds.length === 0) {
207-
return null;
206+
var nodeId;
207+
208+
switch (foundNodeIds.length) {
209+
case 0:
210+
nodeId = null;
211+
break;
212+
case 1:
213+
nodeId = foundNodeIds[0];
214+
break;
215+
default:
216+
nodeId = this._selector(foundNodeIds);
217+
break;
208218
}
209219

210-
var nodeId = (foundNodeIds.length === 1) ? foundNodeIds[0] : this._selector(foundNodeIds);
211-
212-
return this._cluster._getNode(nodeId);
220+
return nodeId !== null
221+
? this._cluster._getNode(nodeId)
222+
: null;
213223
};
214224

215225
/**

test/integration/pool/test-cluster.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function createPoolCluster(clusterConfig, poolConfig) {
221221
cluster.of('*', 'RR').getConnection(function (err, connection) {
222222
cluster.end();
223223

224-
assert.ok(err === null);
224+
assert.ifError(err);
225225
assert.equal(connection._clusterId, 'CORRECT');
226226

227227
assert.equal(removedNodeId, 'ERROR');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var cluster = common.createPoolCluster({
4+
canRetry : true,
5+
removeNodeErrorCount : 5
6+
});
7+
var server = common.createFakeServer();
8+
9+
var connCount = 0;
10+
var poolConfig = common.getTestConfig({port: common.fakeServerPort});
11+
cluster.add('MASTER', poolConfig);
12+
13+
server.listen(common.fakeServerPort, function(err) {
14+
assert.ifError(err);
15+
16+
cluster.getConnection('MASTER', function(err, conn){
17+
assert.ok(err);
18+
assert.equal(err.code, 'ER_HOST_NOT_PRIVILEGED');
19+
assert.equal(err.fatal, true);
20+
assert.equal(connCount, 5);
21+
server.destroy();
22+
});
23+
});
24+
25+
server.on('connection', function(incomingConnection) {
26+
var errno = 1130; // ER_HOST_NOT_PRIVILEGED
27+
connCount += 1;
28+
incomingConnection.deny('You suck.', errno);
29+
});

0 commit comments

Comments
 (0)