use client#connect callback in pg.connect connection error handling

This commit is contained in:
brianc 2012-07-05 22:37:34 -05:00
parent 66328cc6cb
commit 4d482e451b

View File

@ -47,27 +47,19 @@ PG.prototype.connect = function(config, callback) {
name: poolName,
create: function(callback) {
var client = new self.Client(c);
client.connect();
var connectError = function(err) {
client.removeListener('connect', connectSuccess);
callback(err, null);
};
var connectSuccess = function() {
client.removeListener('error', connectError);
client.connect(function(err) {
if(err) return callback(err);
//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);
});
callback(null, client);
};
client.once('connect', connectSuccess);
client.once('error', connectError);
callback(null, client);
});
client.on('drain', function() {
pool.release(client);
});