Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #35 from blax/master

Remove client from availbleObjects on destroy(client)
  • Loading branch information...
commit b06334cd6dea5501147cc14dfd4287bafdf9a919 2 parents 452983d + e987abd
@coopernurse authored
Showing with 33 additions and 10 deletions.
  1. +14 −10 lib/generic-pool.js
  2. +19 −0 test/generic-pool.test.js
View
24 lib/generic-pool.js
@@ -120,6 +120,9 @@ exports.Pool = function (factory) {
*/
me.destroy = function(obj) {
count -= 1;
+ availableObjects = availableObjects.filter(function(objWithTimeout) {
+ return (objWithTimeout.obj !== obj);
+ });
factory.destroy(obj);
};
@@ -127,10 +130,10 @@ exports.Pool = function (factory) {
* 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;
@@ -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) {
View
19 test/generic-pool.test.js
@@ -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);
}
+
};
Please sign in to comment.
Something went wrong with that request. Please try again.