Permalink
Browse files

[minor] Implement 3rd-Eden/jackpot as connection pool, see #54

  • Loading branch information...
1 parent 0df51f8 commit 2885d603f27dcf2e5b18cdc0bebab346b7576a83 @3rd-Eden committed Sep 14, 2012
Showing with 101 additions and 159 deletions.
  1. +0 −66 lib/connection.js
  2. +99 −92 lib/memcached.js
  3. +2 −1 package.json
View
@@ -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
@@ -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);
- }
-};
Oops, something went wrong.

0 comments on commit 2885d60

Please sign in to comment.