coopernurse / node-pool

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

About the acquire function in pool.js #212

Open dbxiaoqiang opened 6 years ago

dbxiaoqiang commented 6 years ago

hello, I have a question about these codes. pool.js

acquire(priority) {
    if (this._started === false && this._config.autostart === true) {
            this.start()
    }
    if (this._draining) {
            return this._Promise.reject(
                new Error('pool is draining and cannot accept work')
            )
     }
}

In this function the pool is started but if the pool had been drained the pool's status is still drained Perhaps it will be a issue. I think if the pool has been drained then it will never be acquired.

sandfox commented 6 years ago

inside start() there is check to stop a "draining" pool from "starting" again https://github.com/coopernurse/node-pool/blob/master/lib/Pool.js#L388:

  start() {
    if (this._draining === true) {
      return;
    }
    if (this._started === true) {
      return;
    }
    this._started = true;
    this._scheduleEvictorRun();
    this._ensureMinimum();
}

so this means in acquire() it will try to start the pool (but not succed), and then the next if statement will cause the method to return a rejected promise.

Does that make sense?

(I have to admit that these internals functions are a little messy and could have better internal interfaces)