Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-page committed Jun 24, 2015
1 parent 6b9eb80 commit cb48e70
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 29 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ The following method is added to the Tedious [Connection](http://pekim.github.co
### Connection.release()
Release the connect back to the pool to be used again

## Version 0.3.7 Changes
* bug fix only

## Version 0.3.6 Changes
* bug fix only

Expand Down
55 changes: 29 additions & 26 deletions lib/connection-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function createConnection(pooled) {
return;

var self = this;
var endHandler;

this.log('creating connection: ' + cid);
var connection = new Connection(this.connectionConfig);
Expand All @@ -79,18 +80,18 @@ function createConnection(pooled) {

var handleError = function(err) {
self.log('connection closing because of error');
self.emit('error', err);

connection.removeListener('end', endHandler);
connection.close();

pooled.status = RETRY;
pooled.con = undefined;
if (pooled.timeout) {
if (pooled.timeout)
clearTimeout(pooled.timeout);
pooled.timeout = undefined;
}
connection.removeAllListeners('end');
connection.close();

setTimeout(createConnection.bind(self, pooled), self.retryDelay);
pooled.timeout = setTimeout(createConnection.bind(self, pooled), self.retryDelay);

self.emit('error', err);
};

connection.on('connect', function (err) {
Expand All @@ -115,7 +116,7 @@ function createConnection(pooled) {

connection.on('error', handleError);

connection.on('end', function () {
var endHandler = function () {
self.log('connection ended: ' + pooled.id);
if (self.drained) //pool has been drained
return;
Expand All @@ -127,7 +128,9 @@ function createConnection(pooled) {
return;
}
}
});
};

connection.on('end', endHandler);
}

function fill() {
Expand Down Expand Up @@ -270,21 +273,6 @@ ConnectionPool.prototype.drain = function (callback) {

this.drained = true;

var eventTotal = this.connections.length + this.waiting.length;
var eventCount = 0;

if (eventTotal === 0) {
if (callback)
callback();
return;
}

var ended = function() {
if (++eventCount === eventTotal)
if (callback)
callback();
};

for (i = this.waiting.length - 1; i >= 0; i--) {
var waiter = this.waiting[i];

Expand All @@ -294,14 +282,29 @@ ConnectionPool.prototype.drain = function (callback) {

this.waiting = null;

var eventTotal = this.connections.length;
var eventCount = 0;

if (eventTotal === 0 && callback)
callback();

var ended = function() {
if (++eventCount === eventTotal && callback)
callback();
};

for (var i = this.connections.length - 1; i >= 0; i--) {
var pooled = this.connections[i];
pooled.con.on('end', ended);

if (pooled.timeout)
clearTimeout(pooled.timeout);

pooled.con.close();
if (pooled.con) {
pooled.con.on('end', ended);
pooled.con.close();
} else {
ended();
}
}

this.connections = null;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tedious-connection-pool",
"version": "0.3.6",
"version": "0.3.7",
"description": "Connection Pool for tedious.",
"main": "lib/connection-pool.js",
"scripts": {
Expand Down
21 changes: 19 additions & 2 deletions test/connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ ALTER LOGIN test DISABLE
*/

describe('ConnectionPool', function () {

it('min', function (done) {
this.timeout(10000);

Expand Down Expand Up @@ -181,8 +180,12 @@ describe('ConnectionPool', function () {
});

it('pool error event', function (done) {
var poolConfig = {min: 2, max: 5};
this.timeout(10000);
var poolConfig = {min: 3};
var pool = new ConnectionPool(poolConfig, {});

pool.acquire(function() { });

pool.on('error', function(err) {
assert(!!err);
pool.drain(done);
Expand Down Expand Up @@ -334,4 +337,18 @@ describe('ConnectionPool', function () {
}));
});
});

it('drain', function (done) {
this.timeout(10000);

var poolConfig = {min: 3};
var pool = new ConnectionPool(poolConfig, {});

pool.acquire(function() { });

setTimeout(function() {
assert.equal(pool.connections.length, poolConfig.min);
pool.drain(done);
}, 4);
});
});

0 comments on commit cb48e70

Please sign in to comment.