@@ -56,7 +56,6 @@ class Pool {
56
56
this . _maxSize = config . maxSize
57
57
this . _acquisitionTimeout = config . acquisitionTimeout
58
58
this . _pools = { }
59
- this . _pendingCreates = { }
60
59
this . _acquireRequests = { }
61
60
this . _activeResourceCounts = { }
62
61
this . _release = this . _release . bind ( this )
@@ -74,11 +73,18 @@ class Pool {
74
73
const key = address . asKey ( )
75
74
76
75
if ( resource ) {
77
- resourceAcquired ( key , this . _activeResourceCounts )
78
- if ( this . _log . isDebugEnabled ( ) ) {
79
- this . _log . debug ( `${ resource } acquired from the pool ${ key } ` )
76
+ if (
77
+ this . _maxSize &&
78
+ this . activeResourceCount ( address ) >= this . _maxSize
79
+ ) {
80
+ this . _destroy ( resource )
81
+ } else {
82
+ resourceAcquired ( key , this . _activeResourceCounts )
83
+ if ( this . _log . isDebugEnabled ( ) ) {
84
+ this . _log . debug ( `${ resource } acquired from the pool ${ key } ` )
85
+ }
86
+ return resource
80
87
}
81
- return resource
82
88
}
83
89
84
90
// We're out of resources and will try to acquire later on when an existing resource is released.
@@ -179,7 +185,6 @@ class Pool {
179
185
if ( ! pool ) {
180
186
pool = [ ]
181
187
this . _pools [ key ] = pool
182
- this . _pendingCreates [ key ] = 0
183
188
}
184
189
while ( pool . length ) {
185
190
const resource = pool . pop ( )
@@ -196,29 +201,12 @@ class Pool {
196
201
}
197
202
}
198
203
199
- // Ensure requested max pool size
200
- if ( this . _maxSize > 0 ) {
201
- // Include pending creates when checking pool size since these probably will add
202
- // to the number when fulfilled.
203
- const numConnections =
204
- this . activeResourceCount ( address ) + this . _pendingCreates [ key ]
205
- if ( numConnections >= this . _maxSize ) {
206
- // Will put this request in queue instead since the pool is full
207
- return null
208
- }
204
+ if ( this . _maxSize && this . activeResourceCount ( address ) >= this . _maxSize ) {
205
+ return null
209
206
}
210
207
211
208
// there exist no idle valid resources, create a new one for acquisition
212
- // Keep track of how many pending creates there are to avoid making too many connections.
213
- this . _pendingCreates [ key ] = this . _pendingCreates [ key ] + 1
214
- let resource
215
- try {
216
- // Invoke callback that creates actual connection
217
- resource = await this . _create ( address , this . _release )
218
- } finally {
219
- this . _pendingCreates [ key ] = this . _pendingCreates [ key ] - 1
220
- }
221
- return resource
209
+ return await this . _create ( address , this . _release )
222
210
}
223
211
224
212
async _release ( address , resource ) {
0 commit comments