Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding option to global and local control for adjustCallback #39

Closed
wants to merge 2 commits into from

2 participants

@wookieb

Before commit

pool.Pool({
 name: 'test',
 create: function() { callback({some:'value'}) }
});
pool.acquire(function(client) {
  // passing client/result object as first argument of callback 
  // may confuse flow control modules like node-seq and others
  console.log(client.some); // value
});

After commit adjustCallback behavior can be controlled.


// local configuration
pool.Pool({
 create: function() { callback({some:'value'}) },
 adjustCallback: false,
 name: 'test'
});
pool.acquire(function(){
  var error = arguments[0], client = arguments[1];
  console.log(error); // undefined
  console.log(client.some); // value
});

// global configuration
pool.Pool.adjustCallback(false);
pool.Pool({
 name: 'test',
 create: function() { callback({some:'value'}) }
});
pool.acquire(function(){
  var error = arguments[0], client = arguments[1];
  console.log(error); // undefined
  console.log(client.some); // value
});

// global flag can be overwritten by local option
pool.Pool.adjustCallback(false);
pool.Pool({
 name: 'test',
 create: function() { callback({some:'value'}) },
 adjustCallback: true
});
pool.acquire(function(client){
  console.log(client.some); // value
});
@wookieb

Fix #32

@coopernurse
Owner

Hi there,

Thanks for the pull request. I wonder if it would be easier to simply declare that factory.create() functions must invoke callbacks using two arguments: (error, resource) and eliminate the adjustCallback code completely.

The adjustCallback() mechanism was an attempt to preserve backwards compatibility, but at this point just creates confusion. Given that virtually all node.js libraries rely on error being the first callback argument, I think most folks are using that in the create functions.

Since it's a non-backwards compatible change, I'd suggest we cut a new 1.0.x release that simply logs a warning if the create function doesn't have two arguments, and then cut a 1.1.0 release that removes adjustCallback.

I think that would make this pull request unnecessary, and would fix #32.

What do you think?

-- James

@wookieb

I'am totally agree with you.
I just want to create solution non-invasive to everyone. I'll remove adjustCallback when I back home.

@wookieb

Fixed :)

@coopernurse
Owner

Thanks! I'm still on vacation, but when I get home I'll work on getting the new release done. I will mark it 2.0.0 since this is not backwards compatible. There is at least one other non-backwards compatible change I want to make to clean up the API a bit.

@coopernurse coopernurse referenced this pull request from a commit
@coopernurse #39 and #32 - remove adjustCallback and require acquire callback to a…
…ccept (err, obj) - not backwards compatible.
3292365
@coopernurse
Owner

Ok, I just committed a patch that removes adjustCallback() -- I haven't pushed this to npm yet. If you have time, please grab this change from master and try it in your app and let me know if it works. If so I'll release this as 2.0.0 (to indicate that the change is not backwards compatible).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 40 deletions.
  1. +12 −26 lib/generic-pool.js
  2. +0 −14 test/generic-pool.test.js
View
38 lib/generic-pool.js
@@ -119,10 +119,10 @@ exports.Pool = function (factory) {
factory.max = parseInt(factory.max, 10);
factory.min = parseInt(factory.min, 10);
-
+
factory.max = Math.max(isNaN(factory.max) ? 1 : factory.max, 1);
factory.min = Math.min(isNaN(factory.min) ? 0 : factory.min, factory.max-1);
-
+
///////////////
/**
@@ -140,7 +140,7 @@ exports.Pool = function (factory) {
return (objWithTimeout.obj !== obj);
});
factory.destroy(obj);
-
+
ensureMinimum();
};
@@ -164,7 +164,7 @@ exports.Pool = function (factory) {
// Client timed out, so destroy it.
log("removeIdle() destroying obj - now:" + now + " timeout:" + timeout, 'verbose');
toRemove.push(availableObjects[i].obj);
- }
+ }
}
for (i = 0, tr = toRemove.length; i < tr; i += 1) {
@@ -196,20 +196,6 @@ exports.Pool = function (factory) {
}
/**
- * 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.
*
* - If there are available clients waiting, shift the first one out (LIFO),
@@ -224,20 +210,20 @@ exports.Pool = function (factory) {
objWithTimeout = null,
err = 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);
+ waitingClients.dequeue()(err, objWithTimeout.obj);
}
else if (count < factory.max) {
createResource();
}
}
}
-
+
function createResource() {
count += 1;
log("createResource() - creating obj - count=" + count + " min=" + factory.min + " max=" + factory.max, 'verbose');
@@ -252,17 +238,17 @@ exports.Pool = function (factory) {
}
if (err) {
count -= 1;
- adjustCallback(cb, err, obj);
+ cb(err, obj);
} else {
if (cb) {
- adjustCallback(cb, err, obj);
+ cb(err, obj);
} else {
me.release(obj);
}
}
});
}
-
+
function ensureMinimum() {
var i, diff;
if (!draining && (count < factory.min)) {
@@ -389,7 +375,7 @@ exports.Pool = function (factory) {
* Decorates a function to use a acquired client from the object pool when called.
*
* @param {Function} decorated
- * The decorated function, accepting a client as the first argument and
+ * The decorated function, accepting a client as the first argument and
* (optionally) a callback as the final argument.
*
* @param {Number} priority
@@ -417,7 +403,7 @@ exports.Pool = function (factory) {
callerCallback.apply(null, arguments);
}
});
-
+
decorated.apply(null, args);
}, priority);
};
View
14 test/generic-pool.test.js
@@ -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({
Something went wrong with that request. Please try again.