Permalink
Browse files

Started working on the connection pool interface

  • Loading branch information...
1 parent 355ea6c commit d751d14f1435e4232c33661c4b00e65ec1abc1c7 @3rd-Eden committed Jun 23, 2012
View
@@ -6,14 +6,17 @@ var EventEmitter = require('events').EventEmitter;
* A net.Stream connection pool.
*
* @constructor
- * @param {Number} limit
+ * @param {Number} limit size of the connection pool
+ * @param {Function} builder stream factory
* @api public
*/
-function Manager(limit) {
+function Manager(limit, builder) {
this.limit = limit || 10; // defaults to 10 connections max
this.pool = [];
this.generator = null;
+
+ if (builder) this.factory(builder);
}
/**
@@ -155,3 +158,49 @@ Manager.prototype.release = function release(net) {
return true;
};
+
+// alias remove to release
+Manager.prototype.remove = Manager.prototype.release;
+
+/**
+ * Free dead connections from the pool.
+ *
+ * @param {Number} keep the amount of connection to keep open
+ * @api public
+ */
+
+Manager.prototype.free = function free(keep) {
+ // default to 0 if no arguments are supplied
+ keep = keep || 0;
+
+ var saved = 0
+ , i = this.pool.length
+ , connection
+ , probability;
+
+ while (i--) {
+ connection = this.pool[i];
+ probability = this.isAvailable(connection);
+
+ // this is still a healthy connection, so try we probably just want to keep
+ // it
+ if (saved < keep && probability === 100) {
+ saved++;
+ continue;
+ }
+
+ this.release(connection);
+ }
+};
+
+/**
+ * Close the connection pool.
+ *
+ * @api public
+ */
+
+Manager.prototype.end = function end() {
+ return this.free(0);
+};
+
+module.exports = Manager;
File renamed without changes.
@@ -0,0 +1,10 @@
+/*globals expect */
+describe('connectionpool', function () {
+ 'use strict';
+
+ var ConnectionPool = require('../lib/connectionpool');
+
+ it('should exported as a function', function () {
+ expect(ConnectionPool).to.be.a('function');
+ });
+});

0 comments on commit d751d14

Please sign in to comment.