generic-pool
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -120,3 +120,3 @@ var PriorityQueue = function(size) { | ||
*/ | ||
function destroy(obj) { | ||
me.destroy = function(obj) { | ||
count -= 1; | ||
@@ -148,3 +148,3 @@ factory.destroy(obj); | ||
log("removeIdle() destroying obj - now:" + now + " timeout:" + timeout); | ||
destroy(availableObjects[i].obj); | ||
me.destroy(availableObjects[i].obj); | ||
} | ||
@@ -179,2 +179,16 @@ } | ||
/** | ||
* Handle callbacks with either the [obj] or [err, obj] arguments in an | ||
* adaptive manner. Uses the `cb.length` property to determine the number | ||
* of arguments expected by `cb`. | ||
*/ | ||
function adjustCallback(cb, err, obj) { | ||
if (!cb) return; | ||
if (cb.length <= 1) { | ||
cb(obj); | ||
} else { | ||
cb(err, obj); | ||
} | ||
}; | ||
/** | ||
* Try to get a new client to work, and clean up pool unused (idle) items. | ||
@@ -191,2 +205,4 @@ * | ||
var obj = null, | ||
objWithTimeout = null, | ||
err = null, | ||
waitingCount = waitingClients.size(); | ||
@@ -198,3 +214,3 @@ log("dispense() clients=" + waitingCount + " available=" + availableObjects.length); | ||
objWithTimeout = availableObjects.shift(); | ||
waitingClients.dequeue()(objWithTimeout.obj); | ||
adjustCallback(waitingClients.dequeue(), err, objWithTimeout.obj); | ||
} | ||
@@ -204,9 +220,21 @@ else if (count < factory.max) { | ||
log("dispense() - creating obj - count=" + count); | ||
factory.create(function (obj) { | ||
factory.create(function () { | ||
var cb = waitingClients.dequeue(); | ||
if (cb) { | ||
cb(obj); | ||
if (arguments.length > 1) { | ||
err = arguments[0]; | ||
obj = arguments[1]; | ||
} else { | ||
me.release(obj); | ||
err = (arguments[0] instanceof Error) ? arguments[0] : null; | ||
obj = (arguments[0] instanceof Error) ? null : arguments[0]; | ||
} | ||
if (err) { | ||
count -= 1; | ||
adjustCallback(cb, err, obj); | ||
} else { | ||
if (cb) { | ||
adjustCallback(cb, err, obj); | ||
} else { | ||
me.release(obj); | ||
} | ||
} | ||
}); | ||
@@ -306,3 +334,3 @@ } | ||
while (obj != null) { | ||
destroy(obj.obj); | ||
me.destroy(obj.obj); | ||
obj = willDie.shift(); | ||
@@ -316,2 +344,2 @@ } | ||
return me; | ||
}; | ||
}; |
{ | ||
"name": "generic-pool", | ||
"description": "Generic resource pooling for Node.JS", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"author": "James Cooper <james@bitmechanic.com>", | ||
@@ -9,3 +9,4 @@ "contributors": [ | ||
{ "name": "Peter Galiba", "email": "poetro@poetro.hu", "url": "http://poetro.hu/" }, | ||
{ "name": "Gary Dusbabek" } | ||
{ "name": "Gary Dusbabek" }, | ||
{ "name": "Tom MacWright", "url" : "http://www.developmentseed.org/" } | ||
], | ||
@@ -12,0 +13,0 @@ "keywords": ["pool", "pooling", "throttle"], |
@@ -13,2 +13,8 @@ | ||
1.0.6 - May 23 2011 | ||
- Merged #13 (support error variable in acquire callback - contributed by tmcw) | ||
- Note: This change is backwards compatible. But new code should use the two | ||
parameter callback format in pool.create() functions from now on. | ||
- Merged #15 (variable scope issue in dispense() - contributed by eevans) | ||
1.0.5 - Apr 20 2011 | ||
@@ -48,3 +54,6 @@ - Merged #12 (ability to drain pool - contributed by gdusbabek) | ||
c.connect(); | ||
callback(c); | ||
// parameter order: err, resource | ||
// new in 1.0.6 | ||
callback(null, c); | ||
}, | ||
@@ -59,3 +68,3 @@ destroy : function(client) { client.end(); }, | ||
// once a resource becomes available | ||
pool.acquire(function(client) { | ||
pool.acquire(function(err, client) { | ||
client.query("select * from foo", [], function() { | ||
@@ -82,4 +91,6 @@ // return object back to pool | ||
see example. (default 1) | ||
log : true/false - if true, verbose log info will be sent to console.log() | ||
(default false) | ||
log : true/false or function - | ||
If a log is a function, it will be called with log strings | ||
Else if log is true, verbose log info will be sent to console.log() | ||
Else internal log messages be ignored (this is the default) | ||
@@ -108,3 +119,3 @@ ## Priority Queueing | ||
// acquire connection - no priority - will go at end of line | ||
pool.acquire(function(client) { | ||
pool.acquire(function(err, client) { | ||
pool.release(client); | ||
@@ -114,3 +125,3 @@ }); | ||
// acquire connection - high priority - will go into front slot | ||
pool.acquire(function(client) { | ||
pool.acquire(function(err, client) { | ||
pool.release(client); | ||
@@ -120,3 +131,3 @@ }, 0); | ||
// acquire connection - medium priority - will go into middle slot | ||
pool.acquire(function(client) { | ||
pool.acquire(function(err, client) { | ||
pool.release(client); | ||
@@ -123,0 +134,0 @@ }, 1); |
var assert = require('assert'); | ||
var poolModule = require('generic-pool'); | ||
var poolModule = require('..'); | ||
@@ -14,4 +14,3 @@ module.exports = { | ||
create : function(callback) { | ||
createCount++; | ||
callback(createCount); | ||
callback(null, { count: ++createCount }); | ||
}, | ||
@@ -24,4 +23,5 @@ destroy : function(client) { destroyCount++; }, | ||
for (var i = 0; i < 10; i++) { | ||
pool.acquire(function(obj) { | ||
return function() { | ||
pool.acquire(function(err, obj) { | ||
return function(err, obj) { | ||
assert.equal(typeof obj.count, 'number'); | ||
setTimeout(function() { | ||
@@ -58,3 +58,3 @@ borrowCount++; | ||
for (i = 0; i < 10; i++) { | ||
pool.acquire(function(obj) { | ||
pool.acquire(function(err, obj) { | ||
return function() { | ||
@@ -96,3 +96,3 @@ setTimeout(function() { | ||
name : 'test3', | ||
create : function(callback) { callback({ id : ++clientCount }); }, | ||
create : function(callback) { callback(null, { id : ++clientCount }); }, | ||
destroy : function(client) { destroyed.push(client.id); }, | ||
@@ -103,7 +103,9 @@ max : 2, | ||
pool.acquire(function(client) { | ||
pool.acquire(function(err, client) { | ||
assert.equal(typeof client.id, 'number'); | ||
// should be removed second | ||
setTimeout(function() { pool.release(client); }, 5); | ||
}); | ||
pool.acquire(function(client) { | ||
pool.acquire(function(err, client) { | ||
assert.equal(typeof client.id, 'number'); | ||
// should be removed first | ||
@@ -129,3 +131,3 @@ pool.release(client); | ||
name : 'test4', | ||
create : function(callback) { callback({id: ++created}); }, | ||
create : function(callback) { callback(null, {id: ++created}); }, | ||
destroy : function(client) { destroyed += 1; }, | ||
@@ -137,4 +139,5 @@ max : 2, | ||
for (var i = 0; i < count; i++) { | ||
pool.acquire(function(client) { | ||
pool.acquire(function(err, client) { | ||
acquired += 1; | ||
assert.equal(typeof client.id, 'number'); | ||
setTimeout(function() { pool.release(client); }, 250); | ||
@@ -156,4 +159,47 @@ }); | ||
}, Error); | ||
}, | ||
'supports single arg callbacks' : function (beforeExit) { | ||
var pool = poolModule.Pool({ | ||
name : 'test5', | ||
create : function(callback) { callback({ id : 1 }); }, | ||
destroy : function(client) { destroyed.push(client.id); }, | ||
max : 2, | ||
idleTimeoutMillis : 100 | ||
}); | ||
pool.acquire(function(client) { | ||
assert.equal(client.id, 1); | ||
}); | ||
}, | ||
'handle creation errors' : function (beforeExit) { | ||
var created = 0; | ||
var pool = poolModule.Pool({ | ||
name : 'test6', | ||
create : function(callback) { | ||
if (created < 5) { | ||
callback(new Error('Error occurred.')); | ||
} else { | ||
callback({ id : created }); | ||
} | ||
created++; | ||
}, | ||
destroy : function(client) { }, | ||
max : 1, | ||
idleTimeoutMillis : 1000 | ||
}); | ||
// ensure that creation errors do not populate the pool. | ||
for (var i = 0; i < 5; i++) { | ||
pool.acquire(function(err, client) { | ||
assert.ok(err instanceof Error); | ||
assert.ok(client === null); | ||
}); | ||
} | ||
pool.acquire(function(err, client) { | ||
assert.ok(err === null); | ||
assert.equal(typeof client.id, 'number'); | ||
}); | ||
} | ||
}; | ||
}; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23793
477
173
0