use client#connect callback in pg.connect connection error handling
This commit is contained in:
parent
66328cc6cb
commit
4d482e451b
20
lib/index.js
20
lib/index.js
@ -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);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user