Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

acquire() never resolves/rejects if factory always rejects #183

Open
Aleksandras-Novikovas opened this issue Mar 10, 2017 · 0 comments
Open
Assignees

Comments

@Aleksandras-Novikovas
Copy link

Aleksandras-Novikovas commented Mar 10, 2017

Example: your network is down and your connection factory always rejects. Trying to acquire never resolved/rejected - pool goes into loop and never stops.
Here code with factory which always fails:

class BadResourceFactory {
    create() {
        return new Promise(function(resolve, reject) {
            var resource = {
                res: "Resource"
            };
            log.info("Created", resource);
//            resolve(resource);
            reject(new Error("Creation error"));
        });
    }
    destroy(resource) {
        return new Promise(function(resolve) {
            log.info("Destroyed", resource);
            resolve();
        });
    }
    validate(resource) {
        return new Promise(function(resolve) {
            log.info("Validated", resource);
            resolve(true);
        });
    }
}

pool.on('factoryCreateError', function(err){
    log.error({err: err}, "Factory create error");
});

var pool = genericPool.createPool(new BadResourceFactory(), {
    min: 1,
    max: 10
});

var res = pool.acquire();

res.then(function(res) {
    log.info({res: res}, "Got resource");
}).catch(function(err) {
    log.info({err: err}, "NO resource");
});

My suggested solution would be:
add option maxTries which would define number of consequent failures before pool stops trying.
Internal counter should be restarted on each acquire() call.

I will clean up my test code and will open pull request.

Regards

EDIT: pull request is added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants