coopernurse / node-pool

Generic resource pooling for node.js
2.38k stars 259 forks source link

Resource not currently part of this pool #293

Open 189 opened 2 years ago

189 commented 2 years ago

I make 20 request after create connection pool with 10 max connection, but always got “Resource not currently part of this pool”。Here is my all code:

const genericPool = require("generic-pool");

const factory = {
  create: function () {
    console.log("factory create");
    return null;
  },
  destroy: function () {
    console.log("====> factory destroy");
    return true;
  }
};

const pool = genericPool.createPool(factory, {
  max: 10, // maximum size of the pool
  idleTimeoutMillis: 30000,
  min: 2
});

function fetch(idx) {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log(idx, "done");
      resolve();
    }, 1 * 1000);
  });
}

let n = 0;

while (n < 20) {
  ((m) => {
    pool
      .acquire()
      .then((client) => {
        return fetch(m).then(() => {
          console.log(m, "release");
          return pool.release(client);
        });
      })
      .catch(function (err) {
        console.log("err=>", err);
      });
  })(n);
  n++;
}

pool.drain().then(function () {
  pool.clear();
});

image

"generic-pool": "^3.8.2"

Here is online demo

eliellis commented 2 years ago

Works fine for me if the pool create function returns something other than null, like an empty object. See here. The implementation compares unique instances internally, and null is not a unique instance when calling release, which is which is why you are seeing this error.