Permalink
Browse files

Take pool index switches in account

Ensure that dead connections are also checked by isAvailable and removed
  • Loading branch information...
1 parent e3a387d commit 630cffa60ad3054098a63078f2e1d512156a43ef @3rd-Eden committed Jun 30, 2012
Showing with 11 additions and 3 deletions.
  1. +11 −3 lib/connectionpool.js
View
@@ -171,7 +171,7 @@ Manager.prototype.allocate = function allocate(fn) {
// available connection
probabilities.push({
probability: probability
- , index: i
+ , connection: connection
});
}
@@ -210,7 +210,7 @@ Manager.prototype.allocate = function allocate(fn) {
}).pop();
if (probability && probability.probability >= 60) {
- return fn(undefined, this.pool[probability.index]);
+ return fn(undefined, probability.connection);
}
// well, that didn't work out, so assume failure
@@ -221,11 +221,12 @@ Manager.prototype.allocate = function allocate(fn) {
* Check if a connection is available for writing.
*
* @param {net.Connection} net
+ * @param {Boolean} ignore ignore closed or dead connections
* @returns {Number} probability that his connection is available or will be
* @api private
*/
-Manager.prototype.isAvailable = function isAvailable(net) {
+Manager.prototype.isAvailable = function isAvailable(net, ignore) {
var readyState = net.readyState
, writable = readyState === 'open' || readyState === 'writeOnly'
, writePending = net._pendingWriteReqs || 0
@@ -236,6 +237,13 @@ Manager.prototype.isAvailable = function isAvailable(net) {
// sure that this stream is available for writing
if (writable && writes === 0) return 100;
+ // the connection is already closed or has been destroyed, why on earth are we
+ // getting it then, remove it from the pool and return 0
+ if (readyState === 'closed' || net.destroyed) {
+ this.remove(net);
+ return 0;
+ }
+
// if the stream isn't writable we aren't that sure..
if (!writable) return 0;

0 comments on commit 630cffa

Please sign in to comment.