Skip to content

Commit

Permalink
Support legacy single-argument callbacks to acquire().
Browse files Browse the repository at this point in the history
  • Loading branch information
Young Hahn committed May 18, 2011
1 parent 1cb9db9 commit a3b65a3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
37 changes: 32 additions & 5 deletions lib/generic-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ 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.
*
Expand All @@ -185,23 +199,36 @@ exports.Pool = function (factory) {
*/
function dispense() {
var obj = null,
err = null,
waitingCount = waitingClients.size();
log("dispense() clients=" + waitingCount + " available=" + availableObjects.length);
if (waitingCount > 0) {
if (availableObjects.length > 0) {
log("dispense() - reusing obj");
objWithTimeout = availableObjects.shift();
waitingClients.dequeue()(null, objWithTimeout.obj);
adjustCallback(waitingClients.dequeue(), err, objWithTimeout.obj);
}
else if (count < factory.max) {
count += 1;
log("dispense() - creating obj - count=" + count);
factory.create(function (err, obj) {
factory.create(function () {
var cb = waitingClients.dequeue();
if (cb) {
cb(err, obj);
if (arguments.length > 1) {
err = arguments[0];
obj = arguments[1];
} else {
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 {
me.release(obj);
if (cb) {
adjustCallback(cb, err, obj);
} else {
me.release(obj);
}
}
});
}
Expand Down
45 changes: 44 additions & 1 deletion test/generic-pool.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var assert = require('assert');
var poolModule = require('generic-pool');
var poolModule = require('..');

module.exports = {

Expand Down Expand Up @@ -149,6 +149,49 @@ module.exports = {
assert.throws(function() {
pool.acquire(function(client) {});
}, 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');
});
}

};

0 comments on commit a3b65a3

Please sign in to comment.