Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Support legacy single-argument callbacks to acquire().

  • Loading branch information...
commit a3b65a371e53599d5439df66566b6c1eff03f80e 1 parent 1cb9db9
Young Hahn authored May 18, 2011
37  lib/generic-pool.js
@@ -174,6 +174,20 @@ exports.Pool = function (factory) {
174 174
   }
175 175
 
176 176
   /**
  177
+   * Handle callbacks with either the [obj] or [err, obj] arguments in an
  178
+   * adaptive manner. Uses the `cb.length` property to determine the number
  179
+   * of arguments expected by `cb`.
  180
+   */
  181
+  function adjustCallback(cb, err, obj) {
  182
+    if (!cb) return;
  183
+    if (cb.length <= 1) {
  184
+      cb(obj);
  185
+    } else {
  186
+      cb(err, obj);
  187
+    }
  188
+  };
  189
+
  190
+  /**
177 191
    * Try to get a new client to work, and clean up pool unused (idle) items.
178 192
    *
179 193
    *  - If there are available clients waiting shift the first one out (LIFO),
@@ -185,23 +199,36 @@ exports.Pool = function (factory) {
185 199
    */
186 200
   function dispense() {
187 201
     var obj = null,
  202
+        err = null,
188 203
         waitingCount = waitingClients.size();
189 204
     log("dispense() clients=" + waitingCount + " available=" + availableObjects.length);
190 205
     if (waitingCount > 0) {
191 206
       if (availableObjects.length > 0) {
192 207
         log("dispense() - reusing obj");
193 208
         objWithTimeout = availableObjects.shift();
194  
-        waitingClients.dequeue()(null, objWithTimeout.obj);
  209
+        adjustCallback(waitingClients.dequeue(), err, objWithTimeout.obj);
195 210
       }
196 211
       else if (count < factory.max) {
197 212
         count += 1;
198 213
         log("dispense() - creating obj - count=" + count);
199  
-        factory.create(function (err, obj) {
  214
+        factory.create(function () {
200 215
           var cb = waitingClients.dequeue();
201  
-          if (cb) {
202  
-            cb(err, obj);
  216
+          if (arguments.length > 1) {
  217
+            err = arguments[0];
  218
+            obj = arguments[1];
  219
+          } else {
  220
+            err = (arguments[0] instanceof Error) ? arguments[0] : null;
  221
+            obj = (arguments[0] instanceof Error) ? null : arguments[0];
  222
+          }
  223
+          if (err) {
  224
+            count -= 1;
  225
+            adjustCallback(cb, err, obj);
203 226
           } else {
204  
-            me.release(obj);
  227
+            if (cb) {
  228
+              adjustCallback(cb, err, obj);
  229
+            } else {
  230
+              me.release(obj);
  231
+            }
205 232
           }
206 233
         });
207 234
       }
45  test/generic-pool.test.js
... ...
@@ -1,5 +1,5 @@
1 1
 var assert     = require('assert');
2  
-var poolModule = require('generic-pool');
  2
+var poolModule = require('..');
3 3
 
4 4
 module.exports = {
5 5
 
@@ -149,6 +149,49 @@ module.exports = {
149 149
         assert.throws(function() {
150 150
             pool.acquire(function(client) {});
151 151
         }, Error);
  152
+    },
  153
+
  154
+    'supports single arg callbacks' : function (beforeExit) {
  155
+        var pool = poolModule.Pool({
  156
+            name     : 'test5',
  157
+            create   : function(callback) { callback({ id : 1 }); },
  158
+            destroy  : function(client) { destroyed.push(client.id); },
  159
+            max : 2,
  160
+            idleTimeoutMillis : 100
  161
+        });
  162
+
  163
+        pool.acquire(function(client) {
  164
+            assert.equal(client.id, 1);
  165
+        });
  166
+    },
  167
+
  168
+    'handle creation errors' : function (beforeExit) {
  169
+        var created = 0;
  170
+        var pool = poolModule.Pool({
  171
+            name     : 'test6',
  172
+            create   : function(callback) {
  173
+                if (created < 5) {
  174
+                    callback(new Error('Error occurred.'));
  175
+                } else {
  176
+                    callback({ id : created });
  177
+                }
  178
+                created++;
  179
+            },
  180
+            destroy  : function(client) { },
  181
+            max : 1,
  182
+            idleTimeoutMillis : 1000
  183
+        });
  184
+        // ensure that creation errors do not populate the pool.
  185
+        for (var i = 0; i < 5; i++) {
  186
+            pool.acquire(function(err, client) {
  187
+                assert.ok(err instanceof Error);
  188
+                assert.ok(client === null);
  189
+            });
  190
+        }
  191
+        pool.acquire(function(err, client) {
  192
+            assert.ok(err === null);
  193
+            assert.equal(typeof client.id, 'number');
  194
+        });
152 195
     }
153 196
     
154 197
 };

0 notes on commit a3b65a3

Please sign in to comment.
Something went wrong with that request. Please try again.