Git Product home page Git Product logo

node-pool's Introduction

build status

Generic Pool

About

Generic resource pool with Promise based API. Can be used to reuse or throttle usage of expensive resources such as database connections.

V3 upgrade warning

Version 3 contains many breaking changes. The differences are mostly minor and I hope easy to accommodate. There is a very rough and basic upgrade guide I've written, improvements and other attempts most welcome.

If you are after the older version 2 of this library you should look at the current github branch for it.

History

The history has been moved to the CHANGELOG

Installation

$ npm install generic-pool [--save]

Example

Here is an example using a fictional generic database driver that doesn't implement any pooling whatsoever itself.

const genericPool = require("generic-pool");
const DbDriver = require("some-db-driver");

/**
 * Step 1 - Create pool using a factory object
 */
const factory = {
  create: function() {
    return DbDriver.createClient();
  },
  destroy: function(client) {
    client.disconnect();
  }
};

const opts = {
  max: 10, // maximum size of the pool
  min: 2 // minimum size of the pool
};

const myPool = genericPool.createPool(factory, opts);

/**
 * Step 2 - Use pool in your code to acquire/release resources
 */

// acquire connection - Promise is resolved
// once a resource becomes available
const resourcePromise = myPool.acquire();

resourcePromise
  .then(function(client) {
    client.query("select * from foo", [], function() {
      // return object back to pool
      myPool.release(client);
    });
  })
  .catch(function(err) {
    // handle error - this is generally a timeout or maxWaitingClients
    // error
  });

/**
 * Step 3 - Drain pool during shutdown (optional)
 */
// Only call this once in your application -- at the point you want
// to shutdown and stop using this pool.
myPool.drain().then(function() {
  myPool.clear();
});

Documentation

Creating a pool

Whilst it is possible to directly instantiate the Pool class directly, it is recommended to use the createPool function exported by module as the constructor method signature may change in the future.

createPool

The createPool function takes two arguments:

  • factory : an object containing functions to create/destroy/test resources for the Pool
  • opts : an optional object/dictonary to allow configuring/altering behaviour of the Pool
const genericPool = require('generic-pool')
const pool = genericPool.createPool(factory, opts)

factory

Can be any object/instance but must have the following properties:

  • create : a function that the pool will call when it wants a new resource. It should return a Promise that either resolves to a resource or rejects to an Error if it is unable to create a resource for whatever reason.
  • destroy: a function that the pool will call when it wants to destroy a resource. It should accept one argument resource where resource is whatever factory.create made. The destroy function should return a Promise that resolves once it has destroyed the resource.

optionally it can also have the following property:

  • validate: a function that the pool will call if it wants to validate a resource. It should accept one argument resource where resource is whatever factory.create made. Should return a Promise that resolves a boolean where true indicates the resource is still valid or false if the resource is invalid.

Note: The values returned from create, destroy, and validate are all wrapped in a Promise.resolve by the pool before being used internally.

opts

An optional object/dictionary with the any of the following properties:

  • max: maximum number of resources to create at any given time. (default=1)
  • min: minimum number of resources to keep in pool at any given time. If this is set >= max, the pool will silently set the min to equal max. (default=0)
  • maxWaitingClients: maximum number of queued requests allowed, additional acquire calls will be callback with an err in a future cycle of the event loop.
  • testOnBorrow: boolean: should the pool validate resources before giving them to clients. Requires that factory.validate is specified.
  • acquireTimeoutMillis: max milliseconds an acquire call will wait for a resource before timing out. (default no limit), if supplied should non-zero positive integer.
  • destroyTimeoutMillis: max milliseconds a destroy call will wait for a resource before timing out. (default no limit), if supplied should non-zero positive integer.
  • fifo : if true the oldest resources will be first to be allocated. If false the most recently released resources will be the first to be allocated. This in effect turns the pool's behaviour from a queue into a stack. boolean, (default true)
  • priorityRange: int between 1 and x - if set, borrowers can specify their relative priority in the queue if no resources are available. see example. (default 1)
  • autostart: boolean, should the pool start creating resources, initialize the evictor, etc once the constructor is called. If false, the pool can be started by calling pool.start, otherwise the first call to acquire will start the pool. (default true)
  • evictionRunIntervalMillis: How often to run eviction checks. Default: 0 (does not run).
  • numTestsPerEvictionRun: Number of resources to check each eviction run. Default: 3.
  • softIdleTimeoutMillis: amount of time an object may sit idle in the pool before it is eligible for eviction by the idle object evictor (if any), with the extra condition that at least "min idle" object instances remain in the pool. Default -1 (nothing can get evicted)
  • idleTimeoutMillis: the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction due to idle time. Supercedes softIdleTimeoutMillis Default: 30000
  • Promise: Promise lib, a Promises/A+ implementation that the pool should use. Defaults to whatever global.Promise is (usually native promises).

pool.acquire

const onfulfilled = function(resource){
	resource.doStuff()
	// release/destroy/etc
}

pool.acquire().then(onfulfilled)
//or
const priority = 2
pool.acquire(priority).then(onfulfilled)

This function is for when you want to "borrow" a resource from the pool.

acquire takes one optional argument:

  • priority: optional, number, see Priority Queueing below.

and returns a Promise Once a resource in the pool is available, the promise will be resolved with a resource (whatever factory.create makes for you). If the Pool is unable to give a resource (e.g timeout) then the promise will be rejected with an Error

pool.release

pool.release(resource)

This function is for when you want to return a resource to the pool.

release takes one required argument:

  • resource: a previously borrowed resource

and returns a Promise. This promise will resolve once the resource is accepted by the pool, or reject if the pool is unable to accept the resource for any reason (e.g resource is not a resource or object that came from the pool). If you do not care the outcome it is safe to ignore this promise.

pool.isBorrowedResource

pool.isBorrowedResource(resource)

This function is for when you need to check if a resource has been acquired from the pool and not yet released/destroyed.

isBorrowedResource takes one required argument:

  • resource: any object which you need to test

and returns true (primitive, not Promise) if resource is currently borrowed from the pool, false otherwise.

pool.destroy

pool.destroy(resource)

This function is for when you want to return a resource to the pool but want it destroyed rather than being made available to other resources. E.g you may know the resource has timed out or crashed.

destroy takes one required argument:

  • resource: a previously borrowed resource

and returns a Promise. This promise will resolve once the resource is accepted by the pool, or reject if the pool is unable to accept the resource for any reason (e.g resource is not a resource or object that came from the pool). If you do not care the outcome it is safe to ignore this promise.

pool.on

pool.on('factoryCreateError', function(err){
  //log stuff maybe
})

pool.on('factoryDestroyError', function(err){
  //log stuff maybe
})

The pool is an event emitter. Below are the events it emits and any args for those events

  • factoryCreateError : emitted when a promise returned by factory.create is rejected. If this event has no listeners then the error will be silently discarded

    • error: whatever reason the promise was rejected with.
  • factoryDestroyError : emitted when a promise returned by factory.destroy is rejected. If this event has no listeners then the error will be silently discarded

    • error: whatever reason the promise was rejected with.

pool.start

pool.start()

If autostart is false then this method can be used to start the pool and therefore begin creation of resources, start the evictor, and any other internal logic.

pool.ready

pool.ready()

Waits for the pool to fully start.

pool.use

const myTask = dbClient => {
  return new Promise( (resolve, reject) => {
    // do something with the client and resolve/reject
    })
}

pool.use(myTask).then(/* a promise that will run after myTask resolves */)

This method handles acquiring a resource from the pool, handing it to your function and then calling pool.release or pool.destroy with resource after your function has finished.

use takes one required argument:

  • fn: a function that accepts a resource and returns a Promise. Once that promise resolves the resource is returned to the pool, else if it rejects then the resource is destroyed.
  • priority: Optionally, you can specify the priority as number. See Priority Queueing section.

and returns a Promise that either resolves with the value from the user supplied fn or rejects with an error.

Idle Object Eviction

The pool has an evictor (off by default) which will inspect idle items in the pool and destroy them if they are too old.

By default the evictor does not run, to enable it you must set the evictionRunIntervalMillis option to a non-zero value. Once enable the evictor will check at most numTestsPerEvictionRun each time, this is to stop it blocking your application if you have lots of resources in the pool.

Priority Queueing

The pool supports optional priority queueing. This becomes relevant when no resources are available and the caller has to wait. acquire() accepts an optional priority int which specifies the caller's relative position in the queue. Each priority slot has it's own internal queue created for it. When a resource is available for borrowing, the first request in the highest priority queue will be given it.

Specifying a priority to acquire that is outside the priorityRange set at Pool creation time will result in the priority being converted the lowest possible priority

// create pool with priorityRange of 3
// borrowers can specify a priority 0 to 2
const opts = {
  priorityRange : 3
}
const pool = genericPool.createPool(someFactory,opts);

// acquire connection - no priority specified - will go onto lowest priority queue
pool.acquire().then(function(client) {
    pool.release(client);
});

// acquire connection - high priority - will go into highest priority queue
pool.acquire(0).then(function(client) {
    pool.release(client);
});

// acquire connection - medium priority - will go into 'mid' priority queue
pool.acquire(1).then(function(client) {
    pool.release(client);
});

// etc..

Draining

If you are shutting down a long-lived process, you may notice that node fails to exit for 30 seconds or so. This is a side effect of the idleTimeoutMillis behavior -- the pool has a setTimeout() call registered that is in the event loop queue, so node won't terminate until all resources have timed out, and the pool stops trying to manage them.

This behavior will be more problematic when you set factory.min > 0, as the pool will never become empty, and the setTimeout calls will never end.

In these cases, use the pool.drain() function. This sets the pool into a "draining" state which will gracefully wait until all idle resources have timed out. For example, you can call:

If you do this, your node process will exit gracefully.

If you know you would like to terminate all the available resources in your pool before any timeouts they might have are reached, you can use clear() in conjunction with drain():

const p = pool.drain()
.then(function() {
    return pool.clear();
});

The promise returned will resolve once all waiting clients have acquired and return resources, and any available resources have been destroyed

One side-effect of calling drain() is that subsequent calls to acquire() will throw an Error.

Pooled function decoration

This has now been extracted out it's own module generic-pool-decorator

Pool info

The following properties will let you get information about the pool:

// How many many more resources can the pool manage/create
pool.spareResourceCapacity

// returns number of resources in the pool regardless of
// whether they are free or in use
pool.size

// returns number of unused resources in the pool
pool.available

// number of resources that are currently acquired by userland code
pool.borrowed

// returns number of callers waiting to acquire a resource
pool.pending

// returns number of maxixmum number of resources allowed by pool
pool.max

// returns number of minimum number of resources allowed by pool
pool.min

Run Tests

$ npm install
$ npm test

The tests are run/written using Tap. Most are ports from the old espresso tests and are not in great condition. Most cases are inside test/generic-pool-test.js with newer cases in their own files (legacy reasons).

Linting

We use eslint combined with prettier

License

(The MIT License)

Copyright (c) 2010-2016 James Cooper <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-pool's People

Contributors

alextes avatar anupbaldawa avatar asannes avatar blax avatar bryandonovan avatar c-h-e-r-r-y avatar chdh avatar coopernurse avatar crashbell avatar diegorbaquero avatar dougwilson avatar drew-r avatar duzun avatar felipou avatar felixfbecker avatar geovanisouza92 avatar idan-at avatar jasonrhodes avatar jema28 avatar joez99 avatar kikobeats avatar poetro avatar rebareba avatar regevbr avatar restjohn avatar san00 avatar sandfox avatar sushantdhiman avatar turtlesoupy avatar windyrobin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-pool's Issues

me.destroy() not destroying

I don't know if I'm missing something, but in the code, me.destroy() is as follows:

https://github.com/coopernurse/node-pool/blob/master/lib/generic-pool.js#L114

me.destroy = function(obj) {
    count -= 1;
    factory.destroy(obj);
};

and, looking at the possible scenarios:

  • If the connection that is going to be destroyed it's available, then it's on availableObjects array, which is an array of objects of the form {obj:obj, timeout:timeout_in_ml}, just disconnecting the object (which is what the custom defined 'destroy' function at the constructor is meant for) won't remove the element {obj:obj, timeout:timeout_in_ms} from the availableObjects array, thus that item will still be available, but the object it returns won't be working anymore!.
  • If the connection that is going to be destroyed it's "in use", it should also be released (or that could be left in the hands of the user, I don't know).

I guess when destroying the conn, the 'availableObjects' corresponding item should be shifted, too.

Please, let me know if I'm missing something. I've read the code a zillion times and can't find how it can be otherwise than what I just said.

txs

should keep a list of "borrowed objects"

Currently we only keep a list of available objects/resource, and then rely on manual bookeeping to keep track of the number of borrowed items. This leaves lots of room for accounting errors and getting things into an inconsistent state.

We should have a list/array of borrowed objects.

pool.destroy(obj) is not documented, and requires release() call first

Pool.destroy() (that is, NOT the factory.destroy function) is not documented at all in the README.md, but is in fact useful.

It is especially if the resource being managed can fail and somehow signal that failure. For example, if the resource is a child_process. that child might die, and we then want to remove it from the pool.

This works, but only if pool.release(obj) is called BEFORE calling pool.destroy(). That should also be documented. The comments for pool.destroy say:

"This should be called within an acquire() block as an alternative to release()."

And this statement is just wrong. If release() is not called, the object will not be in availableObjects to be removed by the filter() call in pool.destroy.

Keep up the great work. This is a super useful library.

Removing something from pool

Sometimes you need to remove something from pool. For example you have a redis connection in pool and sometimes it's get corrpuped and pool keeps to borrow it to clients and there's no obvious way to stop it from borrowing it. Maybe you can keep it forever so it won't be borrowed, but it's not a good idea I guess. Maybe I miss something here?

Removing something from pool

Sometimes you need to remove something from pool. For example you have a redis connection in pool and sometimes it's get corrpuped and pool keeps to borrow it to clients and there's no obvious way to stop it from borrowing it. Maybe you can keep it forever so it won't be borrowed, but it's not a good idea I guess. Maybe I miss something here?

adjustCallback to use callbacks without params

Please, for some reasons, it's useful to put a callback function that does not have any params defined,
and in this case adjustCallback function calls callback with one parameter only, without an error information...
I suggest to change there a condition
to send ONE parameter only in ONE case - cb.length == 1, not cb.length<=1...
original function:

  function adjustCallback(cb, err, obj) {
    if (!cb) return;
    if (cb.length <= 1) {
      cb(obj);
    } else {
      cb(err, obj);
    }
  }

factory.min should be able to be == factory.max

The documentation says
"if this is set > max, the pool will silently set the min to factory.max - 1"

In fact, if you try to set min == max, which should be allowed by the statement above, it is set to max - 1. I worked around this by resetting it in the first call to my create() function. It works fine with min == max, and keeps exactly max objects in the pool as expected.

Thanks for this great library...it really helped me out.

remove deprecated functions

borrow and returnToPool have both been depreciated for a while now so lets bin them.

  • remove functions
  • update docs

aquire returning false

I just had a question about when aquire returns false. If it does this, that means all connections are currently in use. Does it automatically hold the request and wait for a connection to open up or does it just skip all remaining requests?

Replace test library

The current test library has been depreciated for around 3 years but somehow still works. We should probably replace it with something maintained like tap or lab etc before it gets broken by a new release of node.

Open to suggestions or PRs for this. It's going to be a fair chunk of work to rewrite everything I suspect.

Potential bug with use of object as a key (objectTimeout bug?)

Does using an object as a key actually do what the code thinks it should?

availableObjects.push(obj);
objectTimeout[obj] = (new Date().getTime() + idleTimeoutMillis);
var size = 0;
objectTimeout.test = 12345;
for (i in objectTimeout) { size++; }
log("timeout: " + objectTimeout[obj] + ", length: " + size);

In the logs:

pool test_pool - timeout: 1295298653460, length: 1
pool test_pool - timeout: 1295298653461, length: 1
pool test_pool - timeout: 1295298653461, length: 1

For instance, in JavaScript...

objectTimeout[{foo: 0}] === objectTimeout[{bar: 1}]

...which could be the cause of this.

Thanks!

wasteful destroy/create of idle resources

I'm setting a pool minimum and still seeing a wasteful destroy/create cycle for each resource in the queue. Given this simple nodejs file:

var i = 0;
var Pooler = require('generic-pool');
var pool = Pooler.Pool({
    idleTimeoutMillis : 3000,
    reapInterval : 1000,
    create: function(callback){
        console.log('expensive create', i);
        i++;
        callback(null, {i:i});
    },
    destroy: function(client){
        console.log('destroy', client.i);
    },
    min: 5,
    max: 10
});

I'm seeing the following log:

expensive create 0              
expensive create 1              
expensive create 2              
expensive create 3              
expensive create 4              
destroy 1                       
expensive create 5              
destroy 2                       
expensive create 6              
destroy 3                       
expensive create 7              
destroy 4                       
expensive create 8              
destroy 5                       
expensive create 9              
destroy 6                       
expensive create 10             
destroy 7                       
expensive create 11             
destroy 8                       
expensive create 12             
destroy 9                       
expensive create 13             
...

The behavior of destroying then immediately recreating these resources doesn't seem like desired behavior. Yes these timeouts are short, but I'd expect the pooler to only cleanup resources above my specified minimum that were unused. Am I missing something here?

Grow pool

Would it be an idea to be able to grow and shrink the pool ? I know it would in my use case :)

git tags

I notice that recent tags have been uploaded to npm, which is great, but they appear not to have been pushed to github, which would also be awesome to see (while less critical).

Still maintained?

Hi,

Since there are some old unresolved issues, I was just wondering if this package is still maintained?

Invoke createResource() manually?

Hi..
Is it possible to manually invoke createResource?

It's useful, for an instance if we're going to maintain the minimum threshold to be the 'minimum available resources' and not 'minimum resources regardless available or used'. Then we could call createResource() when needed.

Objects being aggressively reaped with 'undefined' timeouts

We're noticing connections getting reaped aggressively with this message:

pool test_pool - removeIdle() destroying obj - now:1295293476740 timeout:undefined
pool test_pool - removeIdle() destroying obj - now:1295293476740 timeout:undefined
pool test_pool - removeIdle() destroying obj - now:1295293476740 timeout:undefined

Does the order of these lines need to be transposed?

availableObjects.push(obj);
objectTimeout[obj] = (new Date().getTime() + idleTimeoutMillis);

Rob

What am I missing with releasing pool?

Ok I have a min of 2 max of 3. I run a query on a page hit. I hit the page once fine. I refresh gets another connection, fine. Hit the page again it grows the pool and gets a connection. Hit the page again and nothing happens. I would think that if I'm releasing the pool after each db query then the pool would shrink? I thought that was the whole point of a pool?

var pool = poolModule.Pool({
    name: 'mysql',
    create: function (callback) {

        console.log('create');

        var connection = mysql.createConnection({
            host: 'localhost',
            user: 'admin',
            password: 'test',
            database: 'test'
        });

        connection.connect(function () {
            return callback(null, connection);
        });

    },
    destroy: function (client) { // this is never called.  ever

        console.log('destroy');
        client.destroy();
    },
    max: 3,
    min: 2,

    idleTimeoutMillis: 3000, //changed this  to 3000
    log: true
});


pool.query = function (query, data, callback) {

    try {

        pool.acquire(function (err, client) {

          //called 3 times.  never called again.

            client.query(query, data, function (err, results, fields) {

                try {

                    pool.release(client); // I've also tried pool.destroy(client);          
                    return callback(err, results);

                } catch (err) {
                    console.log(err)
                }

            });
        });
    } catch (err) {
        console.log(err);
    }   
};

sample code required

hello,

can i have sample running code for connection pooling in node js.

thanks
pranav

Update tests to new version of expresso

diff --git a/test/generic-pool.test.js b/test/generic-pool.test.js
index 8ac55f6..9352136 100644
--- a/test/generic-pool.test.js
+++ b/test/generic-pool.test.js
@@ -1,8 +1,9 @@
 var poolModule = require('generic-pool');
+var assert = require('assert');

 module.exports = {

-    'expands to max limit' : function (assert, beforeExit) {
+    'expands to max limit' : function (beforeExit) {
         var createCount  = 0;
         var destroyCount = 0;
         var borrowCount  = 0;
@@ -36,7 +37,7 @@ module.exports = {
         });
     },

-    'supports priority on borrow' : function(assert, beforeExit) {
+    'supports priority on borrow' : function(beforeExit) {
         var borrowTimeLow  = 0;
         var borrowTimeHigh = 0;
         var borrowCount = 0;
@@ -83,7 +84,7 @@ module.exports = {
         });
     },

-    'removes correct object on reap' : function (assert, beforeExit) {
+    'removes correct object on reap' : function (beforeExit) {
         var destroyed = [];
         var clientCount = 0;

Pool size exceeds maximum

Hi there,

We're using the pool to handle MongoDB connections. On our MongoDB server, we're seeing up to 80 connections used even though we specify max:10 in our Pool config block. Is there any output or info we can provide to help debug?

Thanks!

Rob

meteor js integration

Hi guys,

I'm new in node js and I'm trying to integrate node-postgres package, which requires node-pool package but I'm having some errors. Could somebody please help me?

W20140324-12:49:25.508(-6)? (STDERR) /Users/crowdint/.meteor/tools/8e197f29c5/lib/node_modules/fibers/future.js:173
W20140324-12:49:25.509(-6)? (STDERR) throw(ex);
W20140324-12:49:25.509(-6)? (STDERR)      ^
W20140324-12:49:25.513(-6)? (STDERR) ReferenceError: exports is not defined
W20140324-12:49:25.514(-6)? (STDERR)     at app/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:99:1
W20140324-12:49:25.514(-6)? (STDERR)     at app/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:469:3
W20140324-12:49:25.514(-6)? (STDERR)     at /Users/crowdint/projects/kardex/.meteor/local/build/programs/server/boot.js:155:10
W20140324-12:49:25.515(-6)? (STDERR)     at Array.forEach (native)
W20140324-12:49:25.515(-6)? (STDERR)     at Function._.each._.forEach (/Users/crowdint/.meteor/tools/8e197f29c5/lib/node_modules/underscore/underscore.js:79:11)
W20140324-12:49:25.517(-6)? (STDERR)     at /Users/crowdint/projects/kardex/.meteor/local/build/programs/server/boot.js:82:5

This is the file:

exports.Pool = function (factory) {
  var me = {},
. . .

I did this change but I'm not sure it's what I have to do.

exports = typeof exports === "undefined" ? {} : exports
exports.Pool = function (factory) {
  var me = {},

I wonder where exports it's defined or if there is a problem in the order the files are getting loaded with meteor or something

Default values for create() and destroy() ?

Hi,

I'm using node-pool just as a concurrency limiter, without the need to create or destroy resources each time. Hence I don't need the create() and destroy() arguments but they are mandatory. Could there be default values for them ? like

             'create':function(callback) {
                callback();
            },
            'destroy':function() {}

Thanks!

Support errors on acquire

Currently the create() / acquire() method that is used to get a resource only returns one object in a callback - the resource in question. There are a few cases (invalid input, no disk space) in which object cannot be acquired and an error parameter would be useful. A simple-ish implementation of this could also ensure backwards compatibility.

Couple questions

Quick ones:

  1. Any chance of getting a git tag for the current release (and future releases)?
  2. Under what license is node-pool released?

Thanks!

add "reset" method

"reset" method that can be overridden is a very important addition that will let a "resting" a resource before putting it back to the pool. The resource shouldn't be available until it was "rested" and should be released on err.

var poolModule = require('generic-pool');
var pool = poolModule.Pool({

    reset: function(resource, done) { 
          resource.clean(function(err){}{ 
             done(err);
          });
    }

}

Events instead of log function

Hi,

I'd like to log my own custom errors. Also, I don't want to log every second the message "availableObjects.length=x". Can you inherite the pool from EventEmitter and just emit events so we can catch them and do whatever we want? It's a very easy modification. Just replace this line when you export the module:

var me = {},

By this:

var Pool = function (){
  events.EventEmitter.call (this);
};
Pool.prototype = Object.create (events.EventEmitter.prototype);
Pool.prototype.constructor = Pool;
var me = new Pool (),

You must add the events module:

var events = require ("events");

Then, instead of calling the log function you can throw an event with the data. For example, the createResource function:

me.emit ("create-resource", count, factory.min, factory.max);

And to catch it from outside your library:

pool.on ("create-resource", function (count, min, max){
  ...
});

how to deal with reconnect issue?

well ,after we create the pool ,
for example , a connection is lost and therefor useless ,
how to remove it from available queues?

.drain() doesn't clear timeouts

Calling .drain() doesn't clear the timeouts set by node-pool. This means node won't exit until all objects expired, even when clearing the pool manually or calling .destroyAllNow()

What is the right way to use pooling

Hi all,
I am developing a website using a node. js, tedious. js (database : MSSQL 2008). I am also using 'generic-pool' for pooling.
Here is my code

TediousPooler.js

var poolModule = require('generic-pool'), Connection = require('tedious').Connection;
var pool;
exports = module.exports = TediousPooler;
function TediousPooler(config, connection) {
pool = poolModule.Pool({
name: 'tedious',
create: function (callback) {
var connection = new Connection(config);
connection.on('connect', function (err) {
if (err) { console.log(err); }
else { callback(null, connection); }
});
},
destroy: function (client) { connection.close(); },
max: 10,
min: 2,
idleTimeoutMillis: 30000,
log: true
});
}

TediousPooler.prototype.callProcedure = function (request) {
pool.acquire(function (err, connection) {
if (err) { console.log(err); }
else {
connection.callProcedure(request)
}
});
}

TediousPooler.prototype.poolRelease = function (connection) {
pool.release(connection);
}


SqlConnect.js (here i am prepare statement for SQL eg. assigning parameters and get result from database )

var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;

var tediousPooler = require('./TediousPooler');

var config = {
server: '192.XXX.X.XXX',
userName: 'UserName',
password: 'Password',
options: {
database: 'DatabaseName'
}
};

var pooler;
module.exports.execute = function (sp, aParam, callback) {
var paramList = [];
var rowCollection = [];
paramList = aParam;
exec(sp);

function exec(sql) {
    request = new Request(sql, statementComplete, function (err) {
        if (err) { console.log('Error while executing request : ' + err); }
    });

    paramList.forEach(function (param) {
        if (param['paramType'] === 'Numeric') {
            param['paramType'] = TYPES.Numeric;
        }
        else if (param['paramType'] === 'BigInt') {
            param['paramType'] = TYPES.Int;
        }
        else if (param['paramType'] === 'Decimal') {
            param['paramType'] = TYPES.Decimal;
        }
        else {
            param['paramType'] = TYPES.VarChar;
        }
        request.addParameter(param['paramName'], param['paramType'], param['paramValue']);
    });

    paramList = [];

    request.on('columnMetadata', columnMetadata);
    request.on('row', row);


    if (pooler === undefined) {

        pooler = new tediousPooler(config);
     }
    pooler.callProcedure(request);
}

function statementComplete(err) {
    if (err) { callback('Statement failed: ' + err); }
    else {
        callback(rowCollection);
    }
}

function row(columns) {
    var row = {};
    columns.forEach(function (column) {
        if (column.isNull) {
        } else {
            row[column.metadata.colName] = column.value;
        }
    });
    rowCollection.push(row);
}

function ErrorMessage(error) {
    return;
}

function end() {
    pooler.drain(function () { pooler.destroyAllNow(); });
}

};


App.js - calling storeprocedure

SQLConnect = require('./SQLConnect')

var sp = "usp_ProcedureName"
var aSQLParam = [];
aSQLParam.push({ paramName: 'Name', paramType: 'VarChar', paramValue: 'Value' });

SQLConnect.execute(sp, aSQLParam, function (result) {
//here i am getting result/output from database
})

I want to know that the above process is valid or any other good can be available. and when i have to release pool object.

Implement an "acquire" timeout functionality

I am using node-cubrid, a CUBRID Database driver for Node.js with node-pool.

I have intentionally turned off the database server while having started the Node.js app which uses node-pool. I wanted to check what kind of an error I would receive from node-pool when acquiring a connection. However, node-pool was just hanging in there awaiting for a connection to be established for infinite time. Over 10 minutes has passed now, but node-pool is still not responding.

So, implementing a timeout functionality for acquire would be very useful.

Inactive sessions in oracle when connection-pooling with node-pool's generic-pool

We are using generic-pool with Oracle back end for connection pooling in our project. We are seeing a weird issue of inactive connections/sessions in oracle.
Our factory.max is set to 10 and min is set 2 so our understanding is that, number of connections won’t cross 10, but somehow in oracle we see a connection per request and its goes up very high till it reaches oracle limit (about 500). We can see old connections in inactive state in oracle and when we stop the node server all these connections go away from oracle. Our connection pool’s size never reaches 10, it keeps jumping between 4 - 6, and I am not sure what is going wrong.
We have kept idleTimeoutMillis to 5 minutes, but connections stay in inactive state even beyond 5 minutes. I experimented with reducing the time to 30 secs, no help.
In our code we are releasing connections, and that can be verified because connection pool size never reaches 10. any suggestion help is much appreciated.

Can this be used for a TCP server?

Imagine I have a net.createServer setup, and want to keep a maximum of connections, and delete idle connections, could this module be used for that? I'm failing to understand the logic on creating a "client" socket pool on a server, like how can I limit the connections one server can receive? And how the old idle connections are dropped? Would I need another façade class for the new connections?

destroy

If I call pool.destroy() it will decrease the count even if one didn't pass in any arguments, or the object is not found in the pool. It would be great to have some kind of a check for both cases.
Maybe not passing any argument could mean, that the user would want to destroy the whole pool (all items), or throw a warning if you don't like that idea.
The latter is harder to implement as I see atm, there is no keeping track of the used items, just the waiting. If you dont like the idea of keeping track of the objects, maybe remove the the public method remove. Although not keeping track of the in use objects also questions the value of the returnToPool method, as there is also no check if the object was generated by the factory, and returned by the pool with the use of borrow, or it is just made up.

ev_ref and ev_unref are deprecated

I'm getting these warnings:

WARNING: ev_ref is deprecated, use uv_ref
WARNING: ev_unref is deprecated, use uv_unref

Otherwise it's working great! Thanks.

Sorry - the warnings are not related to generic-pool. They are related to the db-mysql module.

Please close.

Client-side

Hi!

I'm also using node-pool in the browser to act as a AJAX request pool. It works great !

So I was just wondering if that was one of the goals of this project, at least maybe it could be mentionned somewhere in the doc because it may be useful for other people!

cheers

Heads up about .bind() and #14

First, thanks for merging #14.

I just ran into an interesting edge case where the callback .length based arg signature detection fails. If you use .bind() on a function, node will generate a new function which appears to be wrapped in such a way that .length does not accurately represent the "inner" function's arguments, e.g.

pool.acquire(function(err, resource) {
    // this works.
});
pool.acquire(function(err, resource) {
    // this fails -- looks like a 1 arg callback to node-pool.
}.bind(this));

I'm not sure what the best solution will be to this problem, or if it will affect most API users at all. It's easy enough to workaround so it might be worth documenting until we have a fix or the 1 arg callback support can be deprecated.

Is there any hooks for .on('error') to remove a driver from the pool?

Hi,

Great module - I didn't see it from briefly scanning your code: it's pretty common that with various drivers (especially stateful/long-lived ones), I'd want to have the pool auto-purge a client that emits an error event, so it can recreate another one to replace it. Is there a way to do that?

Thanks!
~Mark

Max lifetime parameter

Can you add this parameter? I use node-pool to create pool of FireFox browsers for parsing sites, and every 2h I have to restart my script because of memory leak. Can you help me with my project?=)

Allow resource validation time to be configurable

Currently resources are only tested when being pulled from the available pool.

We should add flags to allow (names debatable).

  • testOnBorrow
  • testOnReturn
  • testOnCreates
  • testWhileIdle - idle objects in the pool will be validated by the idle object evictor reapIntervalMillis

(unrelated, we should remove factory.refreshIdle, renameidleTimeoutMillis to something like getMinEvictableIdleTimeMillis and only refresh idle things if we have reapIntervalMillis

By default testOnBorrow will be true if validation function is supplied, false if not and testOnReturn will be false (which roughly follows current conventions)
I borrowed those names from some apache generic object pool

Maintaining a minimum active number of resources.

I'm having trouble getting my pool keep a minimum number of resources alive. Essentially, I want to have at least 4 available at all times and spawn up to 4 more when load becomes heavier.

This is my code:

this.generatorPool = poolModule.Pool({
    name: 'generator-pool',
    create: function (resolveResource) {
      var generator = new Generator();
      generator.start().then(
        // Generator load successful
        function() {
          resolveResource(null, generator);
        },
        // Generator failed to load
        function () {
          resolveResource(new Error('Failed to load generator'));
        }
      );
    },
    destroy: function (generator) {
      console.log('DESTROYING THE GENERATOR)
      generator.stop();
    },
    max: 8,
    min: 4,
    refreshIdle: false,
    idleTimeoutMillis: 30000
  });

For some reason the first 4 resources are still being destroyed after 30 seconds. What could be up?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.