From 0b9296155a81a752352574543449d8e9f9488275 Mon Sep 17 00:00:00 2001 From: rcanedo Date: Mon, 8 Oct 2012 13:54:35 +0200 Subject: [PATCH] integration of node-pool --- lib/connection-pool.js | 103 ++++++++++++++++----------------------- lib/pooled-connection.js | 7 ++- 2 files changed, 45 insertions(+), 65 deletions(-) diff --git a/lib/connection-pool.js b/lib/connection-pool.js index a83651f..829fb08 100644 --- a/lib/connection-pool.js +++ b/lib/connection-pool.js @@ -1,4 +1,5 @@ -var PooledConnection = require('./pooled-connection'); +var PooledConnection = require('./pooled-connection'), + PoolModule = require('generic-pool'); var connectionEventNames = [ 'connect', @@ -13,69 +14,49 @@ var connectionEventNames = [ ]; function ConnectionPool(poolConfig, connectionConfig) { - var pool = this; - - pool.config = poolConfig || {}; - pool.config.maxSize = pool.config.maxSize || 10; - - pool.requestNumber = 0; - pool.stats = { - maxSize: poolConfig.maxSize, - connections: 0, - connectionsInUse: 0 - }; - pool.inUseConnections = []; - pool.availableConnections = []; - pool.pendingConnectionRequests = []; - - pool.returnConnectionToPool = function(connection) { - connectionEventNames.forEach(function removeAllListeners(eventName) { - connection.removeAllListeners(eventName); - }); - - pool.inUseConnections.splice(pool.inUseConnections.indexOf(connection), 1); - pool.stats.connectionsInUse--; + var self = this, + param = { + name: poolConfig.name || "", + log: true, + create: function(callback) { + connection = new PooledConnection(connectionConfig); + connection.on('connect', function(err) { + if (err) { + + } + else { + connection.on('release', function() { + console.log("on release"); + self.pool.release(connection); + }); + callback(null, connection); + } + }); + }, + destroy: function(connection) { + connection._close(); + }, + max: poolConfig.max || 10, + min: poolConfig.min || 0, + idleTimeoutMillis: poolConfig.idleTimeoutMillis || 3000 + }; + this.pool = PoolModule.Pool(param); + + +} +module.exports = ConnectionPool; - if (pool.pendingConnectionRequests.length > 0) { - var pendingConnectionRequest = pool.pendingConnectionRequests.shift(); - pendingConnectionRequest(undefined, connection); +ConnectionPool.prototype.requestConnection = function(callback) { + var self = this; + this.pool.acquire(function(err, connection) { + if(err) { + } - }; - - pool.requestConnection = function(callback) { - var requestNumber = ++pool.requestNumber; - var connection; - - if (pool.availableConnections.length > 0) { - connection = availableConnections.shift(); + else { + callback(connection); connection.emit('connect'); - } else if (pool.inUseConnections.length < pool.config.maxSize) { - connection = new PooledConnection(pool, connectionConfig); - connection.number = ++pool.stats.connections; } + }); +} - if (connection) { - useConnection(undefined, connection); - } else { - pool.pendingConnectionRequests.push(function (err, connection) { - useConnection(err, connection); - connection.emit('connect'); - }); - } - - function useConnection(err, connection) { - pool.inUseConnections.push(connection); - pool.stats.connectionsInUse++; - - callback(err, connection); - } - }; - - return { - requestConnection: pool.requestConnection, - stats: pool.stats - }; -}; - -module.exports = ConnectionPool; diff --git a/lib/pooled-connection.js b/lib/pooled-connection.js index c595d43..61c5f0f 100644 --- a/lib/pooled-connection.js +++ b/lib/pooled-connection.js @@ -1,20 +1,19 @@ var Connection = require('tedious').Connection; var util = require('util'); -function PooledConnection(pool, config) { +function PooledConnection(config) { Connection.call(this, config); - this.pool = pool; } util.inherits(PooledConnection, Connection); PooledConnection.prototype.close = function() { + this.emit('release'); this.emit('end'); - this.pool.returnConnectionToPool(this); } PooledConnection.prototype._close = function() { - Connection.prototype.close.call(this); + return Connection.prototype.close.call(this); } module.exports = PooledConnection;