@@ -3,56 +3,61 @@ var EventEmitter = require('events').EventEmitter;
33var defaults = require ( __dirname + '/defaults' ) ;
44var genericPool = require ( 'generic-pool' ) ;
55
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 ) ;
2428
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+ } ) ;
3135
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+ }
4550 }
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 ;
4660 }
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 ;
5661} ;
5762
5863//the old connect method of the pool
@@ -62,12 +67,15 @@ var createPool = function(clientConfig) {
6267//a bunch of problems, but for backwards compatibility
6368//we're leaving it in
6469var 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+
7179var oldConnect = function ( pool , client , cb ) {
7280 var tid = setTimeout ( function ( ) {
7381 console . error ( errorMessage ) ;
@@ -90,10 +98,4 @@ var newConnect = function(pool, client, cb) {
9098 } ) ;
9199} ;
92100
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