Skip to content

Commit

Permalink
Removing from available objects when destroy is called
Browse files Browse the repository at this point in the history
  • Loading branch information
blax committed May 3, 2012
1 parent 452983d commit e987abd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/generic-pool.js
Expand Up @@ -120,17 +120,20 @@ exports.Pool = function (factory) {
*/
me.destroy = function(obj) {
count -= 1;
availableObjects = availableObjects.filter(function(objWithTimeout) {
return (objWithTimeout.obj !== obj);
});
factory.destroy(obj);
};

/**
* Checks and removes the available (idle) clients that have timed out.
*/
function removeIdle() {
var toKeep = [],
var toRemove = [],
now = new Date().getTime(),
i,
al,
al, tr,
timeout;

removeIdleScheduled = false;
Expand All @@ -139,18 +142,19 @@ exports.Pool = function (factory) {
// check if they have timed out
for (i = 0, al = availableObjects.length; i < al; i += 1) {
timeout = availableObjects[i].timeout;
if (now < timeout) {
// Client hasn't timed out, so keep it.
toKeep.push(availableObjects[i]);
} else {
// The client timed out, call its destroyer.
if (now >= timeout) {
// Client timed out, so destroy it.
log("removeIdle() destroying obj - now:" + now + " timeout:" + timeout, 'verbose');
me.destroy(availableObjects[i].obj);
}
toRemove.push(availableObjects[i].obj);
}
}

for (i = 0, tr = toRemove.length; i < tr; i += 1) {
me.destroy(toRemove[i]);
}


// Replace the available items with the ones to keep.
availableObjects = toKeep;
al = availableObjects.length;

if (al > 0) {
Expand Down
19 changes: 19 additions & 0 deletions test/generic-pool.test.js
Expand Up @@ -319,6 +319,25 @@ module.exports = {
assert.equal(logmessages.warn.length, 0);
});
});
},

'removes from available objects on destroy': function(beforeExit){
var destroyCalled = false;
var factory = {
name: 'test',
create: function(callback) {callback(null, {})},
destroy: function(client) {destroyCalled = true},
max: 2,
idleTimeoutMillis: 100
};

var pool = poolModule.Pool(factory);
pool.acquire(function(err, obj){
pool.destroy(obj);
});
assert.equal(destroyCalled, true);
assert.equal(pool.availableObjectsCount(), 0);
}


};

0 comments on commit e987abd

Please sign in to comment.