generic-pool
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -1,2 +0,1 @@ | ||
var sys = require('sys'); | ||
var PriorityQueue = function(size) { | ||
@@ -84,3 +83,3 @@ var me = {}, slots, i, total = null; | ||
* Whether the pool should log activity. If function is specified, | ||
* that will be used instead. | ||
* that will be used instead. The function expects the arguments msg, loglevel | ||
* @param {Number} factory.priorityRange | ||
@@ -105,10 +104,11 @@ * The range from 1 to be treated as a valid priority | ||
log = factory.log ? | ||
(typeof factory.log === 'function' ? | ||
factory.log : | ||
function (str) { | ||
console.log("pool " + factory.name + " - " + str); | ||
} | ||
(function (str, level) { | ||
typeof factory.log === 'function' ? | ||
factory.log(str, level) : | ||
console.log(level.toUpperCase() + " pool " + factory.name + " - " + str); | ||
} | ||
) : | ||
function () {}; | ||
factory.max = Math.max(factory.max, 1); | ||
@@ -149,3 +149,3 @@ | ||
// The client timed out, call its destroyer. | ||
log("removeIdle() destroying obj - now:" + now + " timeout:" + timeout); | ||
log("removeIdle() destroying obj - now:" + now + " timeout:" + timeout, 'verbose'); | ||
me.destroy(availableObjects[i].obj); | ||
@@ -160,6 +160,6 @@ } | ||
if (al > 0) { | ||
log("availableObjects.length=" + al); | ||
log("availableObjects.length=" + al, 'verbose'); | ||
scheduleRemoveIdle(); | ||
} else { | ||
log("removeIdle() all objects removed"); | ||
log("removeIdle() all objects removed", 'verbose'); | ||
} | ||
@@ -210,6 +210,6 @@ } | ||
waitingCount = waitingClients.size(); | ||
log("dispense() clients=" + waitingCount + " available=" + availableObjects.length); | ||
log("dispense() clients=" + waitingCount + " available=" + availableObjects.length, 'info'); | ||
if (waitingCount > 0) { | ||
if (availableObjects.length > 0) { | ||
log("dispense() - reusing obj"); | ||
log("dispense() - reusing obj", 'verbose'); | ||
objWithTimeout = availableObjects.shift(); | ||
@@ -220,3 +220,3 @@ adjustCallback(waitingClients.dequeue(), err, objWithTimeout.obj); | ||
count += 1; | ||
log("dispense() - creating obj - count=" + count); | ||
log("dispense() - creating obj - count=" + count, 'verbose'); | ||
factory.create(function () { | ||
@@ -259,3 +259,3 @@ var cb = waitingClients.dequeue(); | ||
* | ||
* @returns {Object} `true` if the pool is fully utilized, `false` otherwise. | ||
* @returns {Object} `true` if the pool is not fully utilized, `false` otherwise. | ||
*/ | ||
@@ -270,5 +270,5 @@ me.acquire = function (callback, priority) { | ||
}; | ||
me.borrow = function (callback, priority) { | ||
log("borrow() is deprecated. use acquire() instead"); | ||
log("borrow() is deprecated. use acquire() instead", 'warn'); | ||
me.acquire(callback, priority); | ||
@@ -286,3 +286,3 @@ }; | ||
if (availableObjects.some(function(objWithTimeout) { return (objWithTimeout.obj === obj); })) { | ||
log("release called twice for the same resource: " + (new Error().stack)); | ||
log("release called twice for the same resource: " + (new Error().stack), 'error'); | ||
return; | ||
@@ -293,9 +293,9 @@ } | ||
availableObjects.push(objWithTimeout); | ||
log("timeout: " + objWithTimeout.timeout); | ||
log("timeout: " + objWithTimeout.timeout, 'verbose'); | ||
dispense(); | ||
scheduleRemoveIdle(); | ||
}; | ||
me.returnToPool = function (obj) { | ||
log("returnToPool() is deprecated. use release() instead"); | ||
log("returnToPool() is deprecated. use release() instead", 'warn'); | ||
me.release(obj); | ||
@@ -306,3 +306,3 @@ }; | ||
* Disallow any new requests and let the request backlog dissapate. | ||
* | ||
* | ||
* @param {Function} callback | ||
@@ -313,7 +313,7 @@ * Optional. Callback invoked when all work is done and all clients have been | ||
me.drain = function(callback) { | ||
log("draining"); | ||
log("draining", 'info'); | ||
// disable the ability to put more work on the queue. | ||
draining = true; | ||
var check = function() { | ||
@@ -339,3 +339,3 @@ if (waitingClients.size() > 0) { | ||
* clients as a result of subsequent calls to acquire. | ||
* | ||
* | ||
* @param {Function} callback | ||
@@ -345,3 +345,3 @@ * Optional. Callback invoked after all existing clients are destroyed. | ||
me.destroyAllNow = function(callback) { | ||
log("force destroying all objects"); | ||
log("force destroying all objects", 'info'); | ||
var willDie = availableObjects; | ||
@@ -363,2 +363,6 @@ availableObjects = []; | ||
me.getName = function() { | ||
return factory.name; | ||
}; | ||
me.availableObjectsCount = function() { | ||
@@ -365,0 +369,0 @@ return availableObjects.length; |
{ | ||
"name": "generic-pool", | ||
"description": "Generic resource pooling for Node.JS", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"author": "James Cooper <james@bitmechanic.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -13,2 +13,7 @@ | ||
1.0.9 - Dec 18 2011 | ||
- Merged #25 (add getName() - contributed by BryanDonovan) | ||
- Merged #27 (remove sys import - contributed by botker) | ||
- Merged #26 (log levels - contributed by JoeZ99) | ||
1.0.8 - Nov 16 2011 | ||
@@ -69,3 +74,3 @@ - Merged #21 (add getter methods to see pool size, etc. - contributed by BryanDonovan) | ||
idleTimeoutMillis : 30000, | ||
log : false | ||
log : true | ||
}); | ||
@@ -98,3 +103,5 @@ | ||
log : true/false or function - | ||
If a log is a function, it will be called with log strings | ||
If a log is a function, it will be called with two parameters: | ||
- log string | ||
- log level ('verbose', 'info', 'warn', 'error') | ||
Else if log is true, verbose log info will be sent to console.log() | ||
@@ -154,2 +161,20 @@ Else internal log messages be ignored (this is the default) | ||
## Pool info | ||
The following functions will let you get information about the pool: | ||
// returns factory.name for this pool | ||
pool.getName() | ||
// returns number of resources in the pool regardless of | ||
// whether they are free or in use | ||
pool.getPoolSize() | ||
// returns number of unused resources in the pool | ||
pool.availableObjectsCount() | ||
// returns number of callers waiting to acquire a resource | ||
pool.waitingClientsCount() | ||
## Run Tests | ||
@@ -156,0 +181,0 @@ |
@@ -10,3 +10,3 @@ var assert = require('assert'); | ||
var borrowCount = 0; | ||
var pool = poolModule.Pool({ | ||
@@ -21,3 +21,3 @@ name : 'test1', | ||
}); | ||
for (var i = 0; i < 10; i++) { | ||
@@ -35,3 +35,3 @@ var full = !pool.acquire(function(err, obj) { | ||
} | ||
beforeExit(function() { | ||
@@ -43,3 +43,3 @@ assert.equal(2, createCount); | ||
}, | ||
'supports priority on borrow' : function(beforeExit) { | ||
@@ -50,3 +50,3 @@ var borrowTimeLow = 0; | ||
var i; | ||
var pool = poolModule.Pool({ | ||
@@ -60,3 +60,3 @@ name : 'test2', | ||
}); | ||
for (i = 0; i < 10; i++) { | ||
@@ -74,3 +74,3 @@ pool.acquire(function(err, obj) { | ||
} | ||
for (i = 0; i < 10; i++) { | ||
@@ -88,3 +88,3 @@ pool.acquire(function(obj) { | ||
} | ||
beforeExit(function() { | ||
@@ -95,7 +95,7 @@ assert.equal(20, borrowCount); | ||
}, | ||
'removes correct object on reap' : function (beforeExit) { | ||
var destroyed = []; | ||
var clientCount = 0; | ||
var pool = poolModule.Pool({ | ||
@@ -108,4 +108,4 @@ name : 'test3', | ||
}); | ||
pool.acquire(function(err, client) { | ||
pool.acquire(function(err, client) { | ||
assert.equal(typeof client.id, 'number'); | ||
@@ -120,5 +120,5 @@ // should be removed second | ||
}); | ||
setTimeout(function() { }, 102); | ||
beforeExit(function() { | ||
@@ -129,3 +129,3 @@ assert.equal(2, destroyed[0]); | ||
}, | ||
'tests drain' : function (beforeExit) { | ||
@@ -136,3 +136,3 @@ var created = 0; | ||
var acquired = 0; | ||
var pool = poolModule.Pool({ | ||
@@ -145,3 +145,3 @@ name : 'test4', | ||
}); | ||
for (var i = 0; i < count; i++) { | ||
@@ -154,3 +154,3 @@ pool.acquire(function(err, client) { | ||
} | ||
assert.notEqual(count, acquired); | ||
@@ -163,3 +163,3 @@ pool.drain(function() { | ||
}); | ||
// subsequent calls to acquire should return an error. | ||
@@ -298,3 +298,44 @@ assert.throws(function() { | ||
}); | ||
}, | ||
'logPassesLogLevel': function(beforeExit){ | ||
var loglevels = {'verbose':0, 'info':1, 'warn':2, 'error':3}; | ||
var logmessages = {verbose:[], info:[], warn:[], error:[]}; | ||
var factory = { | ||
name : 'test1', | ||
create : function(callback) {callback(null, {id:Math.floor(Math.random()*1000)}); }, | ||
destroy : function(client) {}, | ||
max : 2, | ||
idleTimeoutMillis: 100, | ||
log : function(msg, level) {testlog(msg, level);} | ||
}; | ||
var testlog = function(msg, level){ | ||
assert.ok(level in loglevels); | ||
logmessages[level].push(msg); | ||
}; | ||
var pool = poolModule.Pool(factory); | ||
var pool2 = poolModule.Pool({ | ||
name : 'testNoLog', | ||
create : function(callback) {callback(null, {id:Math.floor(Math.random()*1000)}); }, | ||
destroy : function(client) {}, | ||
max : 2, | ||
idleTimeoutMillis: 100 | ||
}); | ||
assert.equal(pool2.getName(), 'testNoLog'); | ||
pool.acquire(function(err, obj){ | ||
if (err) {throw err;} | ||
assert.equal(logmessages.verbose[0], 'dispense() - creating obj - count=1'); | ||
assert.equal(logmessages.info[0], 'dispense() clients=1 available=0'); | ||
logmessages.info = []; | ||
logmessages.verbose = []; | ||
pool2.borrow(function(err, obj){ | ||
assert.equal(logmessages.info.length, 0); | ||
assert.equal(logmessages.verbose.length, 0); | ||
assert.equal(logmessages.warn.length, 0); | ||
}); | ||
}); | ||
} | ||
}; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
30298
608
205
0