Permalink
Browse files

#39 and #32 - remove adjustCallback and require acquire callback to a…

…ccept (err, obj) - not backwards compatible.
  • Loading branch information...
1 parent db4dcc3 commit 32923658f3183a9066782a3b196d4629a924c55c @coopernurse committed Jul 23, 2012
Showing with 26 additions and 23 deletions.
  1. +19 −4 README.md
  2. +7 −5 lib/generic-pool.js
  3. +0 −14 test/generic-pool.test.js
View
@@ -3,6 +3,15 @@
Generic resource pool. Can be used to reuse or throttle expensive resources such as
database connections.
+
+## 2.0 Release Warning
+
+The next npm release will remove support for variable argument callbacks. When you acquire
+a resource from the pool, your callback *must* accept two arguments: (err, obj)
+
+Previously this library attempted to determine the arity of the callback, but this resulted
+in a variety of issues. This change eliminates these issues, and makes the acquire callback
+parameter order consistent with the factory.create callback.
## Installation
@@ -93,10 +102,16 @@
// acquire connection - callback function is called
// once a resource becomes available
pool.acquire(function(err, client) {
- client.query("select * from foo", [], function() {
- // return object back to pool
- pool.release(client);
- });
+ if (err) {
+ // handle error - this is generally the err from your
+ // factory.create function
+ }
+ else {
+ client.query("select * from foo", [], function() {
+ // return object back to pool
+ pool.release(client);
+ });
+ }
});
### Step 3 - Drain pool during shutdown (optional)
View
@@ -223,14 +223,16 @@ exports.Pool = function (factory) {
var obj = null,
objWithTimeout = null,
err = null,
+ clientCb = null,
waitingCount = waitingClients.size();
log("dispense() clients=" + waitingCount + " available=" + availableObjects.length, 'info');
if (waitingCount > 0) {
if (availableObjects.length > 0) {
log("dispense() - reusing obj", 'verbose');
objWithTimeout = availableObjects.shift();
- adjustCallback(waitingClients.dequeue(), err, objWithTimeout.obj);
+ clientCb = waitingClients.dequeue();
+ clientCb(err, objWithTimeout.obj);
}
else if (count < factory.max) {
createResource();
@@ -242,7 +244,7 @@ exports.Pool = function (factory) {
count += 1;
log("createResource() - creating obj - count=" + count + " min=" + factory.min + " max=" + factory.max, 'verbose');
factory.create(function () {
- var cb = waitingClients.dequeue();
+ var clientCb = waitingClients.dequeue();
if (arguments.length > 1) {
err = arguments[0];
obj = arguments[1];
@@ -252,10 +254,10 @@ exports.Pool = function (factory) {
}
if (err) {
count -= 1;
- adjustCallback(cb, err, obj);
+ clientCb(err, obj);
} else {
- if (cb) {
- adjustCallback(cb, err, obj);
+ if (clientCb) {
+ clientCb(err, obj);
} else {
me.release(obj);
}
View
@@ -229,20 +229,6 @@ module.exports = {
}, 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({

0 comments on commit 3292365

Please sign in to comment.