coopernurse / node-pool

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

Borrowed resources never go higher than 1 #285

Open eunito opened 3 years ago

eunito commented 3 years ago

Hi! I'm using the generic-pool and node '.net' in nodeJs to create a socket pool. These are the factory methods I'm using:

 create: () => {
        return new Promise((resolve, reject) => {
            const client = net.createConnection(port, ip, () => {
                this.sendCommand(client, `authenticate ${pwd}`); 
            });

            client.on('error', (err) => {
                try {
                    this.sendCommand(client, 'exit');
                } catch (error) {
                    reject(error);
                }

                reject(err);
            });

            client.on('end', () => { 
                console.log('Socket terminated.'); 
            });

            client.on('ready', () => {
                resolve(client);
            });
        })
    },

        destroy: (client) => {
            return new Promise((resolve, reject) => {

            try {
                this.sendCommand(client, 'exit');
                resolve();
            } catch (error) {
                reject(error);
            }
        })
    },
    validate: (client) => {
        return new Promise((resolve) => {
            if (client.destroyed || !client.readable || !client.writable) {
                return resolve(false);
            } else {
                return resolve(true);
            }
        });
    }

and these are the options I'm using:

"poolOptions": {
      "max": 20,
      "min": 5,
      "testOnBorrow": true,
      "idleTimeoutMillis": 30000,
      "acquireTimeoutMillis": 1000,
      "evictionRunIntervalMillis": 14400000,
      "numTestsPerEvictionRun": 3,
    }

When I try to acquire an client from the pool using several requests at once I allway get the feeling that only one of them is being borrowed while the others should also be used if (when acquiring)... I keep getting:

"spareResourceCapacity": 15,
"size": 5,
"available": 4,
"borrowed": 1, // this is the only value that changes from 1 <-> 0
"pending": 0,
"max": 15,
"min": 5

Is it a bug or is there any validation to perform on a new resource acquiring? I simple acquire a resource from the pool await socketPool.acquire(); and then release/destroy it after usage.

nicmesan2 commented 1 year ago

Any updates on this? I am facing the same issue.

solitud commented 10 months ago

Did you find a fix? I think I have the same problem using generic-pool 3.9.0 and puppeteer.