Skip to content

Commit

Permalink
Merge pull request simplereach#76 from calvinfo/master
Browse files Browse the repository at this point in the history
Fixing double callback/timeout not closed problem
  • Loading branch information
devdazed committed Sep 28, 2012
2 parents bcf5c7e + dc6df14 commit 791da95
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/pool.js
Expand Up @@ -69,7 +69,7 @@ util.inherits(Pool, process.EventEmitter);
* @param {Function} callback The callback to invoke when all connections have been made
*/
Pool.prototype.connect = function(callback){
var i = 0, finished = 0, self = this,
var i = 0, finished = 0, self = this,
len = this.hosts.length * this.hostPoolSize,
connected = 0;

Expand All @@ -82,19 +82,18 @@ Pool.prototype.connect = function(callback){
connected += 1;
self.clients.push(connection);
}

if(finished === len){
if(self.clients.length === 0){
replyNotAvailable(callback);
}

//set the keyspaces connection to be the pool
if(keyspace){
keyspace.connection = self;
}
//we only want to callback once, after we get the final connection
callback(null, keyspace);

if(self.clients.length === 0){
replyNotAvailable(callback);
} else {
callback(null, keyspace);
}
//now that we have a connection, lets start monitoring
self.monitorConnections();
}
Expand Down Expand Up @@ -317,12 +316,17 @@ Pool.prototype.close = function(){

clearInterval(this.retryInterval);

if (len === 0){
this.emit('close');
}

function closed(){
j += 1;
if(j === len){
self.emit('close');
}
}

for(; i < len; i += 1){
this.clients[i].on('close', closed);
this.clients[i].close();
Expand Down
1 change: 1 addition & 0 deletions test/cql2.js
Expand Up @@ -33,6 +33,7 @@ module.exports = {
assert.isDefined(err);
badConn.cql(config['create_ks#cql'], function(err, res){
assert.isDefined(err);
badConn.close();
test.finish();
});
});
Expand Down
1 change: 1 addition & 0 deletions test/cql3.js
Expand Up @@ -60,6 +60,7 @@ module.exports = {
assert.isDefined(err);
badConn.cql(config['create_ks#cql'], function(err, res){
assert.isDefined(err);
badConn.close();
test.finish();
});
});
Expand Down
1 change: 1 addition & 0 deletions test/thrift.js
Expand Up @@ -47,6 +47,7 @@ module.exports = {
assert.isDefined(err);
badConn.dropKeyspace(config.keyspace, function(err){
assert.isDefined(err);
badConn.close();
test.finish();
});
});
Expand Down

0 comments on commit 791da95

Please sign in to comment.