Skip to content

Commit

Permalink
integration of node-pool
Browse files Browse the repository at this point in the history
  • Loading branch information
rcanedo committed Oct 8, 2012
1 parent cb01350 commit 0b92961
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 65 deletions.
103 changes: 42 additions & 61 deletions lib/connection-pool.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var PooledConnection = require('./pooled-connection');
var PooledConnection = require('./pooled-connection'),
PoolModule = require('generic-pool');

var connectionEventNames = [
'connect',
Expand All @@ -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;
7 changes: 3 additions & 4 deletions lib/pooled-connection.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 0b92961

Please sign in to comment.