@@ -62,6 +62,7 @@ export const createConnectionPool = ({
62
62
} : {
63
63
driver : Driver ;
64
64
idleTimeout ?: number ;
65
+ // TODO rename to `maxPoolSize`
65
66
poolSize ?: number ;
66
67
} ) : ConnectionPool => {
67
68
// See test "waits for all connections to be established before attempting to terminate the pool"
@@ -124,23 +125,14 @@ export const createConnectionPool = ({
124
125
throw new Error ( 'Connection pool has ended.' ) ;
125
126
}
126
127
127
- const idleConnection = connections . find (
128
- ( connection ) => connection . state ( ) === 'IDLE' ,
129
- ) ;
130
-
131
- if ( idleConnection ) {
132
- idleConnection . acquire ( ) ;
133
-
134
- return idleConnection ;
135
- }
136
-
137
- if ( pendingConnections . length + connections . length < poolSize ) {
128
+ const addConnection = async ( ) => {
138
129
const pendingConnection = driver . createClient ( ) ;
139
130
140
131
pendingConnections . push ( pendingConnection ) ;
141
132
142
133
const connection = await pendingConnection . catch ( ( error ) => {
143
134
pendingConnections . pop ( ) ;
135
+
144
136
throw error ;
145
137
} ) ;
146
138
@@ -183,8 +175,6 @@ export const createConnectionPool = ({
183
175
184
176
connection . on ( 'destroy' , onDestroy ) ;
185
177
186
- connection . acquire ( ) ;
187
-
188
178
connections . push ( connection ) ;
189
179
190
180
pendingConnections . splice (
@@ -193,38 +183,56 @@ export const createConnectionPool = ({
193
183
) ;
194
184
195
185
return connection ;
196
- } else {
197
- const deferred = defer < ConnectionPoolClient > ( ) ;
186
+ } ;
198
187
199
- waitingClients . push ( {
200
- deferred,
201
- } ) ;
188
+ const idleConnection = connections . find (
189
+ ( connection ) => connection . state ( ) === 'IDLE' ,
190
+ ) ;
191
+
192
+ if ( idleConnection ) {
193
+ idleConnection . acquire ( ) ;
194
+
195
+ return idleConnection ;
196
+ }
197
+
198
+ if ( pendingConnections . length + connections . length < poolSize ) {
199
+ const newConnection = await addConnection ( ) ;
200
+
201
+ newConnection . acquire ( ) ;
202
+
203
+ return newConnection ;
204
+ }
205
+
206
+ const deferred = defer < ConnectionPoolClient > ( ) ;
202
207
203
- const queuedAt = process . hrtime . bigint ( ) ;
208
+ waitingClients . push ( {
209
+ deferred,
210
+ } ) ;
204
211
205
- logger . warn (
212
+ const queuedAt = process . hrtime . bigint ( ) ;
213
+
214
+ logger . warn (
215
+ {
216
+ connections : connections . length ,
217
+ pendingConnections : pendingConnections . length ,
218
+ poolSize,
219
+ waitingClients : waitingClients . length ,
220
+ } ,
221
+ `connection pool full; client has been queued` ,
222
+ ) ;
223
+
224
+ // eslint-disable-next-line promise/prefer-await-to-then
225
+ return deferred . promise . then ( ( connection ) => {
226
+ logger . debug (
206
227
{
207
- connections : connections . length ,
208
- pendingConnections : pendingConnections . length ,
209
- poolSize,
210
- waitingClients : waitingClients . length ,
228
+ connectionId : connection . id ( ) ,
229
+ duration : Number ( process . hrtime . bigint ( ) - queuedAt ) / 1e6 ,
211
230
} ,
212
- ` connection pool full; client has been queued` ,
231
+ ' connection has been acquired from the queue' ,
213
232
) ;
214
233
215
- // eslint-disable-next-line promise/prefer-await-to-then
216
- return deferred . promise . then ( ( connection ) => {
217
- logger . debug (
218
- {
219
- connectionId : connection . id ( ) ,
220
- duration : Number ( process . hrtime . bigint ( ) - queuedAt ) / 1e6 ,
221
- } ,
222
- 'connection has been acquired from the queue' ,
223
- ) ;
224
-
225
- return connection ;
226
- } ) ;
227
- }
234
+ return connection ;
235
+ } ) ;
228
236
} ;
229
237
230
238
return {
0 commit comments