Permalink
Browse files

Merge pull request racker#31 from robert-chiniquy/fix-connection-clos…

…e-race-condition

Fix connection close race condition
  • Loading branch information...
gdusbabek committed May 31, 2012
2 parents c80c2e1 + 5b6ff40 commit 7eedb988f6f9d29a299fe33de2f5bc70c518dc52
Showing with 37 additions and 5 deletions.
  1. +22 −3 lib/driver.js
  2. +15 −2 test/test_driver.js
View
@@ -493,14 +493,30 @@ ConnectionInPool.prototype.connect = function(callback) {
}
Connection.call(this, this._options);
Connection.prototype.connect.call(this, function(err) {
self.connected = !err;
if (err) {
self.connected = false;
}
else {
self.setConnected();
}
self.unhealthyAt = err ? new Date().getTime() : 0;
callback(err);
});
};
ConnectionInPool.prototype.setConnected = function() {
this.connected = true;
this.emit('connected');
};
ConnectionInPool.prototype.setDisconnected = function() {
this.connected = false;
this.emit('disconnected');
};
ConnectionInPool.prototype.isHealthy = function() {
return this.unhealthyAt === 0;
return this.unhealthyAt === 0;
}
/**
@@ -596,7 +612,7 @@ PooledConnection.prototype._incr = function() {
} else if (this.connections[this.current_node].isStaleUnhealthy()) {
// unhealthy and stale, so let reset the node (appears as if unconnected).
this.connections[this.current_node].taken = false;
this.connections[this.current_node].connected = false;
this.connections[this.current_node].setDisconnected();
this.connections[this.current_node].unhealthyAt = 0;
break;
} else {
@@ -808,6 +824,9 @@ PooledConnection.prototype._closeConnections = function(closeCallback) {
if (con.connected) {
con.close(cb);
} else {
con.on('connected', function() {
con.close(null);
});
cb(null);
}
}, function(err) {
View
@@ -881,8 +881,8 @@ exports.testLearnStepTimeout = function(test, assert) {
server.listen(8688, '127.0.0.1', callback);
},
function executeQueryPooledConnection(callback) {
conn.execute('UPDATE CfUgly SET A=1 WHERE KEY=1', [], function(err) {
function executeQueryPooledConnection(callback) {
conn.execute('UPDATE CfUgly SET A=1 WHERE KEY=1', [], function(err) {
assert.ifError(err);
callback();
});
@@ -1084,6 +1084,7 @@ exports.testPooledConnectionShutdown = function(test, assert) {
});
};
exports.testPooledConnectionShutdownTwice = function(test, assert) {
var hosts = ['127.0.0.1:9160'];
var conn = new PooledConnection({'hosts': hosts, 'keyspace': 'Keyspace1'});
@@ -1117,3 +1118,15 @@ exports.testPooledConnectionShutdownTwice = function(test, assert) {
secondCbCalledImmediatelyWithError = true;
});
};
exports.testPooledContainerImmediateShutdown = function(test, assert) {
var hosts = ['127.0.0.1:9160'];
var pool = new PooledConnection({'hosts': hosts, 'keyspace': 'Keyspace1'});
pool.connect();
pool.shutdown(function(err) {
assert.ifError(err);
test.finish();
});
};

0 comments on commit 7eedb98

Please sign in to comment.