Tool Name
@agent-tools/resource-pool
Description
Generic resource pool for managing expensive, reusable resources (database connections, HTTP clients, browser instances) with health validation, priority queuing, and graceful shutdown.
Why It's Useful for Agents
AI agents frequently need to manage pools of expensive resources — database connections, headless browser instances, API clients with rate limits, or LLM backend connections. Currently each tool reinvents pooling logic. A shared resource pool primitive lets every @agent-tools/* package that manages connections delegate lifecycle, health checks, and backpressure to one battle-tested implementation.
Researched libraries:
- generic-pool (2.4k stars, MIT) — mature, promise-based, priority queuing, idle eviction, drain/clear lifecycle
- tarn.js (516 stars, MIT, TypeScript) — robust error recovery, comprehensive timeouts on every operation, event-based diagnostics, used internally by Knex.js/Objection.js
Proposed API
import { createPool, Pool } from "@agent-tools/resource-pool";
// Create a pool with factory callbacks
const pool = createPool<pg.Client>({
create: async () => { const c = new pg.Client(); await c.connect(); return c; },
destroy: async (client) => { await client.end(); },
validate: async (client) => { await client.query("SELECT 1"); return true; },
}, {
min: 2,
max: 10,
acquireTimeout: 5000,
createTimeout: 3000,
destroyTimeout: 2000,
idleTimeout: 30000,
reapInterval: 1000,
});
// Acquire / release
const resource = await pool.acquire();
// ... use resource ...
pool.release(resource);
// Convenience wrapper (auto-release on completion)
const result = await pool.use(async (client) => {
return client.query("SELECT * FROM users");
});
// Priority queuing (lower = higher priority)
const urgent = await pool.acquire({ priority: 0 });
// Status introspection
pool.size; // total resources
pool.available; // idle resources
pool.borrowed; // in-use resources
pool.pending; // waiting acquires
// Events
pool.on("create", (resource) => { /* ... */ });
pool.on("destroy", (resource) => { /* ... */ });
pool.on("acquire", (resource) => { /* ... */ });
pool.on("timeout", (event) => { /* ... */ });
// Graceful shutdown
await pool.drain(); // stop new acquires, wait for returns
await pool.clear(); // destroy all resources
Scope
In scope:
- Generic resource pooling with create/destroy/validate factory pattern
- Min/max pool sizing with lazy and eager initialization
- Comprehensive timeouts (acquire, create, destroy, idle, reap)
- Priority-based acquisition queuing
- Health validation before resource handoff
- Event-based lifecycle diagnostics
use() convenience wrapper with auto-release
- Graceful drain + clear shutdown sequence
- Pool status introspection
Out of scope:
- Database-specific logic (belongs in
@agent-tools/database or @agent-tools/sql)
- Connection string parsing
- Query execution or ORM features
- Cluster/multi-host failover
Tool Name
@agent-tools/resource-poolDescription
Generic resource pool for managing expensive, reusable resources (database connections, HTTP clients, browser instances) with health validation, priority queuing, and graceful shutdown.
Why It's Useful for Agents
AI agents frequently need to manage pools of expensive resources — database connections, headless browser instances, API clients with rate limits, or LLM backend connections. Currently each tool reinvents pooling logic. A shared resource pool primitive lets every
@agent-tools/*package that manages connections delegate lifecycle, health checks, and backpressure to one battle-tested implementation.Researched libraries:
Proposed API
Scope
In scope:
use()convenience wrapper with auto-releaseOut of scope:
@agent-tools/databaseor@agent-tools/sql)