Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

lightning-pool

panates64.8kMIT4.11.1TypeScript support: included

Fastest generic Pool written with TypeScript

pool, generic, generic-pool

readme

lightning-pool

NPM Version NPM Downloads CI Tests Test Coverage

About

High performance resource pool written with TypeScript.

  • The fastest Resource Pool implementation for JavaScript ever! Check out benchmark results
  • Advanced configuration options, suits for enterprise level applications
  • Configuration can be changed while pool running
  • Promise based factory supported
  • Supports validation and resource reset
  • Fully tested. (%100 coverage)

Installation

  • npm install lightning-pool --save

Example

import {Pool} from 'lightning-pool';
import dbDriver from 'some-db-driver';

/**
 * Step 1 - Create a factory object
 */
const factory = {
    create: async function(opts) {
        const client = await DbDriver.createClient();
        return client;
    },
    destroy: async function(client) {  
       await client.close();       
    },
    reset: async function(client){   
       await client.rollback();       
    },
    validate: async function(client) {
       await client.query('select 1');       
    }    
};

/**
 * Step 2 - Create a the pool object
 */
const pool = new Pool(factory, {  
    max: 10,    // maximum size of the pool
    min: 2,     // minimum size of the pool
    minIdle: 2  // minimum idle resources
});

pool.start();

/**
 * Step 3 - Use pool in your code to acquire/release resources
 */
// acquire connection - Promise is resolved
const client = await pool.acquire();
// once a resource becomes available
// Use resource
await client.query("select * from foo");
// return object back to pool
await pool.release(client);

/**
 * Step 4 - Shutdown pool (optional)
 * Call close(force) when you need to shutdown the pool
 */

// Wait for active resource for 5 sec than force shutdown
await pool.close(5000);

Documentation

Creating a Pool instance

lightning-pool module exports createPool() method and Pool class. Both can be used to instantiate a Pool.

import {createPool} from 'lightning-pool';
const pool = createPool(factory, options);
import {Pool} from 'lightning-pool';
const pool = new Pool(factory, options);

factory

Can be any object/instance with the following properties:

  • create : The function that the Pool will call when it needs a new resource. It should return a Promise<resouce>.
  • destroy : The function that the Pool will call when it wants to destroy a resource. It should accept first argument as resource, where resource is whatever factory.create made. It should return a Promise<>.
  • reset (optional) : The function that the Pool will call before any resource back to the Pool. It should accept first argument as resource, where resource is whatever factory.create made. It should return a Promise<>. Pool will destroy and remove the resource from the Pool on any error.
  • validate (optional) : The function that the Pool will call when any resource needs to be validated. It should accept first argument as resource, where resource is whatever factory.create made. It should return a Promise<>. Pool will destroy and remove the resource from the Pool on any error.

options

  • acquireMaxRetries: Maximum number that Pool will try to create a resource before returning the error. (Default 0)
  • acquireRetryWait: Time in millis that Pool will wait after each tries. (Default 2000)
  • acquireTimeoutMillis: Time in millis an acquire call will wait for a resource before timing out. (Default 0 - no limit)
  • fifo: If true resources will be allocated first-in-first-out order. resources will be allocated last-in-first-out order. (Default true)
  • idleTimeoutMillis: The minimum amount of time in millis that an resource may sit idle in the Pool. (Default 30000)
  • houseKeepInterval: Time period in millis that Pool will make a cleanup. (Default 1000)
  • min: Minimum number of resources that Pool will keep. (Default 0)
  • minIdle: Minimum number of resources that Pool will keep in idle state. (Default 0)
  • max: Maximum number of resources that Pool will create. (Default 10)
  • maxQueue: Maximum number of request that Pool will accept. (Default 1000)
  • resetOnReturn: If true Pool will call reset() function of factory before moving it idle state. (Default true)
  • validation: If true Pool will call validation() function of factory when it needs it. If false, validation() never been called. (Default true)

Methods

Pool.prototype.acquire()

Acquires a resource from the Pool or create a new one.

Usage

pool.acquire(): Promise<any>

pool.acquire(factoryCreateOptions: any): Promise<any>

pool.acquire(callback:Callback): Promise<any>

pool.acquire(factoryCreateOptions?: any, callback:Callback): Promise<any>

  • Returns: A Promise
var promise = pool.acquire();
promise.then(resource => {
  // Do what ever you want with resource
}).catch(err =>{
  // Handle Error  
});

Pool.prototype.isAcquired()

Returns if a resource has been acquired from the Pool and not yet released or destroyed.

Usage

pool.isAcquired(resource)

  • resource: A previously acquired resource
  • Returns: True if the resource is acquired, else False
if (pool.isAcquired(resource)) {
  // Do any thing
}

Pool.prototype.includes()

Returns if the Pool contains a resource

Usage

pool.includes(resource)

  • resource: A resource object
  • Returns: True if the resource is in the Pool, else False
if (pool.includes(resource)) {
  // Do any thing
}

Pool.prototype.release()

Releases an allocated resource and let it back to pool.

Usage

pool.release(resource)

  • resource: A previously acquired resource
  • Returns: undefined
pool.release(resource);

Pool.prototype.destroy()

Releases, destroys and removes any resource from Pool.

Usage

pool.destroy(resource)

  • resource: A previously acquired resource
  • Returns: undefined
pool.destroy(resource);

Pool.prototype.start()

Starts the Pool and begins creating of resources, starts house keeping and any other internal logic.

Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called

Usage

pool.start()

  • Returns: undefined
pool.start();

Pool.prototype.close()

Shuts down the Pool and destroys all resources.

Usage

close(callback: Callback): void; close(terminateWait: number, callback?: Callback): Promise<void>; close(force: boolean, callback?: Callback): void;

  • force: If true, Pool will immediately destroy resources instead of waiting to be released
  • terminateWait: If specified, Pool will wait for active resources to release
  • callback: If specified, callback will be called after close. If not specified a promise returns.
var promise = pool.close();
promise.then(() => {
  console.log('Pool has been shut down') 
}).catch(err => {
  console.error(err);  
});

Properties

  • acquired (Number): Returns number of acquired resources.
  • available (Number): Returns number of idle resources.
  • creating (Number): Returns number of resources currently been created.
  • pending (Number): Returns number of acquire request waits in the Pool queue.
  • size (Number): Returns number of total resources.
  • state (PoolState): Returns current state of the Pool.
  • options (PoolOptions): Returns object instance that holds configuration properties
    • acquireMaxRetries (Get/Set): Maximum number that Pool will try to create a resource before returning the error. (Default 0)
    • acquireRetryWait (Get/Set): Time in millis that Pool will wait after each tries. (Default 2000)
    • acquireTimeoutMillis (Get/Set): Time in millis an acquire call will wait for a resource before timing out. (Default 0 - no limit)
    • fifo (Get/Set): If true resources will be allocated first-in-first-out order. resources will be allocated last-in-first-out order. (Default true)
    • idleTimeoutMillis (Get/Set): The minimum amount of time in millis that an resource may sit idle in the Pool. (Default 30000)
    • houseKeepInterval (Get/Set): Time period in millis that Pool will make a cleanup. (Default 1000)
    • min (Get/Set): Minimum number of resources that Pool will keep. (Default 0)
    • minIdle (Get/Set): Minimum number of resources that Pool will keep in idle state. (Default 0)
    • max (Get/Set): Maximum number of resources that Pool will create. (Default 10)
    • maxQueue (Get/Set): Maximum number of request that Pool will acceps. (Default 1000)
    • resetOnReturn (Get/Set): If true Pool will call reset() function of factory before moving it idle state. (Default true)
    • validation (Get/Set): If true Pool will call validation() function of factory when it needs it. If false, validation() never been called. (Default true)

Events

Pool derives from EventEmitter and produce the following events:

  • acquire: Emitted when a resource acquired.
pool.on('acquire', function(resource){
  //....
})
  • create: Emitted when a new resource is added to the Pool.

    pool.on('create', function(resource){
    //....
    })
  • create-error: Emitted when a factory.create informs any error.

    pool.on('create-error', function(error){
    //Log stuff maybe
    })
  • destroy: Emitted when a resource is destroyed and removed from the Pool.

  • destroy-error: Emitted when a factory.destroy informs any error.

    pool.on('destroy-error', function(error, resource){
    //Log stuff maybe
    })
  • return: Emitted when an acquired resource returns to the Pool.

    pool.on('start', function(resource){
    //...
    })
  • start: Emitted when the Pool started.

    pool.on('start', function(){
    //...
    })
  • closing: Emitted when before closing the Pool.

    pool.on('closing', function(){
    //...
    })
  • close: Emitted when after closing the Pool.

    pool.on('close', function(){
    //...
    })
  • validate-error: Emitted when a factory.validate informs any error.

    pool.on('validate-error', function(error, resource){
    //Log stuff maybe
    })

PoolState enum

Pool.PoolState (Number):

  • IDLE: 0, // Pool has not been started

  • STARTED: 1, // Pool has been started

  • CLOSING: 2, // Pool shutdown in progress

  • CLOSED: 3 // Pool has been shut down

Node Compatibility

  • node >= 16.0;

License

MIT

changelog

Changelog

v4.11.1 -

💬 General Changes

  • dev: Fixed circular typing dependencies @Eray Hanoğlu
  • doc: Fixed ci-test badge @Eray Hanoğlu
  • dev: Added git+ to repository url @Eray Hanoğlu

v4.11.0 - 2 May 2025

v4.10.1 - 28 January 2025

🛠 Refactoring and Updates

  • refactor: Updated dependencies @Eray Hanoğlu
  • refactor: Updated dependencies @Eray Hanoğlu

v4.10.0 - 15 October 2024

  • fix: pool.close does not wait for asynchronous resources to be destroyed

v4.9.0 - 15 October 2024

  • fix: Pool does not destroy resources unless started.

v4.8.0 - 20 August 2024

💬 General Changes

  • Fixed compatibility for "Node16" and "NodeNext" moduleResolution options @Eray Hanoğlu

v4.7.3 - 12 August 2024

💬 General Changes

  • Applied publint to check package.json @Eray Hanoğlu

v4.7.2 - 12 August 2024

💬 General Changes

  • Added "node" moduleResolution support @Eray Hanoğlu
  • Added "node" moduleResolution support @Eray Hanoğlu

v4.7.1 - 12 August 2024

💬 General Changes

  • Applied publint to check package.json @Eray Hanoğlu

v4.7.0 - 12 August 2024

🪲 Fixes

  • Fix: Added package.json in esm directory which overwrite "type" property to "module" @Eray Hanoğlu

v4.6.1 - 12 August 2024

v4.6.0 - 12 August 2024

💬 General Changes

  • Rollback to ES2020 @Eray Hanoğlu

v4.5.0 - 9 August 2024

💬 General Changes

  • Made ready for Node16 moduleResolution @Eray Hanoğlu

v4.4.3 - 3 August 2024

💬 General Changes

  • Fixed files config @Eray Hanoğlu

v4.4.2 - 3 August 2024

💬 General Changes

  • Fixed typings config @Eray Hanoğlu

v4.4.1 - 3 August 2024

💬 General Changes

  • Added "tslib" to dependencies @Eray Hanoğlu
  • Added "factoryCreateOptions" argument to .acquire() method. This argument will be passed to factory.create method @Eray Hanoğlu
  • Revert "Added "factoryCreateOptions" argument to .acquire() method. This argument will be passed to factory.create method" @Eray Hanoğlu

v4.4.0 - 29 July 2024

💬 General Changes

  • Revert "Added "factoryCreateOptions" argument to .acquire() method. This argument will be passed to factory.create method" @Eray Hanoğlu

v4.3.0 - 29 July 2024

💬 General Changes

  • Moved lint config to @panates/eslint-config-ts @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu
  • Added "factoryCreateOptions" argument to .acquire() method. This argument will be passed to factory.create method @Eray Hanoğlu
  • Updated config @Eray Hanoğlu
  • Updated config @Eray Hanoğlu
  • Updated config @Eray Hanoğlu
  • Updated config @Eray Hanoğlu
  • Updated config @Eray Hanoğlu
  • Updated config @Eray Hanoğlu
  • Updated node versions @Eray Hanoğlu
  • Updated node versions @Eray Hanoğlu
  • Updated node versions @Eray Hanoğlu
  • Updated node versions @Eray Hanoğlu
  • Updated config @Eray Hanoğlu
  • Updated node versions @Eray Hanoğlu
  • Updated config @Eray Hanoğlu
  • Updated config @Eray Hanoğlu
  • Updated node versions @Eray Hanoğlu
  • Updated config @Eray Hanoğlu

v4.2.2 - 10 September 2023

💬 General Changes

  • updated dependencies @Eray Hanoğlu

v4.2.1 - 9 January 2023

💬 General Changes

  • updated dependencies @Eray Hanoğlu

v4.2.0 - 14 October 2022

💬 General Changes

  • Fixed typing @Eray Hanoğlu

v4.1.0 - 22 September 2022

💬 General Changes

  • Fixed exports for multi module support @Eray Hanoğlu
  • Update CircleCI config @Eray Hanoğlu
  • Update CircleCI config @Eray Hanoğlu
  • Update CircleCI config @Eray Hanoğlu
  • Update CircleCI config @Eray Hanoğlu
  • Update CircleCI config @Eray Hanoğlu
  • Update CircleCI confi @Eray Hanoğlu
  • Update CircleCI config @Eray Hanoğlu

v4.0.0 - 17 September 2022

💬 General Changes

  • Added ESM module support @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu

v3.1.4 - 20 July 2022

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v3.1.3 - 3 July 2021

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v3.1.2 - 30 May 2021

💬 General Changes

  • Lint @Eray Hanoğlu

v3.1.1 - 17 March 2021

💬 General Changes

  • Updated dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu

v3.1.0 - 9 December 2020

💬 General Changes

  • Added releaseAsync() and destroyAsync() for async release operations. @Eray Hanoğlu
  • Updated node versions @Eray Hanoğlu

v3.0.1 - 19 November 2020

💬 General Changes

  • Moved all codebase to TypeScript @Eray Hanoğlu
  • Updated readme @Eray Hanoğlu
  • Immediately destroy resource when force it true @Eray Hanoğlu

v3.0.0 - 19 November 2020

💬 General Changes

  • Moved all codebase to TypeScript @Eray Hanoğlu

v2.4.0 - 17 November 2020

💬 General Changes

  • Added waitTerminateTime to close() method. Now the pool can wait for given time before terminating the pool. @Eray Hanoğlu

v2.3.0 - 9 October 2020

💬 General Changes

  • Removed resetOnReturn option. Reset will be executed if factory has reset method. @Eray Hanoğlu
  • Removed resetOnReturn option. Reset will be executed if factory has reset method. @Eray Hanoğlu

v2.2.0 - 1 October 2020

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v2.1.13 - 30 September 2020

💬 General Changes

  • Updated typing @Eray Hanoğlu

v2.1.12 - 25 September 2020

💬 General Changes

  • Updated typing @Eray Hanoğlu

v2.1.11 - 24 September 2020

💬 General Changes

  • Updated typing @Eray Hanoğlu

v2.1.10 - 22 September 2020

💬 General Changes

  • Updated typing @Eray Hanoğlu

v2.1.9 - 22 September 2020

💬 General Changes

  • Updated typing @Eray Hanoğlu

v2.1.8 - 22 September 2020

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v2.1.7 - 18 August 2020

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v2.1.6 - 16 July 2020

💬 General Changes

  • Updated dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu

v2.1.5 - 9 April 2020

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v2.1.4 - 9 April 2020

v2.1.3 - 13 December 2019

  • [PoolRequest] make callback optional

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v2.1.2 - 29 August 2019

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v2.1.1 - 6 August 2019

💬 General Changes

  • No need calling stop to exiting program. @Eray Hanoğlu

v2.1.0 - 6 August 2019

💬 General Changes

  • Added typings for TypeScript @Eray Hanoğlu

v2.0.10 - 11 April 2019

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v2.0.9 - 11 February 2019

💬 General Changes

  • Updated dependencies @Eray Hanoğlu

v2.0.8 - 14 December 2018

💬 General Changes

  • Updated dependencies @Eray Hanoğlu
  • [-] Fixed assert.rejects and assert.doesNotReject error for old node versions @Eray Hanoğlu
  • [-] Fixed test a bug @Eray Hanoğlu

v2.0.7 - 26 November 2018

💬 General Changes

  • Updated dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu

v2.0.6 - 26 November 2018

💬 General Changes

  • Updated dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoğlu

v2.0.4 - 13 October 2018

💬 General Changes

  • Updated dev dependencies @Eray Hanoğlu
  • Updated dependencies @Eray Hanoglu
  • Fixed travis script @Eray Hanoğlu
  • Coverage optimizations @Eray Hanoğlu
  • Updated travis script @Eray Hanoğlu
  • Updated travis script @Eray Hanoğlu
  • Updated dev dependencies @Eray Hanoğlu
    • Fixed inline documentation @Eray Hanoglu

v2.0.1 - 27 August 2018

💬 General Changes

    • Updated benchmark. Removed advanced-pool comparison (it is so slow to compare) @Eray Hanoglu
    • Updated benchmark. Removed advanced-pool comparison (it is so slow to compare) @Eray Hanoglu
    • Fix: State does not set to ACQUIRED after validate @Eray Hanoglu
    • Updated benchmark. Removed advanced-pool comparison (it is so slow to compare) @Eray Hanoglu

v2.0.0 - 13 August 2018

💬 General Changes

    • Removed obsolete callback support. Now factory works with promises only. @Eray Hanoglu

v1.2.0 - 10 August 2018

💬 General Changes

    • Moved to JavaScript ES6 @Eray Hanoglu

v1.1.2 - 12 March 2018

💬 General Changes

  • Updated dependencies @Eray Hanoglu

v1.1.1 - 12 March 2018

💬 General Changes

  • Updated dependencies @Eray Hanoglu
  • Removed redundant export @Eray Hanoglu

v1.1.0 - 10 January 2018

💬 General Changes

  • Renamed "stop" with "close" @Eray Hanoglu

v1.0.4 - 10 January 2018

💬 General Changes

  • [fix] Added missed EventEmitter.apply @Eray Hanoglu

v1.0.3 - 10 January 2018

💬 General Changes

  • [fix] Fix throwing error when use "min" @Eray Hanoglu
  • [fix] Fix invalid assert code @Eray Hanoglu

v1.0.2 - 13 November 2017

💬 General Changes

  • Shutdown process improved. @Eray Hanoglu

v1.0.1

💬 General Changes

  • First release @Eray Hanoglu
  • Initial commit @Eray Hanoglu
  • First release @Eray Hanoglu
  • First release @Eray Hanoglu
  • First release @Eray Hanoglu