@philiprehberger/disposable-pool
v0.1.1
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(),
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();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 reusemax?— Maximum pool size (default:Infinity)idleTimeout?— Destroy idle resources after this many msacquireTimeout?— 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-releasedrain()— 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 testLicense
MIT
