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

connection-pool

v0.0.4

Published

Connection pooling logic for use with any service

Downloads

31

Readme

connection-pool

Connection pooling logic for use with any service

Build Status Dependency Status NPM version

Installation

npm install connection-pool

Example

For example, say you wanted to set up and use a connection pool for MySQL:

var MySqlProvider = require('connection-pool/providers/mysql');
var LimitStrategy = require('connection-pool/strategies/limit');

var pool = new LimitStrategy(new MySqlProvider({
  host: 'hostname',
  port: 'port',
  user: 'user',
  password: 'password',
  database: 'database'
}), {
  limit: 20,
  idleTime: '10 minutes'
});

function query(str) {
  return pool.use(function (connection) {
    return new Promise(function (resolve, reject) {
      connection.query(str, function (err, rows) {
        if (err) reject(err);
        else resolve(rows);
      });
    });
  });
}

Strategies

Each strategy takes a Provider as it's first argument and an options object as its second argument. The provier must be an object with the following interface:

  • .create() returns either a Connection or a Promise.<Connection>
  • .destroy(connection) takes a Connection and ensures that it is properly disposed of
  • .isLive(connection) takes a Connection and returns true if it is still usable and false if an error may have caused it to become stale.
  • .unwrap(connection) takes a Connection and returns the true underlying conneciton/service to be passed to the use function.

Only .create() is required. By default, .destroy(connectioin) is a no-op, .isLive(connection) returns true and .unwrap(connection) returns the connection.

Base

All other strategies inherit from Base. The base strategy provides the following public methods:

  • expand - increases the number of connections available in the pool by one.
  • shrink - reduces the number of connections available in the pool by one.
  • destroy - removes all connections from the pool and prevents any more being opened.
  • use(fn) - take a connection out of the pool and give it to fn, wait for fn to complete, then return the connection to the pool. It will return a Promise for the result of calling fn.

The base stratgy is also an EventEmitter and provides the following events:

  • expand - fired when .expand() is called
  • shrink - fired when .shrink() is called
  • use - fired when .use() is called
  • queue-push - fired when a transaction is pushed into the queue
  • queue-shift - fired when a transaction is taken off the queue
  • begin-transaction - fired when a transaction starts
  • end-transaction - fired when a transaction ends

By default the base strategy is pretty useless as it starts with an empty pool. You can call expand to add to the pool though, so could fairly easilly have a fixed sized pool.

// create a connection pool of size 2
var BaseStrategy = require('conneciton-pool/strategies/base');

var pool = new BaseStrategy(provider);
pool.expand();
pool.expand();

Simple

The simple strategy creates a new connection whenever there is an operation waiting for a connection. The problem with this is that a sudden spike can lead to a lot of connections staying open forever (unless you manually close them using shrink).

Limit

Limit extends the Simple strategy by letting you specify a maximum number of connections to create and/or a maximum time to have idle connections before closing one.

var LimitStrategy = require('conneciton-pool/strategies/limit');

var pool = new LimitStrategy(provider, {
  min: 4, // always keep at least one connection open (default: 0)
  max: 20, // never open more than 20 connections (default: Infinity)
  idleTime: '10 minutes', // every 10 minutes that go past with at least the `lowWaterMark` number of
                          // connections going spare, close a conneciton (default: Infinity)
  lowWaterMark: 2 // only close connections if there are more than this many spare (default: 0)
});

License

MIT