File tree 2 files changed +43
-5
lines changed
2 files changed +43
-5
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,14 @@ const EventEmitter = require('events').EventEmitter
3
3
4
4
const NOOP = function ( ) { }
5
5
6
+ const removeWhere = ( list , predicate ) => {
7
+ const i = list . findIndex ( predicate )
8
+
9
+ return i === - 1
10
+ ? undefined
11
+ : list . splice ( i , 1 ) [ 0 ]
12
+ }
13
+
6
14
class IdleItem {
7
15
constructor ( client , timeoutId ) {
8
16
this . client = client
@@ -80,7 +88,7 @@ class Pool extends EventEmitter {
80
88
if ( this . ending ) {
81
89
this . log ( 'pulse queue on ending' )
82
90
if ( this . _idle . length ) {
83
- this . _idle . map ( item => {
91
+ this . _idle . slice ( ) . map ( item => {
84
92
this . _remove ( item . client )
85
93
} )
86
94
}
@@ -114,10 +122,15 @@ class Pool extends EventEmitter {
114
122
}
115
123
116
124
_remove ( client ) {
117
- this . _idle = this . _idle . filter ( item => {
118
- clearTimeout ( item . timeoutId )
119
- return item . client !== client
120
- } )
125
+ const removed = removeWhere (
126
+ this . _idle ,
127
+ item => item . client === client
128
+ )
129
+
130
+ if ( removed !== undefined ) {
131
+ clearTimeout ( removed . timeoutId )
132
+ }
133
+
121
134
this . _clients = this . _clients . filter ( c => c !== client )
122
135
client . end ( )
123
136
this . emit ( 'remove' , client )
Original file line number Diff line number Diff line change @@ -20,6 +20,31 @@ describe('idle timeout', () => {
20
20
} )
21
21
} )
22
22
23
+ it ( 'times out and removes clients when others are also removed' , co . wrap ( function * ( ) {
24
+ const pool = new Pool ( { idleTimeoutMillis : 10 } )
25
+ const clientA = yield pool . connect ( )
26
+ const clientB = yield pool . connect ( )
27
+ clientA . release ( )
28
+ clientB . release ( new Error ( ) )
29
+
30
+ const removal = new Promise ( ( resolve ) => {
31
+ pool . on ( 'remove' , ( ) => {
32
+ expect ( pool . idleCount ) . to . equal ( 0 )
33
+ expect ( pool . totalCount ) . to . equal ( 0 )
34
+ resolve ( )
35
+ } )
36
+ } )
37
+
38
+ const timeout = wait ( 100 ) . then ( ( ) =>
39
+ Promise . reject ( new Error ( 'Idle timeout failed to occur' ) ) )
40
+
41
+ try {
42
+ yield Promise . race ( [ removal , timeout ] )
43
+ } finally {
44
+ pool . end ( )
45
+ }
46
+ } ) )
47
+
23
48
it ( 'can remove idle clients and recreate them' , co . wrap ( function * ( ) {
24
49
const pool = new Pool ( { idleTimeoutMillis : 1 } )
25
50
const results = [ ]
You can’t perform that action at this time.
0 commit comments