{"library":"pool2","title":"Pool2","description":"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install pool2"],"cli":null},"imports":["const Pool = require('pool2');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const Pool = require('pool2');\n\n// Simulate a resource that takes time to acquire and needs disposal\nlet resourceCounter = 0;\n\nfunction createResource(id) {\n  return {\n    id: id,\n    status: 'open',\n    connect: () => new Promise(resolve => setTimeout(() => {\n      console.log(`Resource ${id} connected.`);\n      resolve();\n    }, 100)),\n    close: () => new Promise(resolve => setTimeout(() => {\n      console.log(`Resource ${id} closed.`);\n      resolve();\n    }, 50)),\n    destroy: () => {\n      console.log(`Resource ${id} forcibly destroyed.`);\n    }\n  };\n}\n\nconst pool = new Pool({\n  acquire: function (cb) {\n    const id = ++resourceCounter;\n    const rsrc = createResource(id);\n    rsrc.connect()\n      .then(() => cb(null, rsrc))\n      .catch(err => cb(err));\n  },\n  acquireTimeout: 5000,\n  dispose: function (rsrc, cb) {\n    rsrc.close()\n      .then(() => cb())\n      .catch(err => cb(err));\n  },\n  disposeTimeout: 2000,\n  destroy: function (rsrc) {\n    if (rsrc && rsrc.status === 'open') {\n      rsrc.destroy();\n    }\n  },\n  ping: function (rsrc, cb) {\n    // Simple ping, assume resource is always alive for this example\n    cb();\n  },\n  min: 1,\n  max: 3,\n  idleTimeout: 3000,\n  syncInterval: 1000\n});\n\nasync function usePool() {\n  console.log('Acquiring resource...');\n  let resource;\n  try {\n    resource = await new Promise((resolve, reject) => {\n      pool.acquire((err, r) => {\n        if (err) return reject(err);\n        resolve(r);\n      });\n    });\n    console.log(`Successfully acquired resource ${resource.id}. Doing work...`);\n    await new Promise(resolve => setTimeout(resolve, 500)); // Simulate work\n  } catch (err) {\n    console.error('Failed to acquire resource:', err.message);\n    return;\n  } finally {\n    if (resource) {\n      console.log(`Releasing resource ${resource.id}.`);\n      pool.release(resource);\n    }\n  }\n  console.log('Current pool stats:', pool.stats());\n}\n\n(async () => {\n  await usePool();\n  await usePool();\n  await usePool();\n\n  console.log('Ending pool in 5 seconds...');\n  setTimeout(() => {\n    pool.end((errs) => {\n      if (errs && errs.length > 0) {\n        console.error('Errors during pool end:', errs);\n      } else {\n        console.log('Pool ended gracefully.');\n      }\n    });\n  }, 5000);\n})();","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}