@@ -122,8 +122,6 @@ func newPool(config poolConfig, connOpts ...ConnectionOption) *pool {
122
122
PoolOptions : & event.MonitorPoolOptions {
123
123
MaxPoolSize : config .MaxPoolSize ,
124
124
MinPoolSize : config .MinPoolSize ,
125
- // TODO: We don't use this value, should we publish it?
126
- WaitQueueTimeoutMS : uint64 (config .MaxIdleTime ) / uint64 (time .Millisecond ),
127
125
},
128
126
Address : pool .address .String (),
129
127
})
@@ -214,49 +212,43 @@ func (p *pool) disconnect(ctx context.Context) error {
214
212
}
215
213
}
216
214
217
- // Collect all conns from the pool and all wantConns from newConnWait while holding the
218
- // connsCond lock. We can't call removeConnection on the connections or cancel on the wantConns
219
- // while holding any locks, so do that after we release the lock.
215
+ // Empty the idle connections stack and try to deliver ErrPoolDisconnected to any waiting
216
+ // wantConns from idleConnWait while holding the idleMu lock.
217
+ p .idleMu .Lock ()
218
+ p .idleConns = p .idleConns [:0 ]
219
+ for {
220
+ w := p .idleConnWait .popFront ()
221
+ if w == nil {
222
+ break
223
+ }
224
+ w .tryDeliver (nil , ErrPoolDisconnected )
225
+ }
226
+ p .idleMu .Unlock ()
227
+
228
+ // Collect all conns from the pool and try to deliver ErrPoolDisconnected to any waiting
229
+ // wantConns from newConnWait while holding the connsCond lock. We can't call removeConnection
230
+ // on the connections or cancel on the wantConns while holding any locks, so do that after we
231
+ // release the lock.
220
232
p .connsCond .L .Lock ()
221
233
conns := make ([]* connection , 0 , len (p .conns ))
222
234
for _ , conn := range p .conns {
223
235
conns = append (conns , conn )
224
236
}
225
-
226
- wantConns := make ([]* wantConn , 0 , p .newConnWait .len ())
227
237
for {
228
238
w := p .newConnWait .popFront ()
229
239
if w == nil {
230
240
break
231
241
}
232
- wantConns = append ( wantConns , w )
242
+ w . tryDeliver ( nil , ErrPoolDisconnected )
233
243
}
234
244
p .connsCond .L .Unlock ()
235
245
236
- // Empty the idle connections stack and collect all wantConns from idleConnWait while holding
237
- // the idleMu lock. We can't call cancel on the wantConns while holding any locks, so do that
238
- // after we release the lock.
239
- p .idleMu .Lock ()
240
- p .idleConns = p .idleConns [:0 ]
241
-
242
- for {
243
- w := p .idleConnWait .popFront ()
244
- if w == nil {
245
- break
246
- }
247
- wantConns = append (wantConns , w )
248
- }
249
- p .idleMu .Unlock ()
250
-
251
246
// Now that we're not holding any locks, remove all of the connections we collected from the
252
- // pool and try to deliver ErrPoolDisconnected to any waiting wantConns .
247
+ // pool.
253
248
for _ , conn := range conns {
254
249
_ = p .removeConnection (conn , event .ReasonPoolClosed )
255
250
_ = p .closeConnection (conn ) // We don't care about errors while closing the connection.
256
251
}
257
- for _ , w := range wantConns {
258
- w .tryDeliver (nil , ErrPoolDisconnected )
259
- }
260
252
261
253
atomic .StoreInt64 (& p .connected , disconnected )
262
254
@@ -724,9 +716,14 @@ func (p *pool) maintain(ctx context.Context, wg *sync.WaitGroup) {
724
716
725
717
// Figure out how many more wantConns we need to satisfy minPoolSize. Assume that the
726
718
// outstanding wantConns (i.e. the ones that weren't removed from the slice) will all return
727
- // connections when they're ready, so only add wantConns to make up the difference.
719
+ // connections when they're ready, so only add wantConns to make up the difference. Limit
720
+ // the number of connections requested to max 10 at a time to prevent overshooting
721
+ // minPoolSize in case other checkOut() calls are requesting new connections, too.
728
722
total := p .totalConnectionCount ()
729
723
n := int (p .minSize ) - total - len (wantConns )
724
+ if n > 10 {
725
+ n = 10
726
+ }
730
727
731
728
for i := 0 ; i < n ; i ++ {
732
729
w := newWantConn ()
0 commit comments