diff --git a/index.js b/index.js index 54683b9..0bbc4c4 100644 --- a/index.js +++ b/index.js @@ -23,18 +23,34 @@ function ConnectionPool(poolConfig, connectionConfig) { pool.config = poolConfig; pool.inUseConnections = []; pool.availableConnections = []; + pool.pendingConnectionRequests = []; pool.connectionAvailable = function(connection) { this.inUseConnections.splice(this.inUseConnections.indexOf(connection), 1); }; pool.requestConnection = function(callback) { - var connection = new PooledConnection(pool, connectionConfig); - pool.inUseConnections.push(connection); - - process.nextTick(function() { - callback(connection); - }); + var connection; + + if (pool.availableConnections.length > 1) { + connection = availableConnections.shift(); + } else if (pool.inUseConnections.length < pool.config.maxSize) { + connection = new PooledConnection(pool, connectionConfig); + } + + if (connection) { + useConnection(connection); + } else { + pool.pendingConnectionRequests.push(useConnection); + } + + function useConnection(connection) { + pool.inUseConnections.push(connection); + + process.nextTick(function() { + callback(connection); + }); + } }; return { diff --git a/test/connection-pool.test.js b/test/connection-pool.test.js index 541b867..4d059d6 100644 --- a/test/connection-pool.test.js +++ b/test/connection-pool.test.js @@ -11,64 +11,47 @@ var connectionConfig = { describe('ConnectionPool', function() { describe('one connection', function() { - var poolSize = 1; + var poolConfig = {maxSize: 1}; it('should connect, and end', function(done) { - var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig); - - requestConnectionAndClose(pool, function() { - done(); - }); + testPool(poolConfig, poolConfig.maxSize, requestConnectionAndClose, done); }); it('should connect, select, and end', function(done) { - var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig); - - requestConnectionSelectAndClose(pool, function() { - done(); - }); + testPool(poolConfig, poolConfig.maxSize, requestConnectionSelectAndClose, done); }); }); describe('multiple connections within pool maxSize', function() { - var poolSize = 5; + var poolConfig = {maxSize: 5}; it('should connect, and end', function(done) { - var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig); - - function doIt(done) { - requestConnectionAndClose(pool, function() { - done(); - }); - } - - var functions = []; - for (var f = 0; f < poolSize; f++) { - functions.push(doIt); - } - - async.parallel(functions, done); + testPool(poolConfig, poolConfig.maxSize, requestConnectionAndClose, done); }); it('should connect, select, and end', function(done) { - var pool = new ConnectionPool({maxSize: poolSize}, connectionConfig); - - function doIt(done) { - requestConnectionSelectAndClose(pool, function() { - done(); - }); - } - - var functions = []; - for (var f = 0; f < poolSize; f++) { - functions.push(doIt); - } - - async.parallel(functions, done); + testPool(poolConfig, poolConfig.maxSize, requestConnectionSelectAndClose, done); }); }); }); +function testPool(poolConfig, numberOfConnectionsToUse, useConnectionfunction, done) { + var pool = new ConnectionPool(poolConfig, connectionConfig); + + function doIt(done) { + useConnectionfunction(pool, function() { + done(); + }); + } + + var functions = []; + for (var f = 0; f < numberOfConnectionsToUse; f++) { + functions.push(doIt); + } + + async.parallel(functions, done); +} + function requestConnectionAndClose(pool, done) { pool.requestConnection(function (connection) { connection.on('connect', function(err) {