diff --git a/lib/client-pool.js b/lib/client-pool.js index ed3793ec..5a3d3f8a 100644 --- a/lib/client-pool.js +++ b/lib/client-pool.js @@ -4,41 +4,11 @@ var defaults = require(__dirname + '/defaults'); module.exports = { init: function(Client) { - //wrap up common connection management boilerplate - var connect = function(config, callback) { - if(poolEnabled()) { - return getPooledClient(config, callback) - } - - var client = new Client(config); - client.connect(); - - var onError = function(error) { - client.removeListener('connect', onReady); - callback(error); - } - - var onReady = function() { - client.removeListener('error', onError); - callback(null, client); - client.on('drain', client.end.bind(client)); - } - - client.once('error', onError); - - client.once('connect', onReady); - } - - //connection pool global cache var clientPools = { } - var poolEnabled = function() { - return defaults.poolSize; - } - - var getPooledClient = function(config, callback) { + var connect = function(config, callback) { //lookup pool using config as key //TODO this don't work so hot w/ object configs var pool = clientPools[config]; diff --git a/lib/utils.js b/lib/utils.js index 3813ec94..f8c2e12f 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -20,10 +20,15 @@ var Pool = function(maxSize, createFn) { this.items = []; this.waits = []; } + sys.inherits(Pool, events.EventEmitter); + var p = Pool.prototype; p.checkOut = function(callback) { + if(!this.maxSize) { + return callback(null, this.createFn()); + } var len = 0; for(var i = 0, len = this.items.length; i < len; i++) { var item = this.items[i]; @@ -34,13 +39,7 @@ p.checkOut = function(callback) { //check if we can create a new item if(this.items.length < this.maxSize && this.createFn) { var result = this.createFn(); - var item = result; - //create function can return item conforming to interface - //of stored items to allow for create function to create - //checked out items - if(typeof item.checkedIn == "undefined") { - var item = {ref: result, checkedIn: true} - } + var item = {ref: result, checkedIn: true} this.items.push(item); if(item.checkedIn) { return this._pulse(item, callback) diff --git a/test/unit/utils-tests.js b/test/unit/utils-tests.js index 428ef406..cdf3f557 100644 --- a/test/unit/utils-tests.js +++ b/test/unit/utils-tests.js @@ -91,24 +91,28 @@ test('an empty pool', function() { }) }) -test('when creating async new pool members', function() { - var count = 0; - var pool = new Pool(3, function() { - var item = {ref: {name: ++count}, checkedIn: false}; - process.nextTick(function() { - pool.checkIn(item.ref) - }) - return item; +test('a pool with size of zero', function() { + var index = 0; + var pool = new Pool(0, function() { + return index++; }) - test('one request recieves member', function() { + test('checkin does nothing', function() { + index = 0; + pool.checkIn(301813); + assert.equal(pool.checkOut(assert.calls(function(err, item) { + assert.equal(item, 0); + }))); + }) + test('always creates a new item', function() { + index = 0; pool.checkOut(assert.calls(function(err, item) { - assert.equal(item.name, 1) - pool.checkOut(assert.calls(function(err, item) { - assert.equal(item.name, 2) - pool.checkOut(assert.calls(function(err, item) { - assert.equal(item.name, 3) - })) - })) + assert.equal(item, 0); + })) + pool.checkOut(assert.calls(function(err, item) { + assert.equal(item, 1); + })) + pool.checkOut(assert.calls(function(err, item) { + assert.equal(item, 2); })) }) }) @@ -167,7 +171,7 @@ test('normalizing connection info', function() { assert.equal(output.database, process.env.USER); assert.equal(output.port, 5432); }); - + test('uses overridden defaults', function() { defaults.host = "/var/run/postgresql"; defaults.user = "boom";