@@ -12,20 +12,45 @@ const removeWhere = (list, predicate) => {
12
12
}
13
13
14
14
class IdleItem {
15
- constructor ( client , idleListener , timeoutId ) {
15
+ constructor ( client , idleListener , timeoutId ) {
16
16
this . client = client
17
17
this . idleListener = idleListener
18
18
this . timeoutId = timeoutId
19
19
}
20
20
}
21
21
22
22
class PendingItem {
23
- constructor ( callback ) {
23
+ constructor ( callback ) {
24
24
this . callback = callback
25
25
}
26
26
}
27
27
28
- function promisify ( Promise , callback ) {
28
+ function throwOnRelease ( ) {
29
+ throw new Error ( 'Release called on client which has already been released to the pool.' )
30
+ }
31
+
32
+ function release ( client , err ) {
33
+ client . release = throwOnRelease
34
+ if ( err || this . ending || ! client . _queryable || client . _ending ) {
35
+ this . _remove ( client )
36
+ this . _pulseQueue ( )
37
+ return
38
+ }
39
+
40
+ // idle timeout
41
+ let tid
42
+ if ( this . options . idleTimeoutMillis ) {
43
+ tid = setTimeout ( ( ) => {
44
+ this . log ( 'remove idle client' )
45
+ this . _remove ( client )
46
+ } , this . options . idleTimeoutMillis )
47
+ }
48
+
49
+ this . _idle . push ( new IdleItem ( client , tid ) )
50
+ this . _pulseQueue ( )
51
+ }
52
+
53
+ function promisify ( Promise , callback ) {
29
54
if ( callback ) {
30
55
return { callback : callback , result : undefined }
31
56
}
@@ -41,8 +66,8 @@ function promisify (Promise, callback) {
41
66
return { callback : cb , result : result }
42
67
}
43
68
44
- function makeIdleListener ( pool , client ) {
45
- return function idleListener ( err ) {
69
+ function makeIdleListener ( pool , client ) {
70
+ return function idleListener ( err ) {
46
71
err . client = client
47
72
48
73
client . removeListener ( 'error' , idleListener )
@@ -57,7 +82,7 @@ function makeIdleListener (pool, client) {
57
82
}
58
83
59
84
class Pool extends EventEmitter {
60
- constructor ( options , Client ) {
85
+ constructor ( options , Client ) {
61
86
super ( )
62
87
this . options = Object . assign ( { } , options )
63
88
@@ -89,11 +114,11 @@ class Pool extends EventEmitter {
89
114
this . ended = false
90
115
}
91
116
92
- _isFull ( ) {
117
+ _isFull ( ) {
93
118
return this . _clients . length >= this . options . max
94
119
}
95
120
96
- _pulseQueue ( ) {
121
+ _pulseQueue ( ) {
97
122
this . log ( 'pulse queue' )
98
123
if ( this . ended ) {
99
124
this . log ( 'pulse queue ended' )
@@ -136,7 +161,7 @@ class Pool extends EventEmitter {
136
161
throw new Error ( 'unexpected condition' )
137
162
}
138
163
139
- _remove ( client ) {
164
+ _remove ( client ) {
140
165
const removed = removeWhere (
141
166
this . _idle ,
142
167
item => item . client === client
@@ -151,7 +176,7 @@ class Pool extends EventEmitter {
151
176
this . emit ( 'remove' , client )
152
177
}
153
178
154
- connect ( cb ) {
179
+ connect ( cb ) {
155
180
if ( this . ending ) {
156
181
const err = new Error ( 'Cannot use a pool after calling end on the pool' )
157
182
return cb ? cb ( err ) : this . Promise . reject ( err )
@@ -197,7 +222,7 @@ class Pool extends EventEmitter {
197
222
return result
198
223
}
199
224
200
- newClient ( pendingItem ) {
225
+ newClient ( pendingItem ) {
201
226
const client = new this . Client ( this . options )
202
227
this . _clients . push ( client )
203
228
const idleListener = makeIdleListener ( this , client )
@@ -245,7 +270,7 @@ class Pool extends EventEmitter {
245
270
}
246
271
247
272
// acquire a client for a pending work item
248
- _acquireClient ( client , pendingItem , idleListener , isNew ) {
273
+ _acquireClient ( client , pendingItem , idleListener , isNew ) {
249
274
if ( isNew ) {
250
275
this . emit ( 'connect' , client )
251
276
}
@@ -289,7 +314,7 @@ class Pool extends EventEmitter {
289
314
290
315
// release a client back to the poll, include an error
291
316
// to remove it from the pool
292
- _release ( client , idleListener , err ) {
317
+ _release ( client , idleListener , err ) {
293
318
client . on ( 'error' , idleListener )
294
319
295
320
if ( err || this . ending ) {
@@ -311,7 +336,7 @@ class Pool extends EventEmitter {
311
336
this . _pulseQueue ( )
312
337
}
313
338
314
- query ( text , values , cb ) {
339
+ query ( text , values , cb ) {
315
340
// guard clause against passing a function as the first parameter
316
341
if ( typeof text === 'function' ) {
317
342
const response = promisify ( this . Promise , text )
@@ -364,7 +389,7 @@ class Pool extends EventEmitter {
364
389
return response . result
365
390
}
366
391
367
- end ( cb ) {
392
+ end ( cb ) {
368
393
this . log ( 'ending' )
369
394
if ( this . ending ) {
370
395
const err = new Error ( 'Called end on pool more than once' )
@@ -377,15 +402,15 @@ class Pool extends EventEmitter {
377
402
return promised . result
378
403
}
379
404
380
- get waitingCount ( ) {
405
+ get waitingCount ( ) {
381
406
return this . _pendingQueue . length
382
407
}
383
408
384
- get idleCount ( ) {
409
+ get idleCount ( ) {
385
410
return this . _idle . length
386
411
}
387
412
388
- get totalCount ( ) {
413
+ get totalCount ( ) {
389
414
return this . _clients . length
390
415
}
391
416
}
0 commit comments