@@ -3,93 +3,95 @@ var EventEmitter = require('events').EventEmitter;
3
3
var defaults = require ( './defaults' ) ;
4
4
var genericPool = require ( 'generic-pool' ) ;
5
5
6
- var pools = {
7
- //dictionary of all key:pool pairs
8
- all : { } ,
9
- //reference to the client constructor - can override in tests or for require('pg').native
10
- Client : require ( './client' ) ,
11
- getOrCreate : function ( clientConfig ) {
12
- clientConfig = clientConfig || { } ;
13
- var name = JSON . stringify ( clientConfig ) ;
14
- var pool = pools . all [ name ] ;
15
- if ( pool ) {
16
- return pool ;
17
- }
18
- pool = genericPool . Pool ( {
19
- name : name ,
20
- max : clientConfig . poolSize || defaults . poolSize ,
21
- idleTimeoutMillis : clientConfig . poolIdleTimeout || defaults . poolIdleTimeout ,
22
- reapIntervalMillis : clientConfig . reapIntervalMillis || defaults . reapIntervalMillis ,
23
- log : clientConfig . poolLog || defaults . poolLog ,
24
- create : function ( cb ) {
25
- var client = new pools . Client ( clientConfig ) ;
26
- // Ignore errors on pooled clients until they are connected.
27
- client . on ( 'error' , Function . prototype ) ;
28
- client . connect ( function ( err ) {
29
- if ( err ) return cb ( err , null ) ;
30
6
31
- // Remove the noop error handler after a connection has been established.
32
- client . removeListener ( 'error' , Function . prototype ) ;
7
+ module . exports = function ( Client ) {
8
+ var pools = {
9
+ //dictionary of all key:pool pairs
10
+ all : { } ,
11
+ //reference to the client constructor - can override in tests or for require('pg').native
12
+ getOrCreate : function ( clientConfig ) {
13
+ clientConfig = clientConfig || { } ;
14
+ var name = JSON . stringify ( clientConfig ) ;
15
+ var pool = pools . all [ name ] ;
16
+ if ( pool ) {
17
+ return pool ;
18
+ }
19
+ pool = genericPool . Pool ( {
20
+ name : name ,
21
+ max : clientConfig . poolSize || defaults . poolSize ,
22
+ idleTimeoutMillis : clientConfig . poolIdleTimeout || defaults . poolIdleTimeout ,
23
+ reapIntervalMillis : clientConfig . reapIntervalMillis || defaults . reapIntervalMillis ,
24
+ log : clientConfig . poolLog || defaults . poolLog ,
25
+ create : function ( cb ) {
26
+ var client = new Client ( clientConfig ) ;
27
+ // Ignore errors on pooled clients until they are connected.
28
+ client . on ( 'error' , Function . prototype ) ;
29
+ client . connect ( function ( err ) {
30
+ if ( err ) return cb ( err , null ) ;
33
31
34
- //handle connected client background errors by emitting event
35
- //via the pg object and then removing errored client from the pool
36
- client . on ( 'error' , function ( e ) {
37
- pool . emit ( 'error' , e , client ) ;
32
+ // Remove the noop error handler after a connection has been established.
33
+ client . removeListener ( 'error' , Function . prototype ) ;
38
34
39
- // If the client is already being destroyed, the error
40
- // occurred during stream ending. Do not attempt to destroy
41
- // the client again.
42
- if ( ! client . _destroying ) {
43
- pool . destroy ( client ) ;
44
- }
45
- } ) ;
35
+ //handle connected client background errors by emitting event
36
+ //via the pg object and then removing errored client from the pool
37
+ client . on ( 'error' , function ( e ) {
38
+ pool . emit ( 'error' , e , client ) ;
46
39
47
- // Remove connection from pool on disconnect
48
- client . on ( 'end' , function ( e ) {
49
- // Do not enter infinite loop between pool.destroy
50
- // and client 'end' event...
51
- if ( ! client . _destroying ) {
40
+ // If the client is already being destroyed, the error
41
+ // occurred during stream ending. Do not attempt to destroy
42
+ // the client again.
43
+ if ( ! client . _destroying ) {
44
+ pool . destroy ( client ) ;
45
+ }
46
+ } ) ;
47
+
48
+ // Remove connection from pool on disconnect
49
+ client . on ( 'end' , function ( e ) {
50
+ // Do not enter infinite loop between pool.destroy
51
+ // and client 'end' event...
52
+ if ( ! client . _destroying ) {
53
+ pool . destroy ( client ) ;
54
+ }
55
+ } ) ;
56
+ client . poolCount = 0 ;
57
+ return cb ( null , client ) ;
58
+ } ) ;
59
+ } ,
60
+ destroy : function ( client ) {
61
+ client . _destroying = true ;
62
+ client . poolCount = undefined ;
63
+ client . end ( ) ;
64
+ }
65
+ } ) ;
66
+ pools . all [ name ] = pool ;
67
+ //mixin EventEmitter to pool
68
+ EventEmitter . call ( pool ) ;
69
+ for ( var key in EventEmitter . prototype ) {
70
+ if ( EventEmitter . prototype . hasOwnProperty ( key ) ) {
71
+ pool [ key ] = EventEmitter . prototype [ key ] ;
72
+ }
73
+ }
74
+ //monkey-patch with connect method
75
+ pool . connect = function ( cb ) {
76
+ var domain = process . domain ;
77
+ pool . acquire ( function ( err , client ) {
78
+ if ( domain ) {
79
+ cb = domain . bind ( cb ) ;
80
+ }
81
+ if ( err ) return cb ( err , null , function ( ) { /*NOOP*/ } ) ;
82
+ client . poolCount ++ ;
83
+ cb ( null , client , function ( err ) {
84
+ if ( err ) {
52
85
pool . destroy ( client ) ;
86
+ } else {
87
+ pool . release ( client ) ;
53
88
}
54
89
} ) ;
55
- client . poolCount = 0 ;
56
- return cb ( null , client ) ;
57
90
} ) ;
58
- } ,
59
- destroy : function ( client ) {
60
- client . _destroying = true ;
61
- client . poolCount = undefined ;
62
- client . end ( ) ;
63
- }
64
- } ) ;
65
- pools . all [ name ] = pool ;
66
- //mixin EventEmitter to pool
67
- EventEmitter . call ( pool ) ;
68
- for ( var key in EventEmitter . prototype ) {
69
- if ( EventEmitter . prototype . hasOwnProperty ( key ) ) {
70
- pool [ key ] = EventEmitter . prototype [ key ] ;
71
- }
91
+ } ;
92
+ return pool ;
72
93
}
73
- //monkey-patch with connect method
74
- pool . connect = function ( cb ) {
75
- var domain = process . domain ;
76
- pool . acquire ( function ( err , client ) {
77
- if ( domain ) {
78
- cb = domain . bind ( cb ) ;
79
- }
80
- if ( err ) return cb ( err , null , function ( ) { /*NOOP*/ } ) ;
81
- client . poolCount ++ ;
82
- cb ( null , client , function ( err ) {
83
- if ( err ) {
84
- pool . destroy ( client ) ;
85
- } else {
86
- pool . release ( client ) ;
87
- }
88
- } ) ;
89
- } ) ;
90
- } ;
91
- return pool ;
92
- }
93
- } ;
94
+ } ;
94
95
95
- module . exports = pools ;
96
+ return pools ;
97
+ } ;
0 commit comments