JSPM

  • Created
  • Published
  • Downloads 4949854
  • Score
    100M100P100Q214465F

Generic resource pooling for Node.JS

Package Exports

  • generic-pool

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (generic-pool) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

About

Generic resource pool. Can be used to reuse or throttle expensive resources such as database connections.

Installation

$ npm install generic-pool

Example

// Create a MySQL connection pool with
// a max of 10 connections and a 30 second max idle time
var poolModule = require('generic-pool');
var pool = poolModule.Pool({
    name     : 'mysql',
    create   : function(callback) {
        var Client = require('mysql').Client;
        var c = new Client();
        c.user     = 'scott';
        c.password = 'tiger';
        c.database = 'mydb';
        c.connect();
        callback(c);
    },
    destroy  : function(client) { client.end(); },
    max      : 10,
    idleTimeoutMillis : 30000
});

// borrow connection - callback function is called
// once a resource becomes available
pool.borrow(function(client) {
    client.query("select * from foo", [], function() {
        // return object back to pool
        pool.returnToPool(client);
    });
});

Documentation

Pool() accepts an object with these slots:

              name : name of pool (string, optional)
            create : function that returns a new resource
                       should call callback() with the created resource
           destroy : function that accepts a resource and destroys it
               max : maximum number of resources to create at any given time
 idleTimeoutMillis : max milliseconds a resource can go unused before it should be destroyed
                     (default 30000)
reapIntervalMillis : frequency to check for idle resources (default 1000)

Run Tests

$ npm install expresso
$ expresso -I lib test/*.js