11var EventEmitter = require ( 'events' ) . EventEmitter ;
2+ var sys = require ( 'sys' ) ;
23var Client = require ( __dirname + '/client' ) ;
34var defaults = require ( __dirname + '/defaults' ) ;
45var genericPool = require ( 'generic-pool' ) ;
@@ -7,7 +8,8 @@ var genericPool = require('generic-pool');
78var pools = { } ;
89
910//returns connect function using supplied client constructor
10- var makeConnectFunction = function ( ClientConstructor ) {
11+ var makeConnectFunction = function ( pg ) {
12+ var ClientConstructor = pg . Client ;
1113 return function ( config , callback ) {
1214 var c = config ;
1315 var cb = callback ;
@@ -31,6 +33,13 @@ var makeConnectFunction = function(ClientConstructor) {
3133 } ;
3234 var connectSuccess = function ( ) {
3335 client . removeListener ( 'error' , connectError ) ;
36+
37+ //handle connected client background errors by emitting event
38+ //via the pg object and then removing errored client from the pool
39+ client . on ( 'error' , function ( e ) {
40+ pg . emit ( 'error' , e , client ) ;
41+ pool . destroy ( client ) ;
42+ } ) ;
3443 callback ( null , client ) ;
3544 } ;
3645 client . once ( 'connect' , connectSuccess ) ;
@@ -58,25 +67,25 @@ var end = function() {
5867 } )
5968} ;
6069
61- module . exports = {
62- Client : Client ,
63- Connection : require ( __dirname + '/connection' ) ,
64- connect : makeConnectFunction ( Client ) ,
65- end : end ,
66- defaults : defaults
67- }
70+ var PG = function ( clientConstructor ) {
71+ EventEmitter . call ( this ) ;
72+ this . Client = clientConstructor ;
73+ this . Connection = require ( __dirname + '/connection' ) ;
74+ this . connect = makeConnectFunction ( this ) ;
75+ this . end = end ;
76+ this . defaults = defaults ;
77+ } ;
78+
79+ sys . inherits ( PG , EventEmitter ) ;
80+
81+ module . exports = new PG ( Client ) ;
6882
6983var nativeExport = null ;
7084//lazy require native module...the c++ may not have been compiled
7185module . exports . __defineGetter__ ( "native" , function ( ) {
7286 if ( nativeExport === null ) {
7387 var NativeClient = require ( __dirname + '/native' ) ;
74- nativeExport = {
75- Client : NativeClient ,
76- connect : makeConnectFunction ( NativeClient ) ,
77- end : end ,
78- defaults : defaults
79- }
88+ nativeExport = new PG ( NativeClient ) ;
8089 }
8190 return nativeExport ;
8291} )
0 commit comments