Skip to content

Commit

Permalink
[minor] Implement 3rd-Eden/jackpot as connection pool, see #54
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Sep 14, 2012
1 parent 0df51f8 commit 2885d60
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 159 deletions.
66 changes: 0 additions & 66 deletions lib/connection.js
Expand Up @@ -4,7 +4,6 @@ var EventEmitter = require('events').EventEmitter
, spawn = require('child_process').spawn
, Utils = require('./utils');

exports.Manager = ConnectionManager; // connection pooling
exports.IssueLog = IssueLog; // connection issue handling
exports.Available = ping; // connection availablity

Expand Down Expand Up @@ -104,68 +103,3 @@ issues.attemptReconnect = function attemptReconnect () {
Utils.merge(issue, JSON.parse(JSON.stringify(issue.config)));
});
};

function ConnectionManager (name, limit, constructor) {
this.name = name;
this.total = limit;
this.factory = constructor;
this.connections = [];
}

var Manager = ConnectionManager.prototype;

Manager.allocate = function allocate (callback) {
var total, i
, Manager = this;

i = total = this.connections.length;

// check for available
while (i--){
if (this.isAvailable(this.connections[i])) {
return callback(false, this.connections[i]);
}
}

// create new
if (total < this.total) {
return this.connections.push(this.factory.apply(this, arguments));
}

// give up and don't saturate the node.js process by retying #43
var full = new Error("All the connections in the memcached pool are busy");
full.connectionPool = true;
callback(full);
};

Manager.isAvailable = function isAvailable (connection) {
var readyState = connection.readyState;
return (readyState === 'open' || readyState === 'writeOnly')
&& !(connection._writeQueue && connection._writeQueue.length)
&& !(connection._handle && connection._handle.writeQueueSize);
};

Manager.remove = function remove (connection) {
var position = this.connections.indexOf(connection);

if (position !== -1) this.connections.splice(position, 1);
if (connection.readyState && connection.readyState !== 'closed' && connection.end) {
connection.end();
}
};

Manager.free = function freemymemories (keep) {
var save = 0
, connection;

while (this.connections.length) {
connection = this.connections.shift();

if (save < keep && this.isAvailable(this.connection[0])) {
save++;
continue;
}

this.remove(connection);
}
};

0 comments on commit 2885d60

Please sign in to comment.