New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix connection close race condition #31
Changes from all commits
1a62b66
8514797
8fa1838
5b6ff40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will result in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch, thanks. Fixed in 5b6ff40 |
||
con.close(null); | ||
}); | ||
cb(null); | ||
} | ||
}, function(err) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this test have failed outright previously or just timed out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It times out. Wasn't sure how to make it fail outright. |
||
assert.ifError(err); | ||
test.finish(); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not self.setDisconnected()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #31 (comment)