2010-09-29 12:18:46 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2010-11-15 13:04:41 +08:00
|
|
|
var Client = require(__dirname+'/client');
|
2010-12-11 07:32:34 +08:00
|
|
|
var defaults = require(__dirname + '/defaults');
|
2011-08-12 09:59:56 +08:00
|
|
|
var genericPool = require('generic-pool');
|
|
|
|
|
|
|
|
//cache of existing client pools
|
|
|
|
var pools = {};
|
|
|
|
|
|
|
|
//returns connect function using supplied client constructor
|
|
|
|
var makeConnectFunction = function(ClientConstructor) {
|
|
|
|
return function(config, callback) {
|
|
|
|
var c = config;
|
|
|
|
var cb = callback;
|
|
|
|
//allow for no config to be passed
|
|
|
|
if(typeof c === 'function') {
|
|
|
|
cb = c;
|
|
|
|
c = defaults;
|
|
|
|
}
|
|
|
|
//get unique pool name if using a config object instead of config string
|
|
|
|
var poolName = typeof(c) === 'string' ? c : c.user+c.host+c.port+c.database;
|
|
|
|
var pool = pools[poolName];
|
|
|
|
if(pool) return pool.acquire(cb);
|
|
|
|
var pool = pools[poolName] = genericPool.Pool({
|
|
|
|
name: poolName,
|
|
|
|
create: function(callback) {
|
2011-08-16 09:15:43 +08:00
|
|
|
var client = new ClientConstructor(c);
|
2011-08-12 09:59:56 +08:00
|
|
|
client.connect();
|
|
|
|
var connectError = function(err) {
|
|
|
|
client.removeListener('connect', connectSuccess);
|
|
|
|
callback(err, null);
|
|
|
|
};
|
|
|
|
var connectSuccess = function() {
|
|
|
|
client.removeListener('error', connectError);
|
|
|
|
callback(null, client);
|
|
|
|
};
|
|
|
|
client.once('connect', connectSuccess);
|
|
|
|
client.once('error', connectError);
|
|
|
|
client.on('drain', function() {
|
|
|
|
pool.release(client);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
destroy: function(client) {
|
|
|
|
client.end();
|
|
|
|
},
|
2011-08-12 10:52:29 +08:00
|
|
|
max: defaults.poolSize,
|
|
|
|
idleTimeoutMillis: defaults.poolIdleTimeout
|
2011-08-12 09:59:56 +08:00
|
|
|
});
|
|
|
|
return pool.acquire(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var end = function() {
|
|
|
|
Object.keys(pools).forEach(function(name) {
|
|
|
|
var pool = pools[name];
|
|
|
|
pool.drain(function() {
|
|
|
|
pool.destroyAllNow();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
};
|
2011-03-12 02:29:27 +08:00
|
|
|
|
2010-09-29 12:18:46 +08:00
|
|
|
module.exports = {
|
2010-11-15 14:10:21 +08:00
|
|
|
Client: Client,
|
2010-11-15 13:04:41 +08:00
|
|
|
Connection: require(__dirname + '/connection'),
|
2011-08-12 09:59:56 +08:00
|
|
|
connect: makeConnectFunction(Client),
|
|
|
|
end: end,
|
2011-03-16 12:00:58 +08:00
|
|
|
defaults: defaults
|
2010-11-15 14:10:21 +08:00
|
|
|
}
|
2011-03-16 12:00:58 +08:00
|
|
|
|
2011-08-12 09:59:56 +08:00
|
|
|
var nativeExport = null;
|
2011-05-02 13:32:30 +08:00
|
|
|
//lazy require native module...the c++ may not have been compiled
|
2011-03-16 12:00:58 +08:00
|
|
|
module.exports.__defineGetter__("native", function() {
|
2011-08-12 09:59:56 +08:00
|
|
|
if(nativeExport === null) {
|
|
|
|
var NativeClient = require(__dirname + '/native');
|
|
|
|
nativeExport = {
|
|
|
|
Client: NativeClient,
|
|
|
|
connect: makeConnectFunction(NativeClient),
|
|
|
|
end: end,
|
|
|
|
defaults: defaults
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nativeExport;
|
2011-03-16 12:00:58 +08:00
|
|
|
})
|