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 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-pool-cluster

v1.0.3

Published

Pooling generic resources for multi-server environment

Downloads

109

Readme

About

Pooling generic resources for multi-server environment.

This module extends Generic resource pool module, which can be used to reuse expensive resources. Node-pool-cluster module allows pooling connections to multi-server environments, it creates connection pools for multiple servers in a cluster, thus providing load balancing of requests to these connection pools.

A separate connection pool is created for each server added in the cluster. Requests for resources are load balance between different server pools added. The module balances request in round-robin fashion if the pools have zero waiting queue, otherwise request is served by least busy server pool.

Table of contents

Installation

$ npm install node-pool-cluster

Usage

The module uses similar convention used in generic-pool module. Any one familiar with node-pool module, will be able to easily switch to node-pool-cluster module, with minimal changes.

Create an object of node-pool-cluster using a poolFactory object

// Create a MySQL connection pool with a max of 5 connections, and a min of 2
// with a 20 seconds max idle time. These settings will be used for all pools
// created for individual servers.
var NodePoolCluster = require('node-pool-cluster');

var nodePoolCluster = NodePoolCluster.initCluster({
  name: 'mysql-client',
  destroy: function(mySqlClient) { mySqlClient.end(); },
  max: 5,
  min: 2,
  idleTimeoutMillis : 20000
});

One thing to note that poolFactory object doesn't contain create function, which is there in generic-pool module. However create function will have to passed when adding a server to the cluster. For documentation of poolFactory object fields (and other pool features), please refer to generic-pool documentation.

Add servers to the node-pool cluster

var MySQLClient = require('mysql').Client;

// Adding server 1 to the node-pool cluster
nodePoolCluster.addPool(function(callback) {
  var mySqlClient = new MySQLClient();
  mySqlClient.user = 'username';
  mySqlClient.password = 'password';
  mySqlClient.database = 'dbname';
  mySqlClient.host = 'host1';
  mySqlClient.connect(function(err) {
    callback(err, mySqlClient);  
  });
});

// Adding server 2 to the node-pool cluster
nodePoolCluster.addPool(function(callback) {
  var mySqlClient = new MySQLClient();
  mySqlClient.host = 'host2';
  mySqlClient.user = 'username';
  mySqlClient.password = 'password';
  mySqlClient.database = 'dbname';
  mySqlClient.connect(function(err) {
    callback(err, mySqlClient);  
  });
});

// … repeat for as many servers as you have in cluster.

In the above example, we have added two servers(host1 & host2) to the cluster. Each of these servers will have a separate connection pool created with settings from the poolFactory object used to create server pool.

Use cluster pool in your code to acquire/release resources

// Acquire connection - callback function is called once a resource becomes
// available.
nodePoolCluster.acquire(function(err, mySqlClient, pool) {
  if (err) {
    // Handle error - this is generally the err from your create function.
  } else {
    mySqlClient.query('SELECT * FROM table', [], function() {
      // Release connection object back to the pool.
      pool.release(mySqlClient);
    });
  }
});

If you're familiar with generic-pool, you'll see the only difference is that your callback function receives not only client connection, but also pool object for the server which has been chosen to serve your request. You will be using it to release client back to the specific pool.

How the load balancing works

Each server in the cluster has its own connection pool created with specified min and max number of connections. They are launched when first needed in a round-robin fashion and then are waiting to be reused. Whenever a new connection request comes, it first checks if any launched connections are available, it then distributes request in round-robin order between servers, thus maintaining equal share of requests among servers.

If all connections are busy, then the request is sent to the server pool with shortest waiting queue.

Error handling

Error handling is similar to generic-pool module.

Additional functions

getPoolSize()

Return total number of launched connections in all server pools.

removePool()

Removes a server pool from the cluster. It also drains the pool gracefully.

clear()

Removes all the server pools from cluster and drains the pools gracefully.

drainAll()

Drains all the pool in the server gracefully.

Run tests

$ npm install mocha
$ npm test

History

The history has been moved to the CHANGELOG

License

The MIT License (MIT)

Copyright (c) 2016 Kapil Joshi <[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.