@@ -3,56 +3,61 @@ var EventEmitter = require('events').EventEmitter;
3
3
var defaults = require ( __dirname + '/defaults' ) ;
4
4
var genericPool = require ( 'generic-pool' ) ;
5
5
6
- //takes the same config structure as client
7
- var createPool = function ( clientConfig ) {
8
- clientConfig = clientConfig || { } ;
9
- var name = JSON . stringify ( clientConfig ) ;
10
- var pool = createPool . all [ name ] ;
11
- if ( pool ) {
12
- return pool ;
13
- }
14
- pool = genericPool . Pool ( {
15
- name : name ,
16
- max : defaults . poolSize ,
17
- idleTimeoutMillis : defaults . poolIdleTimeout ,
18
- reapIntervalMillis : defaults . reapIntervalMillis ,
19
- log : defaults . poolLog ,
20
- create : function ( cb ) {
21
- var client = new createPool . Client ( clientConfig ) ;
22
- client . connect ( function ( err ) {
23
- if ( err ) return cb ( err , null ) ;
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 ( __dirname + '/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 : defaults . poolSize ,
21
+ idleTimeoutMillis : defaults . poolIdleTimeout ,
22
+ reapIntervalMillis : defaults . reapIntervalMillis ,
23
+ log : defaults . poolLog ,
24
+ create : function ( cb ) {
25
+ var client = new pools . Client ( clientConfig ) ;
26
+ client . connect ( function ( err ) {
27
+ if ( err ) return cb ( err , null ) ;
24
28
25
- //handle connected client background errors by emitting event
26
- //via the pg object and then removing errored client from the pool
27
- client . on ( 'error' , function ( e ) {
28
- pool . emit ( 'error' , e , client ) ;
29
- pool . destroy ( client ) ;
30
- } ) ;
29
+ //handle connected client background errors by emitting event
30
+ //via the pg object and then removing errored client from the pool
31
+ client . on ( 'error' , function ( e ) {
32
+ pool . emit ( 'error' , e , client ) ;
33
+ pool . destroy ( client ) ;
34
+ } ) ;
31
35
32
- return cb ( null , client ) ;
33
- } ) ;
34
- } ,
35
- destroy : function ( client ) {
36
- client . end ( ) ;
37
- }
38
- } ) ;
39
- createPool . all [ name ] = pool ;
40
- //mixin EventEmitter to pool
41
- EventEmitter . call ( pool ) ;
42
- for ( var key in EventEmitter . prototype ) {
43
- if ( EventEmitter . prototype . hasOwnProperty ( key ) ) {
44
- pool [ key ] = EventEmitter . prototype [ key ] ;
36
+ return cb ( null , client ) ;
37
+ } ) ;
38
+ } ,
39
+ destroy : function ( client ) {
40
+ client . end ( ) ;
41
+ }
42
+ } ) ;
43
+ pools . all [ name ] = pool ;
44
+ //mixin EventEmitter to pool
45
+ EventEmitter . call ( pool ) ;
46
+ for ( var key in EventEmitter . prototype ) {
47
+ if ( EventEmitter . prototype . hasOwnProperty ( key ) ) {
48
+ pool [ key ] = EventEmitter . prototype [ key ] ;
49
+ }
45
50
}
51
+ //monkey-patch with connect method
52
+ pool . connect = function ( cb ) {
53
+ pool . acquire ( function ( err , client ) {
54
+ if ( err ) return cb ( err , null , function ( ) { /*NOOP*/ } ) ;
55
+ //support both 2 (old) and 3 arguments
56
+ ( cb . length > 2 ? newConnect : oldConnect ) ( pool , client , cb ) ;
57
+ } ) ;
58
+ } ;
59
+ return pool ;
46
60
}
47
- //monkey-patch with connect method
48
- pool . connect = function ( cb ) {
49
- pool . acquire ( function ( err , client ) {
50
- if ( err ) return cb ( err , null , function ( ) { /*NOOP*/ } ) ;
51
- //support both 2 (old) and 3 arguments
52
- ( cb . length > 2 ? newConnect : oldConnect ) ( pool , client , cb ) ;
53
- } ) ;
54
- } ;
55
- return pool ;
56
61
} ;
57
62
58
63
//the old connect method of the pool
@@ -62,12 +67,15 @@ var createPool = function(clientConfig) {
62
67
//a bunch of problems, but for backwards compatibility
63
68
//we're leaving it in
64
69
var alarmDuration = 5000 ;
65
- var errorMessage = [ 'A client has been checked out from the pool for longer than ' + alarmDuration + ' ms.' ,
66
- 'You might have a leak!' ,
67
- 'You should use the following new way to check out clients' , 'pg.connect(function(err, client, done)) {' ,
68
- ' //do something' ,
69
- ' done(); //call done() to signal you are finished with the client' ,
70
- '}' ] . join ( require ( 'os' ) . EOL ) ;
70
+ var errorMessage = [
71
+ 'A client has been checked out from the pool for longer than ' + alarmDuration + ' ms.' ,
72
+ 'You might have a leak!' ,
73
+ 'You should use the following new way to check out clients' , 'pg.connect(function(err, client, done)) {' ,
74
+ ' //do something' ,
75
+ ' done(); //call done() to signal you are finished with the client' ,
76
+ '}'
77
+ ] . join ( require ( 'os' ) . EOL ) ;
78
+
71
79
var oldConnect = function ( pool , client , cb ) {
72
80
var tid = setTimeout ( function ( ) {
73
81
console . error ( errorMessage ) ;
@@ -90,10 +98,4 @@ var newConnect = function(pool, client, cb) {
90
98
} ) ;
91
99
} ;
92
100
93
- //list of all created pools
94
- createPool . all = { } ;
95
-
96
- //reference to client constructor
97
- createPool . Client = require ( __dirname + '/client' ) ;
98
-
99
- module . exports = createPool ;
101
+ module . exports = pools ;
0 commit comments