@@ -50,6 +50,38 @@ exports.initialize = function initializeDataSource(dataSource, callback) {
50
50
}
51
51
} ;
52
52
53
+ // there is a bug in the generic pool where it will not recreate
54
+ // destroyed workers (even if there is waiting work to do) unless
55
+ // there is a min specified. Make sure we keep some connections
56
+ // SEE: https://github.com/coopernurse/node-pool/pull/186
57
+ // SEE: https://github.com/brianc/node-pg-pool/issues/48
58
+ // SEE: https://github.com/strongloop/loopback-connector-postgresql/issues/231
59
+ function _ensureMinimum ( ) {
60
+ var i , diff , waiting ;
61
+ if ( this . _draining ) return ;
62
+ waiting = this . _waitingClients . size ( ) ;
63
+ if ( this . _factory . min > 0 ) { // we have positive specified minimum
64
+ diff = this . _factory . min - this . _count ;
65
+ } else if ( waiting > 0 ) { // we have no minimum, but we do have work to do
66
+ diff = Math . min ( waiting , this . _factory . max - this . _count ) ;
67
+ }
68
+ for ( i = 0 ; i < diff ; i ++ ) {
69
+ this . _createResource ( ) ;
70
+ }
71
+ } ;
72
+ function makePool ( options ) {
73
+ var pg = new postgresql . Pool ( options ) ;
74
+ // Monkey patch to ensure we always finish our work
75
+ // - There is a bug where callbacks go uncalled if min is not set
76
+ // - We might still not want a connection to *always* exist
77
+ // - but we do want to create up to max connections if we have work
78
+ // - still waiting
79
+ // This should be safe till the version of pg-pool is upgraded
80
+ // SEE: https://github.com/coopernurse/node-pool/pull/186
81
+ pg . pool . _ensureMinimum = _ensureMinimum ;
82
+ return pg ;
83
+ }
84
+
53
85
/**
54
86
* PostgreSQL connector constructor
55
87
*
@@ -78,7 +110,8 @@ function PostgreSQL(postgresql, settings) {
78
110
this . clientConfig . connectionString = settings . url ;
79
111
}
80
112
this . clientConfig . Promise = Promise ;
81
- this . pg = new postgresql . Pool ( this . clientConfig ) ;
113
+ this . pg = makePool ( this . clientConfig ) ;
114
+
82
115
this . settings = settings ;
83
116
if ( settings . debug ) {
84
117
debug ( 'Settings %j' , settings ) ;
0 commit comments