npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-resource-pool

v0.0.1

Published

Resource pool class for node.

Readme

node-resource-pool

Build Status

This is a resource pool implementation for node. You can use it to handle pool connections, files, and other kinds of resources for which creation is an expensive operation.

Requirements

  • node (tested on 0.6.x and above but it should work on previous versions too).

Installation

In your package.json's dependencies add:

"node-resource-pool": "git://github.com/imkira/node-resource-pool"

or simply type from the command line:

npm install git://github.com/imkira/node-resource-pool

If you append --save, node-resource-pool will be automatically added to your package.json file.

Usage

Below is an example on how to use node-resource-pool with node-mysql.

var resourcePool = require('node-resource-pool');
var mysql = require('mysql');

var myPool = null;

myPool = resourcePool.create({
  create: function(done) { // callback for creating a resource (required)
    var connection = mysql.createConnection({
      host : 'localhost',
      user : 'root',
      password : 'password'
    });

    // you may want to remove the connection from the pool if some error happens
    connection.on('error', function() {
      myPool.destroy(connection);
    });

    // you may want to remove the connection from the pool if it is terminated
    connection.on('end', function() {
      myPool.destroy(connection);
    });

    connection._isValid = true;
    connection.connect();

    // done accepts error, resourceValue
    done(null, connection);
  },
  destroy: function(connection, done) { // callback for destroying a resource (required)
    // done does not accept parameters but needs to be called
    try {
      connection.destroy();
    }
    catch (err) {
    }
    done();
  },
  validate: function(resource) { // callback for validating a resource (optional)
    // should return true if resource is valid, or false otherwise
    return ((resource) && (resource.value) && (resource.value._isValid === true));
  },
  compare: function(connection1, connection2) { // callback for comparing resources (optional)
    return (connection1 === connection2);
  },
  backoff: function() { // function that returns the delay time to be placed on resource creation failure (optional)
    // Delays resource creation failure by a time between 100 and 1000 milliseconds.
    // If an error peak happens, backoff time will temporarily limit the rate at which requets are served.
    // For instance, assume you have a pool that has 0 free resources and that you are currently disconnected
    // from your database server. If you get a peak of requests to the pool,
    // all acquire operations will fail immediately as soon as the connection
    // object fails to connect but it will still be considered in
    // "creation-pending" state. If you have a limit on maxCreating, new requests
    // will be queued rather than immediately served. If in turn you have
    // maxRequests set to a reasonable value, when too many requests pile up
    // due to long periods of failure, requests will be immediately denied.
    return 100 + Math.floor(Math.random() * 900);
  },
  min: 5, // minimum number of resources to have ready at any time (required, default: 0)
  max: 500, // maximum number of resources handle (required, default: 1024)
  maxCreating: 100, // maximum number of resources in creation-pending state to handle (optional, default: unlimited)
  maxRequests: 1000, // maximum number of (unserved) waiting requests to queue, above which requests are automatically denied (optional, default: unlimited)
  acquireTimeout: 5000, // default timeout to wait for acquire (optional, default: unlimited)
  idleTimeout: 60000, // specify time after which unused (idle) resources get automatically destroyed (optional, default: disabled)
  idleCheckInterval: 1000, // interval used for checking whether a given resource is idle or not (optional, default: 1000)
  expireTimeout: 300000, // specify the timer that is placed on a given resource when it is created; if resource is not being used and this time is reached the resource is automatically destroyed (optional, default: disabled)
  expireCheckInterval: 1000 // interval used for checking whether a given resource is expired or not (optional, default: 1000)
});

myPool.acquire(function(err, connection) {
  if (err) {
    console.log('error', err);
  }
  else {
    console.log('resource acquired');
    // do someting with connection
    setTimeout(function() {
      console.log('we dont need this resource anymore, release it to the pool');
      myPool.release(connection);
    }, 1000);
  }
});

myPool.acquire(function(err, connection) {
  if (err) {
    console.log('error', err);
  }
  else {
    console.log('resource acquired');
    // if some error happens with the connection
    setTimeout(function() {
      // For instance, assume there was a problem with the connection and you
      // want to destroy it, rather than releasing it to the pool and getting
      // re-acquired.
      console.log('some error happened, let us destroy this resource');
      myPool.destroy(connection);
    }, 1000);
  }
});

// drain cancels unserved requests, so we wait 1 second before doing so or the
// previous acquire is cancelled.
setTimeout(function() {
  console.log('start draining');
  myPool.drain(function() {
    console.log('finished draining');
  });
}, 1000);

All times are interpreted in milliseconds.

Yet another resource pool? Why?

I know there are many resource pool implementations and I found myself using coopernurse's node-pool (thanks!) for some time. But after a while I decided to add my own features, to be in control of my pool just like I wanted. That being said, it doesn't mean this is cooler or better than other implementations. Anyway, use it if you think it fits your needs.

Contribute

  • Found a bug?
  • Want to contribute and add a new feature?

Please fork this project and send me a pull request!

License

node-resource-pool is licensed under the MIT license:

www.opensource.org/licenses/MIT

Copyright

Copyright (c) 2013 Mario Freitas. See LICENSE.txt for further details.