racker/node-cassandra-client — pull request #75 by fourk (2013-08-23 16:59:38+00:00)
Title: WIP -- Fix race condition that results in 'Keyspace not defined' error

This is a hard-to-reproduce race condition. However, based on the way we previously would, inside `ConnectionInPool.prototype.connect` would perform...

```javascript
Connection.call(this, this._options);
Connection.prototype.connect.call(this, function(err) {
```

...I am confident that this is why we were seeing the race condition. That code was executed even when a connection was already in the process of attempting to connect. The callback from that initial connection attempt could fire before the second call to connect finished. The second call to connect would mangle the state of the connection object and cause it to re-start the connect process.

This PR is a work-in-progress. Tests pass and I am fairly confident that it fixes the race condition. It needs a few changes though:
* Not requiring underscore.js just for `_.once`. Replicate that particular bit of functionality without adding an additional dependency
* Not relying on event emitters for the `conn.on('connected'` callbacks. Keep an array of callbacks on the connection object, use a single event listener that clears this callback queue on connected.
* Figuring out how to properly deal with connections where `Connection is connected and unhealthy, but not stale`.