2013-02-20 09:34:28 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
|
2013-02-16 06:13:28 +08:00
|
|
|
var defaults = require(__dirname + '/defaults');
|
|
|
|
var genericPool = require('generic-pool');
|
|
|
|
|
2013-02-21 06:08:48 +08:00
|
|
|
var pools = {
|
|
|
|
//dictionary of all key:pool pairs
|
|
|
|
all: {},
|
|
|
|
//reference to the client constructor - can override in tests or for require('pg').native
|
|
|
|
Client: require(__dirname + '/client'),
|
|
|
|
getOrCreate: function(clientConfig) {
|
|
|
|
clientConfig = clientConfig || {};
|
|
|
|
var name = JSON.stringify(clientConfig);
|
|
|
|
var pool = pools.all[name];
|
|
|
|
if(pool) {
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
pool = genericPool.Pool({
|
|
|
|
name: name,
|
|
|
|
max: defaults.poolSize,
|
|
|
|
idleTimeoutMillis: defaults.poolIdleTimeout,
|
|
|
|
reapIntervalMillis: defaults.reapIntervalMillis,
|
|
|
|
log: defaults.poolLog,
|
|
|
|
create: function(cb) {
|
|
|
|
var client = new pools.Client(clientConfig);
|
|
|
|
client.connect(function(err) {
|
|
|
|
if(err) return cb(err, null);
|
2013-02-20 09:34:28 +08:00
|
|
|
|
2013-02-21 06:08:48 +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) {
|
|
|
|
pool.emit('error', e, client);
|
|
|
|
pool.destroy(client);
|
|
|
|
});
|
2013-02-20 09:34:28 +08:00
|
|
|
|
2013-02-21 06:08:48 +08:00
|
|
|
return cb(null, client);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
destroy: function(client) {
|
|
|
|
client.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
pools.all[name] = pool;
|
|
|
|
//mixin EventEmitter to pool
|
|
|
|
EventEmitter.call(pool);
|
|
|
|
for(var key in EventEmitter.prototype) {
|
|
|
|
if(EventEmitter.prototype.hasOwnProperty(key)) {
|
|
|
|
pool[key] = EventEmitter.prototype[key];
|
|
|
|
}
|
2013-02-20 09:34:28 +08:00
|
|
|
}
|
2013-02-21 06:08:48 +08:00
|
|
|
//monkey-patch with connect method
|
|
|
|
pool.connect = function(cb) {
|
2014-03-16 05:40:45 +08:00
|
|
|
var domain = process.domain;
|
2013-02-21 06:08:48 +08:00
|
|
|
pool.acquire(function(err, client) {
|
2014-03-16 05:40:45 +08:00
|
|
|
if(domain) {
|
|
|
|
cb = domain.bind(cb);
|
|
|
|
domain.add(client);
|
|
|
|
//native clients do not have a connection object
|
|
|
|
if(client.connection) {
|
|
|
|
domain.add(client.connection);
|
|
|
|
domain.add(client.connection.stream);
|
|
|
|
}
|
|
|
|
}
|
2013-02-21 06:08:48 +08:00
|
|
|
if(err) return cb(err, null, function() {/*NOOP*/});
|
2013-03-08 05:57:00 +08:00
|
|
|
cb(null, client, function(err) {
|
2014-03-16 05:40:45 +08:00
|
|
|
if(domain) {
|
|
|
|
//native clients do not have a connection object
|
|
|
|
if(client.connection) {
|
|
|
|
domain.remove(client.connection.stream);
|
|
|
|
domain.remove(client.connection);
|
|
|
|
}
|
|
|
|
domain.remove(client);
|
|
|
|
}
|
2013-03-08 05:57:00 +08:00
|
|
|
if(err) {
|
|
|
|
pool.destroy(client);
|
|
|
|
} else {
|
|
|
|
pool.release(client);
|
|
|
|
}
|
|
|
|
});
|
2013-02-21 06:08:48 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
return pool;
|
2013-02-20 09:34:28 +08:00
|
|
|
}
|
|
|
|
};
|
2013-02-16 06:13:28 +08:00
|
|
|
|
2013-02-21 06:08:48 +08:00
|
|
|
module.exports = pools;
|