2013-02-20 09:34:28 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
|
2015-10-10 17:10:51 +08:00
|
|
|
var defaults = require('./defaults');
|
2013-02-16 06:13:28 +08:00
|
|
|
var genericPool = require('generic-pool');
|
|
|
|
|
2013-02-20 09:34:28 +08:00
|
|
|
|
2016-04-09 07:46:10 +08:00
|
|
|
module.exports = function(Client) {
|
|
|
|
var pools = {
|
2016-04-27 06:41:11 +08:00
|
|
|
Client: Client,
|
2016-04-09 07:46:10 +08:00
|
|
|
//dictionary of all key:pool pairs
|
|
|
|
all: {},
|
|
|
|
//reference to the client constructor - can override in tests or for require('pg').native
|
|
|
|
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: clientConfig.poolSize || defaults.poolSize,
|
|
|
|
idleTimeoutMillis: clientConfig.poolIdleTimeout || defaults.poolIdleTimeout,
|
|
|
|
reapIntervalMillis: clientConfig.reapIntervalMillis || defaults.reapIntervalMillis,
|
|
|
|
log: clientConfig.poolLog || defaults.poolLog,
|
|
|
|
create: function(cb) {
|
2016-04-27 06:41:11 +08:00
|
|
|
var client = new pools.Client(clientConfig);
|
2016-04-09 07:46:10 +08:00
|
|
|
// Ignore errors on pooled clients until they are connected.
|
|
|
|
client.on('error', Function.prototype);
|
|
|
|
client.connect(function(err) {
|
|
|
|
if(err) return cb(err, null);
|
2015-08-24 22:18:43 +08:00
|
|
|
|
2016-04-09 07:46:10 +08:00
|
|
|
// Remove the noop error handler after a connection has been established.
|
|
|
|
client.removeListener('error', Function.prototype);
|
2014-07-30 20:17:04 +08:00
|
|
|
|
2016-04-09 07:46:10 +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);
|
2013-02-20 09:34:28 +08:00
|
|
|
|
2016-04-09 07:46:10 +08:00
|
|
|
// If the client is already being destroyed, the error
|
|
|
|
// occurred during stream ending. Do not attempt to destroy
|
|
|
|
// the client again.
|
|
|
|
if (!client._destroying) {
|
|
|
|
pool.destroy(client);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove connection from pool on disconnect
|
|
|
|
client.on('end', function(e) {
|
|
|
|
// Do not enter infinite loop between pool.destroy
|
|
|
|
// and client 'end' event...
|
|
|
|
if ( ! client._destroying ) {
|
|
|
|
pool.destroy(client);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
client.poolCount = 0;
|
|
|
|
return cb(null, client);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
destroy: function(client) {
|
|
|
|
client._destroying = true;
|
|
|
|
client.poolCount = undefined;
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//monkey-patch with connect method
|
|
|
|
pool.connect = function(cb) {
|
|
|
|
var domain = process.domain;
|
|
|
|
pool.acquire(function(err, client) {
|
|
|
|
if(domain) {
|
|
|
|
cb = domain.bind(cb);
|
|
|
|
}
|
|
|
|
if(err) return cb(err, null, function() {/*NOOP*/});
|
|
|
|
client.poolCount++;
|
|
|
|
cb(null, client, function(err) {
|
|
|
|
if(err) {
|
2014-03-18 16:42:19 +08:00
|
|
|
pool.destroy(client);
|
2016-04-09 07:46:10 +08:00
|
|
|
} else {
|
|
|
|
pool.release(client);
|
2014-03-18 16:42:19 +08:00
|
|
|
}
|
2014-03-18 01:51:32 +08:00
|
|
|
});
|
2013-02-21 06:08:48 +08:00
|
|
|
});
|
2016-04-09 07:46:10 +08:00
|
|
|
};
|
|
|
|
return pool;
|
2013-02-20 09:34:28 +08:00
|
|
|
}
|
2016-04-09 07:46:10 +08:00
|
|
|
};
|
2013-02-16 06:13:28 +08:00
|
|
|
|
2016-04-09 07:46:10 +08:00
|
|
|
return pools;
|
|
|
|
};
|