@philiprehberger/disposable-pool
v0.2.0
Published
Generic async resource pool with acquire/release, validation, and auto-scaling
Readme
@philiprehberger/disposable-pool

Generic async resource pool with acquire/release, validation, and auto-scaling.
Installation
npm install @philiprehberger/disposable-poolUsage
import { createPool } from '@philiprehberger/disposable-pool';
const pool = createPool({
create: async () => await connectToDatabase(),
destroy: async (conn) => await conn.close(),
validate: async (conn) => conn.isAlive(),
min: 2,
max: 10,
idleTimeout: 30000,
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);
}
// Flush stale idle resources without touching active ones
await pool.clear();
// Graceful shutdown
await pool.drain();API
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 reusemin?— Minimum resources to pre-warm and keep alive (default:0)max?— Maximum pool size (default:Infinity)idleTimeout?— Destroy idle resources after this many ms (never belowmin)acquireTimeout?— Reject acquire if waiting longer than this many ms
Pool<T>
acquire()— Get a resource from the poolrelease(resource)— Return a resource to the poolwithResource(fn)— Acquire, run fn, auto-releaseclear()— Destroy all idle resources and re-warm up tomindrain()— Destroy all resources and reject pending waiterssize— Total resources (idle + active)available— Number of idle resourcespending— Number of queued waiters
Development
npm install
npm run build
npm testSupport
If you find this project useful:
