coopernurse / node-pool

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

One pool for multiple different database #202

Closed malikimprowised closed 7 years ago

malikimprowised commented 7 years ago

Is it possible to have one pool that include multiple database replicas which has different host and port and divert the query among them using some sort of algorithm?

sandfox commented 7 years ago

Currently, no. I think I have seen something like the before in resource pool libs in other languages. What sort of (external) API would you imagine this having? I'm not against adding some support for this.

richraid21 commented 7 years ago

When inserting connections into the pool, allowing for a type system would be nice. Then, when you need a connection, you could pass in an optional filter and you would get a connection that matches.

Possible example Creating

var pool = new Pool([{
    name     : 'mysql-master',
    type: 'write',
    create   : function(callback) {
        var c = mysql.createConnection({
                //....
        })
        callback(null, c);
    },
   name     : 'mysql-replica',
    type: 'read',
    create   : function(callback) {
        var c = mysql.createConnection({
                //....
        })
        callback(null, c);
    },
],   //...
});

Acquiring

pool.acquire({ type: 'write'}, function(err, client) {
    //....
});

Something like that

sandfox commented 7 years ago

This seems like a lot of complexity compared to:

const readPool = new Pool({/* .. opts... /*})
const writePool = new Pool({/* .. opts... /*}

and I'm not sure what advantages it would give?

richraid21 commented 7 years ago

Yea you're probably right. Best not to over complicate things!

sandfox commented 7 years ago

If such an API / functionality was useful to you/anyone I think it would be better done as wrapper/proxy/thing/lib/module thing infront of generic-pool, and doing so would probably make it pretty simple?