0.1.1 • Published 4 years ago

thingy-pool v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

Thingy Pool

About

A promise-based object pool for Node.js.

Installation

$ npm install thingy-pool

Creating a factory

The factory is responsible for creating and destroying the objects you want to pool. The factory can also validate the objects if needed.

const Api = require('some-api');

const factory = {
  create: async () => {
    const api = new Api();
    await api.connect();
    return api;
  },
  destroy: async api => {
    await api.disconnect();
    console.log(`${api} has been disconnected!`);
  },
  validate: async api => {
    const response = await api.ping();
    const isValid = response && response.time < 200;
    return isValid;
  },
};

Creating a pool

See Options for a complete list of options

const Pool = require('thingy-pool');
const factory = require('./myObjectFactory');

const options = { minSize: 2, maxSize: 5, shouldValidateOnDispatch: true };
const pool = new Pool(factory, options);

Using the pool

// or const fetchSomething = async () => {
async function fetchAndPrint(something) {
  const thingy = await pool.acquire();
  const result = await thingy.fetch(something);
  pool.release(thingy);
  console.log(`fetched ${something}: ${result}`);
}

fetchAndPrint('something'); // fetched something: result
fetchAndPrint('somethingElse'); // fetched somethingElse: result

Objects must be released back to the pool after they are used. Alternatively, pool.use() can be passed a callback.

async function fetchAndPrint(something) {
  const result = await pool.use(thingy => thingy.fetch(something));
  console.log(`fetched ${something}: ${result}`);
}

fetchAndPrint('something'); // fetched something: result
fetchAndPrint('somethingElse'); // fetched somethingElse: result

Options

PropertyTypeDefaultDescription
minSizenumber0The minimum number of objects the pool will try to maintain including borrowed objects
maxSizenumber1The maximum number of objects the pool can manage including objects being created
defaultTimeoutInMsnumber | null30000The default time in milliseconds requests for objects will time out
maxPendingRequestsnumber | nullnullThe number of requests that can be queued if the pool is at the maximum size and has no objects available
idleCheckIntervalInMsnumber | nullnullThe interval the pool checks for and removes idle objects. Requires softIdleTimeInMs or hardIdleTimeInMs
maxIdleToRemovenumber | nullnullThe max objects that can be removed each time the pool checks for idle objects. null will check all objects
softIdleTimeInMsnumber | nullnullThe amount of time an object must be idle before being eligible for soft removal. Will not remove object if available objects is at or below minSize
hardIdleTimeInMsnumber | nullnullThe amount of time an object must be idle before being eligible for hard removal. Removes object regardless of minSize
shouldAutoStartbooleantrueShould the pool start creating objects to reach the minimum size as soon as it is created?
shouldValidateOnDispatchbooleanfalseShould the pool check objects with factory.validate before dispatching them to requests?
shouldValidateOnReturnbooleanfalseShould the pool check objects with factory.validate when they are being returned?
shouldUseFifobooleantrueShould the pool dispatch objects using first in first out (FIFO)?

Options with type number must be positive integers with the exception of minSize and maxPendingRequests which also accept 0. Think of null more like None. This is mainly to distinguish between maxPendingRequests: null which does not limit pending requests and maxPendingRequests: 0 which will reject requests if there a no objects available and no additional objects can be created. Not sure if that's useful...

Pool Methods

pool.acquire(options?) → {Promise\}

Request an object from the Pool. If no objects are available and the pool is below the maximum size, a new one will be created

ParameterTypeDefaultDescription
options.prioritynumber0The priority for the request. The higher the number the higher the priority
options.timeoutInMsnumber | null30000Time in milliseconds before the request times out
const thingy = await pool.acquire();

// With custom priority
const thingy = await pool.acquire({ priority: 5 });

// With custom timeout
const thingy = await pool.acquire({ timeoutInMs: 5000 });

// With custom priority and timeout
const thingy = await pool.acquire({ priority: 10, timeoutInMs: 1000 });

pool.clear() → {Promise\}

Destroys all pooled objects that are currently available. Resolves after objects have been destroyed

pool.getInfo(); // { size: 5, available: 2, borrowed: 3, ...moreInfo }
await pool.clear();
pool.getInfo(); // { size: 3, available: 0, borrowed: 3, ...moreInfo }

pool.getInfo() → {PoolInfo}

Current object counts and pool's state

pool.getInfo().available; // 2
// or
const { available, borrowed, size, state } = pool.getInfo();

pool.getOptions() → {Options}

Current pool options

pool.getOptions().defaultTimeoutInMs; // 30000
// or
const { maxSize, maxPendingRequests } = pool.getOptions();

pool.getSize() → {number}

Total number of objects in the pool. Includes objects being created and excludes objects being destroyed

pool.getSize(); // 3

pool.has(object) → {boolean}

Checks if the object is part of the pool

pool.has(thingy); // true

pool.isBorrowed(object) → {boolean}

Checks if the object is currently borrowed

pool.isBorrowed(thingy); // true

pool.getState() → {PoolState}

Current pool state as a string

pool.getState(); // 'STARTED'

pool.on(event, callback) → {this}

ParameterTypeDescription
eventPoolEventThe name of the event to listen for
callbackfunctionA function that can take the argument from the event
pool.on('poolDidStart', () => console.log('The pool has started!'));
pool.on('poolDidStop', () => console.log('The pool has stopped!'));

This is how you could stop the pool if the factory isn't creating objects correctly

let createErrorCount = 0;

function handleFactoryCreateError(error) {
  console.warn('An error occurred while trying to create an object');
  console.log(error);
  createErrorCount += 1;
  if (createErrorCount > 5) {
    console.error('Too many creation errors. Stopping the pool!');
    pool.stop();
  }
}

pool.on('factoryCreateError', handleFactoryCreateError);

pool.release(object) → {Promise\}

Returns the object back to the pool for future use

const thingy = await pool.acquire();
pool.getInfo(); // { size: 5, available: 2, borrowed: 3, ...moreInfo }
const result = await thingy.doSomethingAsync();
await pool.release(thingy);
pool.getInfo(); // { size: 5, available: 3, borrowed: 2, ...moreInfo }

pool.releaseAndDestroy(object) → {Promise\}

Returns the object to the pool and destroys it

const thingy = await pool.acquire();
pool.getInfo(); // { size: 5, available: 2, borrowed: 3, ...moreInfo }
const result = await thingy.doSomethingAsync();
await pool.releaseAndDestroy(thingy);
pool.getInfo(); // { size: 4, available: 2, borrowed: 2, ...moreInfo }

pool.start() → {Promise\}

Starts the pool

await pool.start();
const thingy = await pool.acquire();

pool.stop() → {Promise\}

Stops the pool

const thingy = await pool.acquire();
const finalResult = await thingy.doSomethingAsync();
pool.release(thingy);
pool.stop();

pool.use(callback, options?) → {Promise\}

Use a pooled object with a callback and release to object automatically

ParameterTypeDefaultDescription
callbackfunctionA function that takes a pooled object as an argument
options.prioritynumber0The priority for the request. The higher the number the higher the priority
options.timeoutInMsnumber | null30000Time in milliseconds before the request times out
const something = await pool.use(thingy => thingy.fetch('something'));
console.log(`I fetched ${something}`);

// or

pool.use(async thingy => {
  const something = await thingy.fetch('something');
  console.log(`I fetched ${something}`);
});

// with custom options
const something = await pool.use(thingy => thingy.fetch('something'), { priority: 10, timeoutInMs: 1000 });
console.log(`I fetched ${something}`);

Pool Info

const poolInfo = pool.getInfo();
PropertyTypeDescription
availablenumberNumber of objects that are available for requests
beingCreatednumberNumber of objects being created
beingDestroyednumberNumber of objects being destroyed. Not included in the total pool size
beingValidatednumberNumber of objects being validated. The sum of beingValidatedForDispatch and beingValidatedForReturn
beingValidatedForDispatchnumberNumber of objects being validated before attempting to dispatch to a request
beingValidatedForReturnnumberNumber of objects being validated before being returned to available objects
pendingRequestsnumberNumber of requests waiting for an object
borrowednumberNumber of objects currently borrowed
notBorrowednumberNumber of objects not currently borrowed
sizenumberTotal number of objects in the pool. Includes objects being created and excludes objects being destroyed
statestringThe current pool state

Pool Events

The pool is an event emitter and can emit the following events:

NameArgumentsDescription
factoryCreateErrorerrorAn error ocurred while trying to create an object. Likely an error from factory.create
factoryDestroyErrorerrorAn error ocurred while trying to destroy an object. Likely an error from factory.destroy
factoryValidateErrorerrorAn error ocurred while trying to validate an object. Likely an error from factory.validate
poolDidStartPool has started and initial objects have been created to reach the minimum pool size
poolDidStopPool has stopped and all objects have been destroyed

Credits

Thingy Pool was heavily inspired by Generic Pool. Thanks to @coopernurse and everyone that has contributed.

Thanks @vitomilana for your continued interest in this project and helping the team stay on track.

I built this as a way to learn more about programing. I really appreciate everyone out there asking and answering questions that are helping people they will never meet.

Special Thanks:

The above people and repositories really helped me while working on this. I'm including them to say thanks and maybe someone else will find them useful.

License

Copyright 2020 Pat Brown

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.