Skip to content

Commit 4d482e4

Browse files
committed
use client#connect callback in pg.connect connection error handling
1 parent 66328cc commit 4d482e4

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

lib/index.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,19 @@ PG.prototype.connect = function(config, callback) {
4747
name: poolName,
4848
create: function(callback) {
4949
var client = new self.Client(c);
50-
client.connect();
51-
52-
var connectError = function(err) {
53-
client.removeListener('connect', connectSuccess);
54-
callback(err, null);
55-
};
56-
57-
var connectSuccess = function() {
58-
client.removeListener('error', connectError);
59-
50+
client.connect(function(err) {
51+
if(err) return callback(err);
52+
6053
//handle connected client background errors by emitting event
6154
//via the pg object and then removing errored client from the pool
6255
client.on('error', function(e) {
6356
self.emit('error', e, client);
6457
pool.destroy(client);
6558
});
59+
6660
callback(null, client);
67-
};
61+
});
6862

69-
client.once('connect', connectSuccess);
70-
client.once('error', connectError);
7163
client.on('drain', function() {
7264
pool.release(client);
7365
});

0 commit comments

Comments
 (0)