Generic async resource pool with acquire/release, validation, and auto-scaling
npm install @philiprehberger/disposable-poolGeneric async resource pool with acquire/release, validation, and auto-scaling.
npm install @philiprehberger/disposable-pool
import { createPool } from '@philiprehberger/disposable-pool';
const pool = createPool({
create: async () => await connectToDatabase(),
destroy: async (conn) => await conn.close(),
validate: async (conn) => conn.isAlive(),
max: 10,
acquireTimeout: 5000,
});
// Auto-release with withResource
const result = await pool.withResource(async (conn) => {
return await conn.query('SELECT * FROM users');
});
// Manual acquire/release
const conn = await pool.acquire();
try {
await conn.query('INSERT INTO logs ...');
} finally {
pool.release(conn);
}
// Graceful shutdown
await pool.drain();
createPool<T>(options: PoolOptions<T>): Pool<T>Creates a new resource pool.
PoolOptions<T>create — Factory function returning a new resourcedestroy — Cleanup function for a resourcevalidate? — Check if a resource is still valid before reusemax? — Maximum pool size (default: Infinity)idleTimeout? — Destroy idle resources after this many msacquireTimeout? — Reject acquire if waiting longer than this many msPool<T>acquire() — Get a resource from the poolrelease(resource) — Return a resource to the poolwithResource(fn) — Acquire, run fn, auto-releasedrain() — Destroy all resources and reject pending waiterssize — Total resources (idle + active)available — Number of idle resourcespending — Number of queued waitersnpm install
npm run build
npm test
If you find this project useful: