2010-09-29 12:18:46 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2011-10-11 08:40:52 +08:00
|
|
|
var util = require('util');
|
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-30 11:48:17 +08:00
|
|
|
|
|
|
|
//external genericPool module
|
2011-08-12 09:59:56 +08:00
|
|
|
var genericPool = require('generic-pool');
|
|
|
|
|
|
|
|
//cache of existing client pools
|
|
|
|
var pools = {};
|
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
var PG = function(clientConstructor) {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
this.Client = clientConstructor;
|
|
|
|
this.Connection = require(__dirname + '/connection');
|
2013-01-21 21:52:00 +08:00
|
|
|
this.Query = clientConstructor.Query;
|
2011-08-30 11:48:17 +08:00
|
|
|
this.defaults = defaults;
|
|
|
|
};
|
|
|
|
|
2011-10-11 08:40:52 +08:00
|
|
|
util.inherits(PG, EventEmitter);
|
2011-08-12 09:59:56 +08:00
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
PG.prototype.end = function() {
|
2011-08-12 09:59:56 +08:00
|
|
|
Object.keys(pools).forEach(function(name) {
|
|
|
|
var pool = pools[name];
|
|
|
|
pool.drain(function() {
|
|
|
|
pool.destroyAllNow();
|
|
|
|
});
|
2013-01-21 21:52:00 +08:00
|
|
|
});
|
|
|
|
};
|
2011-03-12 02:29:27 +08:00
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
PG.prototype.connect = function(config, callback) {
|
|
|
|
var self = this;
|
|
|
|
var c = config;
|
|
|
|
var cb = callback;
|
|
|
|
//allow for no config to be passed
|
|
|
|
if(typeof c === 'function') {
|
|
|
|
cb = c;
|
|
|
|
c = defaults;
|
|
|
|
}
|
2011-08-29 15:35:46 +08:00
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
//get unique pool name even if object was used as config
|
|
|
|
var poolName = typeof(c) === 'string' ? c : c.user+c.host+c.port+c.database;
|
|
|
|
var pool = pools[poolName];
|
|
|
|
|
2013-01-21 21:52:00 +08:00
|
|
|
if(pool) { return pool.acquire(cb); }
|
2011-08-30 11:48:17 +08:00
|
|
|
|
2013-01-21 21:52:00 +08:00
|
|
|
pool = pools[poolName] = genericPool.Pool({
|
2011-08-30 11:48:17 +08:00
|
|
|
name: poolName,
|
|
|
|
create: function(callback) {
|
|
|
|
var client = new self.Client(c);
|
2012-07-06 11:37:34 +08:00
|
|
|
client.connect(function(err) {
|
2013-01-21 21:52:00 +08:00
|
|
|
if(err) { return callback(err); }
|
2013-01-24 07:45:40 +08:00
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
//handle connected client background errors by emitting event
|
|
|
|
//via the pg object and then removing errored client from the pool
|
|
|
|
client.on('error', function(e) {
|
|
|
|
self.emit('error', e, client);
|
|
|
|
pool.destroy(client);
|
|
|
|
});
|
2012-07-06 11:37:34 +08:00
|
|
|
|
2011-08-30 11:48:17 +08:00
|
|
|
callback(null, client);
|
2012-07-06 11:37:34 +08:00
|
|
|
});
|
2011-08-30 11:48:17 +08:00
|
|
|
|
|
|
|
client.on('drain', function() {
|
|
|
|
pool.release(client);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
destroy: function(client) {
|
|
|
|
client.end();
|
|
|
|
},
|
|
|
|
max: defaults.poolSize,
|
2012-02-01 19:34:34 +08:00
|
|
|
idleTimeoutMillis: defaults.poolIdleTimeout,
|
2012-07-16 11:08:26 +08:00
|
|
|
reapIntervalMillis: defaults.reapIntervalMillis,
|
|
|
|
log: defaults.poolLog
|
2011-08-30 11:48:17 +08:00
|
|
|
});
|
|
|
|
return pool.acquire(cb);
|
2013-01-21 21:52:00 +08:00
|
|
|
};
|
2011-08-29 15:35:46 +08:00
|
|
|
|
2011-11-02 23:07:14 +08:00
|
|
|
// cancel the query runned by the given client
|
|
|
|
PG.prototype.cancel = function(config, client, query) {
|
|
|
|
var c = config;
|
|
|
|
//allow for no config to be passed
|
2013-01-21 21:52:00 +08:00
|
|
|
if(typeof c === 'function') {
|
2011-11-02 23:07:14 +08:00
|
|
|
c = defaults;
|
2013-01-21 21:52:00 +08:00
|
|
|
}
|
2011-11-02 23:07:14 +08:00
|
|
|
var cancellingClient = new this.Client(c);
|
|
|
|
cancellingClient.cancel(client, query);
|
2013-01-21 21:52:00 +08:00
|
|
|
};
|
2011-11-02 23:07:14 +08:00
|
|
|
|
2011-08-29 15:35:46 +08:00
|
|
|
module.exports = new PG(Client);
|
2011-03-16 12:00:58 +08:00
|
|
|
|
2013-01-24 07:45:40 +08:00
|
|
|
//lazy require native module...the native module may not have installed
|
2011-03-16 12:00:58 +08:00
|
|
|
module.exports.__defineGetter__("native", function() {
|
2011-08-30 11:48:17 +08:00
|
|
|
delete module.exports.native;
|
2013-01-24 08:29:52 +08:00
|
|
|
module.exports.native = new PG(require(__dirname + '/native'));
|
|
|
|
return module.exports.native;
|
2012-09-19 01:30:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports.types = require('./types');
|