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

lib-generic-pool

v1.0.5

Published

a generic pool implementation without any bells and whistles

Downloads

44

Readme

lib-generic-pool

This library provides a simple yet powerful way of implementing connection (& other kind of) pools. It works as a wrapper using existing underlying libraries (see FAQs)

Philosophy

  1. No bells and whistles
  2. Keep it simple silly

Table of contents

Usage

const Pool = require('lib-generic-pool');
/*
create a pool by providing at least these 4 parameters:
    - connSettings: an array of parameters passed to the createFunc that will created the underlying connection
    - createFunc: a function that returns a promise and creates the connection
    - destroyFunc: a function that returns a promise and destroyes a given connection
    - validateConnFunc - a function that is used to make sure that a connection is still active before the client acquires it; must return boolean value
*/

//// pre-requisites

let connSettings = ["10.1.1.1", {"username": "xxx", "password": "xxx"}]
let createFunc = async (connSettings) => { 
    // uses connSettings and then returns the connection using underlying library methods
    return { "key1": "value1" } 
    }
let destroyFunc = async (conn) => {// uses the conn and then ends/destroys using underlying library methods
}
let validateConnFunc = async (conn) => { return true }

//// working with the pool

// create pool
let pool = await Pool(connSettings, createFunc, destroyFunc, validateConnFunc);

// acquire conn
let conn1 = await pool.acquire()

// do something with it
// ...

// release conn
await pool.release(conn1)

// destroy pool
await pool.destroy()

API

Pool

Creates a new pool and initializes with the options.min number of connections

let pool = await Pool(connSettings, createFunc, destroyFunc, validateConnFunc, options);
  • connSettings: an array of parameters passed to the createFunc that will created the underlying connection
  • createFunc: a function that takes in connSettings and returns a promise that resolves to a newly created connection
  • destroyFunc: a function that takes in a conn and returns a promise that resolves once the conn has been destroyed/ended
  • validateConnFunc: a function that takes in a conn and checks if the conn is still active before the client acquires it; must return a promise that resolves to a boolean value
  • options: a set of options for the pool
    • min: minimum number of connections the pool maintains. (default = 1)
    • max: maximum number of connections the pool maintains. (default = 5)
    • acquireTimeoutSeconds: max seconds an acquire call will wait for a resource before timing out (default = 10)

pool.acquire

Acquire a connection from the pool.

  • It will attempt to create a new connection if
    • all existing connections are being used
    • no. of existing connections < options.max
  • If no. of existing connections = options.max
    • It will check every second until options.acquireTimeoutSeconds for an existing connection to be available again. If none of the connections become available, it will return with a rejected promise
  • If there is an error in the acquired connection, the client should release and acquire a new connection

It also validates an existing connection using validateConnFunc before returning it to the client. This helps auto cycle connections that are closed by the underlying library.

pool.release

Makes an existing connection available in the pool again

pool.destroy

Destoys all connections in the pool using destroyFunc

pool._create

Interal API and should not be directly used by clients

FAQ

  • Q: What underlying libraries can be used with lib-generic-pool ?
    • A: Any library that manages connections. However, the lib is oficially been tested with the following:
      • ssh2-sftp-client https://www.npmjs.com/package/ssh2-sftp-client
      • node-redis https://github.com/NodeRedis/node-redis/

Examples

Using ssh2-sftp-client

Refer to file named pool.sftp.test.js

Using node-redis

Refer to file named pool.redis.test.js