Registry / database / pool2
library1.4.1jsnpmunverified

Pool2 is a generic resource pooling library for Node.js, designed to efficiently manage expensive, reusable resources like database connections or network sockets. It provides a configurable framework for acquiring, releasing, disposing, and destroying resources, incorporating essential features such as minimum and maximum pool sizes, idle timeouts, request queuing, and health checks (ping functions). Key differentiators include its robust handling of resource lifecycle events and explicit timeouts for acquire and dispose operations, which helps prevent resource leaks and ensure application stability. The package is currently at version 1.4.1. While a specific release cadence isn't defined, the documentation indicates it's a mature and stable solution for resource management, offering fine-grained control over resource behavior and pooling strategies.

npm install pool2
INSTALL
IMPORT
SIG · POOL2
P
pool2
databasejavascriptv1.4.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Pool
const Pool = require('pool2');
import Pool from 'pool2';
Pool2 v1.x is primarily a CommonJS module. Direct ES Module 'import' syntax is not officially supported without a transpiler or a CommonJS wrapper.

Demonstrates the basic creation of a resource pool, acquiring and releasing resources, and observing pool statistics. It includes custom acquire/dispose/destroy functions with asynchronous operations and pool shutdown.

const Pool = require('pool2'); // Simulate a resource that takes time to acquire and needs disposal let resourceCounter = 0; function createResource(id) { return { id: id, status: 'open', connect: () => new Promise(resolve => setTimeout(() => { console.log(`Resource ${id} connected.`); resolve(); }, 100)), close: () => new Promise(resolve => setTimeout(() => { console.log(`Resource ${id} closed.`); resolve(); }, 50)), destroy: () => { console.log(`Resource ${id} forcibly destroyed.`); } }; } const pool = new Pool({ acquire: function (cb) { const id = ++resourceCounter; const rsrc = createResource(id); rsrc.connect() .then(() => cb(null, rsrc)) .catch(err => cb(err)); }, acquireTimeout: 5000, dispose: function (rsrc, cb) { rsrc.close() .then(() => cb()) .catch(err => cb(err)); }, disposeTimeout: 2000, destroy: function (rsrc) { if (rsrc && rsrc.status === 'open') { rsrc.destroy(); } }, ping: function (rsrc, cb) { // Simple ping, assume resource is always alive for this example cb(); }, min: 1, max: 3, idleTimeout: 3000, syncInterval: 1000 }); async function usePool() { console.log('Acquiring resource...'); let resource; try { resource = await new Promise((resolve, reject) => { pool.acquire((err, r) => { if (err) return reject(err); resolve(r); }); }); console.log(`Successfully acquired resource ${resource.id}. Doing work...`); await new Promise(resolve => setTimeout(resolve, 500)); // Simulate work } catch (err) { console.error('Failed to acquire resource:', err.message); return; } finally { if (resource) { console.log(`Releasing resource ${resource.id}.`); pool.release(resource); } } console.log('Current pool stats:', pool.stats()); } (async () => { await usePool(); await usePool(); await usePool(); console.log('Ending pool in 5 seconds...'); setTimeout(() => { pool.end((errs) => { if (errs && errs.length > 0) { console.error('Errors during pool end:', errs); } else { console.log('Pool ended gracefully.'); } }); }, 5000); })();
Debug
Known issues
gotchaThe `acquireTimeout` only dictates how long `pool2` waits for the `acquire` function to *finish* its callback. If the underlying resource acquisition (e.g., database connection) takes longer than `acquireTimeout` but eventually succeeds, `pool2` will have moved on, potentially leading to unmanaged 'in-flight' resources that exceed `max` capacity. It's crucial for the `acquire` function itself to implement and handle its own timeouts and clean up if it exceeds an internal limit.
fix
Implement connection-level timeouts within your `acquire` function's resource creation logic. For instance, if connecting to a database, ensure the database driver's connection timeout is set and that a timeout error from the driver is passed to the `acquire` callback to properly notify `pool2`.
affects: >=1.0.0
gotchaFailures in the `dispose` function, even if `disposeTimeout` is set, will still remove the resource from the pool. However, if the underlying resource's graceful closure fails, it may leave dangling sockets or open handles, preventing a graceful application exit or causing resource leaks outside of `pool2`'s control.
fix
Ensure your `dispose` function thoroughly attempts to close the resource and handles potential errors gracefully. Log any `dispose` failures prominently to identify and debug resource leakage issues promptly. Consider a more aggressive cleanup in the `destroy` function if `dispose` fails.
affects: >=1.0.0
gotchaThe `destroy` function is 'fire-and-forget' and does not accept a callback. This means `pool2` does not wait for or guarantee the completion of the `destroy` logic. It's intended as a last-resort cleanup.
fix
Keep `destroy` logic idempotent and non-blocking if possible. Critical cleanup that *must* complete should ideally be handled within a robust `dispose` function, or with external monitoring for resources that `destroy` attempts to clean up.
affects: >=1.0.0
gotchaIncorrectly configuring `min` and `max` pool sizes alongside `idleTimeout` and `syncInterval` can lead to unexpected pool behavior, such as premature resource destruction or excessive resource creation during low load, or connection pool exhaustion under high load if not sized correctly relative to the application's demands and database capacity.
fix
Carefully balance `min` and `max` based on your application's typical and peak load. Set `idleTimeout` to reclaim unused resources, but ensure it's shorter than any upstream server-side timeouts (e.g., database `wait_timeout`) to prevent stale connections. Monitor pool metrics (`pool.stats()`) in production to fine-tune these values.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Acquire Timeout Exceeded
The `acquire` function took longer than `acquireTimeout` to invoke its callback, or no resources were available within the `requestTimeout` and `maxRequests` limits. This does not necessarily mean the resource failed to connect, only that `pool2` stopped waiting.
fix
Increase `acquireTimeout` if the resource truly takes longer to provision, or set `requestTimeout` if waiting for an available resource. More importantly, check if the underlying resource acquisition in your `acquire` function has its own timeouts and handles them by reporting failure to `pool2`'s callback. Also verify `max` pool size and `maxRequests` are adequate for your load.
Application hangs or crashes unexpectedly due to too many open file descriptors or 'Socket already closed' errors.
The `dispose` function failed to properly close an underlying resource (e.g., a database connection), leading to a resource leak. While `pool2` removes the resource from its internal tracking, the external resource remains open and unmanaged.
fix
Thoroughly review the `dispose` function implementation to ensure all necessary steps are taken to gracefully close the resource. Add logging for `dispose` failures to quickly identify and troubleshoot issues related to resource leakage. Consider integrating a linter or static analysis tool to catch unhandled promises or forgotten callbacks.
Upgrade
Version history
1.4.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
10
Resources
pool2 — npm install pool2 · libregistry