Skip to content

Commit f92a927

Browse files
committed
Request max 10 connections at a time to satisfy minPoolSize and simplify disconnect().
1 parent 5a240a2 commit f92a927

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

x/mongo/driver/topology/pool.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ func newPool(config poolConfig, connOpts ...ConnectionOption) *pool {
122122
PoolOptions: &event.MonitorPoolOptions{
123123
MaxPoolSize: config.MaxPoolSize,
124124
MinPoolSize: config.MinPoolSize,
125-
// TODO: We don't use this value, should we publish it?
126-
WaitQueueTimeoutMS: uint64(config.MaxIdleTime) / uint64(time.Millisecond),
127125
},
128126
Address: pool.address.String(),
129127
})
@@ -214,49 +212,43 @@ func (p *pool) disconnect(ctx context.Context) error {
214212
}
215213
}
216214

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.
220232
p.connsCond.L.Lock()
221233
conns := make([]*connection, 0, len(p.conns))
222234
for _, conn := range p.conns {
223235
conns = append(conns, conn)
224236
}
225-
226-
wantConns := make([]*wantConn, 0, p.newConnWait.len())
227237
for {
228238
w := p.newConnWait.popFront()
229239
if w == nil {
230240
break
231241
}
232-
wantConns = append(wantConns, w)
242+
w.tryDeliver(nil, ErrPoolDisconnected)
233243
}
234244
p.connsCond.L.Unlock()
235245

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-
251246
// 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.
253248
for _, conn := range conns {
254249
_ = p.removeConnection(conn, event.ReasonPoolClosed)
255250
_ = p.closeConnection(conn) // We don't care about errors while closing the connection.
256251
}
257-
for _, w := range wantConns {
258-
w.tryDeliver(nil, ErrPoolDisconnected)
259-
}
260252

261253
atomic.StoreInt64(&p.connected, disconnected)
262254

@@ -724,9 +716,14 @@ func (p *pool) maintain(ctx context.Context, wg *sync.WaitGroup) {
724716

725717
// Figure out how many more wantConns we need to satisfy minPoolSize. Assume that the
726718
// 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.
728722
total := p.totalConnectionCount()
729723
n := int(p.minSize) - total - len(wantConns)
724+
if n > 10 {
725+
n = 10
726+
}
730727

731728
for i := 0; i < n; i++ {
732729
w := newWantConn()

0 commit comments

Comments
 (0)