@@ -42,15 +42,16 @@ function Postgres(a, b) {
42
42
let ending = false
43
43
44
44
const queries = Queue ( )
45
- , connections = [ ...Array ( options . max ) ] . map ( ( ) => Connection ( options , { onopen, onend, ondrain, onclose } ) )
46
- , closed = Queue ( connections )
45
+ , connecting = Queue ( )
47
46
, reserved = Queue ( )
47
+ , closed = Queue ( )
48
+ , ended = Queue ( )
48
49
, open = Queue ( )
49
50
, busy = Queue ( )
50
51
, full = Queue ( )
51
- , ended = Queue ( )
52
- , connecting = Queue ( )
53
- , queues = { closed , ended , connecting , reserved , open , busy , full }
52
+ , queues = { connecting , reserved , closed , ended , open , busy , full }
53
+
54
+ const connections = [ ... Array ( options . max ) ] . map ( ( ) => Connection ( options , queues , { onopen , onend , onclose } ) )
54
55
55
56
const sql = Sql ( handler )
56
57
@@ -229,23 +230,30 @@ function Postgres(a, b) {
229
230
230
231
function handler ( q ) {
231
232
q . catch ( e => uncaughtError || ( uncaughtError = e ) )
232
- c . state === ' full'
233
+ c . queue === full
233
234
? queries . push ( q )
234
- : c . execute ( q ) || ( c . state = 'full' , full . push ( c ) )
235
+ : c . execute ( q ) || move ( c , full )
235
236
}
236
237
}
237
238
238
239
function onexecute ( c ) {
239
- queues [ c . state ] . remove ( c )
240
- c . state = ' reserved'
240
+ connection = c
241
+ move ( c , reserved )
241
242
c . reserved = ( ) => queries . length
242
243
? c . execute ( queries . shift ( ) )
243
- : c . state = 'reserved'
244
- reserved . push ( c )
245
- connection = c
244
+ : move ( c , reserved )
246
245
}
247
246
}
248
247
248
+ function move ( c , queue ) {
249
+ c . queue . remove ( c )
250
+ queue . push ( c )
251
+ c . queue = queue
252
+ queue === open
253
+ ? c . idleTimer . start ( )
254
+ : c . idleTimer . cancel ( )
255
+ }
256
+
249
257
function json ( x ) {
250
258
return new Parameter ( x , 3802 )
251
259
}
@@ -262,28 +270,27 @@ function Postgres(a, b) {
262
270
return query . reject ( Errors . connection ( 'CONNECTION_ENDED' , options , options ) )
263
271
264
272
if ( open . length )
265
- return go ( open , query )
273
+ return go ( open . shift ( ) , query )
266
274
267
275
if ( closed . length )
268
276
return connect ( closed . shift ( ) , query )
269
277
270
278
busy . length
271
- ? go ( busy , query )
279
+ ? go ( busy . shift ( ) , query )
272
280
: queries . push ( query )
273
281
}
274
282
275
- function go ( xs , query ) {
276
- const c = xs . shift ( )
283
+ function go ( c , query ) {
277
284
return c . execute ( query )
278
- ? ( c . state = 'busy' , busy . push ( c ) )
279
- : ( c . state = 'full' , full . push ( c ) )
285
+ ? move ( c , busy )
286
+ : move ( c , full )
280
287
}
281
288
282
289
function cancel ( query ) {
283
290
return new Promise ( ( resolve , reject ) => {
284
291
query . state
285
292
? query . active
286
- ? Connection ( options , { } ) . cancel ( query . state , resolve , reject )
293
+ ? Connection ( options ) . cancel ( query . state , resolve , reject )
287
294
: query . cancelled = { resolve, reject }
288
295
: (
289
296
queries . remove ( query ) ,
@@ -317,21 +324,17 @@ function Postgres(a, b) {
317
324
}
318
325
319
326
function connect ( c , query ) {
320
- c . state = 'connecting'
321
- connecting . push ( c )
327
+ move ( c , connecting )
322
328
c . connect ( query )
323
329
}
324
330
325
331
function onend ( c ) {
326
- queues [ c . state ] . remove ( c )
327
- c . state = 'ended'
328
- ended . push ( c )
332
+ move ( c , ended )
329
333
}
330
334
331
335
function onopen ( c ) {
332
- queues [ c . state ] . remove ( c )
333
336
if ( queries . length === 0 )
334
- return ( c . state = 'open' , open . push ( c ) )
337
+ return move ( c , open )
335
338
336
339
let max = Math . ceil ( queries . length / ( connecting . length + 1 ) )
337
340
, ready = true
@@ -340,23 +343,15 @@ function Postgres(a, b) {
340
343
ready = c . execute ( queries . shift ( ) )
341
344
342
345
ready
343
- ? ( c . state = 'busy' , busy . push ( c ) )
344
- : ( c . state = 'full' , full . push ( c ) )
345
- }
346
-
347
- function ondrain ( c ) {
348
- full . remove ( c )
349
- onopen ( c )
346
+ ? move ( c , busy )
347
+ : move ( c , full )
350
348
}
351
349
352
350
function onclose ( c ) {
353
- queues [ c . state ] . remove ( c )
354
- c . state = 'closed'
351
+ move ( c , closed )
355
352
c . reserved = null
356
353
options . onclose && options . onclose ( c . id )
357
- queries . length
358
- ? connect ( c , queries . shift ( ) )
359
- : queues . closed . push ( c )
354
+ queries . length && connect ( c , queries . shift ( ) )
360
355
}
361
356
}
362
357
0 commit comments