Skip to content

Commit

Permalink
streams-like pattern to convey committment level
Browse files Browse the repository at this point in the history
Callers can throttle requests to the pool to prevent unconstrained
queueing which can cause memory issues in extreme cases.
  • Loading branch information
tilgovi committed Oct 17, 2011
1 parent 77ee3d7 commit 611a481
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/generic-pool.js
Expand Up @@ -248,13 +248,16 @@ exports.Pool = function (factory) {
* Optional. Integer between 0 and (priorityRange - 1). Specifies the priority
* of the caller if there are no available resources. Lower numbers mean higher
* priority.
*
* @returns {Object} `true` if the pool is fully utilized, `false` otherwise.
*/
me.acquire = function (callback, priority) {
if (draining) {
throw new Error("pool is draining and cannot accept work");
}
waitingClients.enqueue(callback, priority);
dispense();
return (count < factory.max);
};

me.borrow = function (callback, priority) {
Expand Down
3 changes: 2 additions & 1 deletion test/generic-pool.test.js
Expand Up @@ -19,7 +19,7 @@ module.exports = {
});

for (var i = 0; i < 10; i++) {
pool.acquire(function(err, obj) {
var full = !pool.acquire(function(err, obj) {
return function(err, obj) {
assert.equal(typeof obj.count, 'number');
setTimeout(function() {
Expand All @@ -28,6 +28,7 @@ module.exports = {
}, 100);
};
}());
assert.ok((i < 1) ^ full);
}

beforeExit(function() {
Expand Down

0 comments on commit 611a481

Please sign in to comment.