node-pool icon indicating copy to clipboard operation
node-pool copied to clipboard

Is it possible to have a callback when the minimum number of connections is created?

Open abh1kg opened this issue 10 years ago • 5 comments

Pretty cool module, I must say. I am using it for a project of mine and I couldn't find out whether it's really possible or not. Can the pool call a custom callback when the minimum number of connections are created?

Example: I am using the following piece of code to create the pool. Would it be possible to have a callback executed when the minimum number of connections (in this case, 3) is created?

var pool = gp.Pool({
        name : 'connection_pool',
        validate : function(client) {
            console.log('INFO- Validate called:' + (!client.hadError && client.readyState === 'connected'));
            return (!client.hadError && client.readyState === 'connected');
        },
        destroy : function(client) {
            // if the client is already closed don't call end
            if (!client.hadError && client.readyState !== 'closed') {
                client.end();
            }
        },
        max : 25,
        min : 3,
        idleTimeoutMillis : 30000,
        log : false,
        refreshIdle : true,
        create : function(callback) {
            options.autoReconnect = true;
            var client = hdb.createClient(options);
            client.hadError = false;
            client.once('error', function onerror(err) {
                console.error('Client error:', err);
                client.hadError = true;
            });
            client.connect(function(err) {
                if (err) {
                    return console.error("Error:" + err);
                }
            });
            callback(null, client);
        }
    });

abh1kg avatar Apr 23 '15 03:04 abh1kg

Currently it's not possible at the moment although in theory it could be added. Whats your use case for it?

sandfox avatar Apr 23 '15 07:04 sandfox

@sandfox : I am building a service on Cloud Foundry PaaS, which initializes the pool with the minimum number of connections. The service is an HTTP component, which uses the pool for connection resource handling. I want to make it certain that unless the minimum connections have been created, HTTP calls must be blocked (or a maintenance error is returned). Without a callback, I am currently not finding a way to do this.

abh1kg avatar Apr 23 '15 08:04 abh1kg

Sounds like a good reason. At the moment the only ways to achieve that (that I can think of) without library changes is to poll the pool and check pool.getPoolSize() to see how "full" it is. It's not exactly a clean solution though and has plenty of room for being needlessly slow.

Alternatively (and I think this is the better option) you could add some code inside your create function that did something like incrementing a counter for you and once that counter crossed a threshold (the min pool size), you could then do some other action (e.g call a callback to allow your http server to start accepting connections).

Otherwise it's going to mean adding some event emitters / callbacks to the pool for events like "resource created" etc, but I'm not sure I'm going to have time to do that in the near future.

sandfox avatar Apr 23 '15 14:04 sandfox

@sandfox: The counter-increment code inside the 'create' function is how I am doing this now. Probably a good way to achieve this but a callback would certainly be smoother.

abh1kg avatar Apr 23 '15 14:04 abh1kg

4 years later, it seems like this issue could be relatively easily solved by adding a custom Event for this? The library is EventEmitter-enabled right now and an an .emit near the minimum regulating code seems like all it would take?

t3hmrman avatar Apr 02 '19 16:04 t3hmrman