Skip to content

Commit 43ccd68

Browse files
committed
GODRIVER-2423 Include pinned connections in WaitQueueTimeoutError only for load balanced clusters. (#1353)
1 parent 2b69f03 commit 43ccd68

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

x/mongo/driver/topology/errors.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package topology
99
import (
1010
"context"
1111
"fmt"
12+
"time"
1213

1314
"go.mongodb.org/mongo-driver/mongo/description"
1415
)
@@ -69,11 +70,17 @@ func (e ServerSelectionError) Unwrap() error {
6970

7071
// WaitQueueTimeoutError represents a timeout when requesting a connection from the pool
7172
type WaitQueueTimeoutError struct {
72-
Wrapped error
73-
PinnedCursorConnections uint64
74-
PinnedTransactionConnections uint64
75-
maxPoolSize uint64
76-
totalConnectionCount int
73+
Wrapped error
74+
pinnedConnections *pinnedConnections
75+
maxPoolSize uint64
76+
totalConnections int
77+
availableConnections int
78+
waitDuration time.Duration
79+
}
80+
81+
type pinnedConnections struct {
82+
cursorConnections uint64
83+
transactionConnections uint64
7784
}
7885

7986
// Error implements the error interface.
@@ -95,14 +102,19 @@ func (w WaitQueueTimeoutError) Error() string {
95102
)
96103
}
97104

98-
return fmt.Sprintf(
99-
"%s; maxPoolSize: %d, connections in use by cursors: %d"+
100-
", connections in use by transactions: %d, connections in use by other operations: %d",
101-
errorMsg,
102-
w.maxPoolSize,
103-
w.PinnedCursorConnections,
104-
w.PinnedTransactionConnections,
105-
uint64(w.totalConnectionCount)-w.PinnedCursorConnections-w.PinnedTransactionConnections)
105+
msg := fmt.Sprintf("%s; total connections: %d, maxPoolSize: %d, ", errorMsg, w.totalConnections, w.maxPoolSize)
106+
if pinnedConnections := w.pinnedConnections; pinnedConnections != nil {
107+
openConnectionCount := uint64(w.totalConnections) -
108+
pinnedConnections.cursorConnections -
109+
pinnedConnections.transactionConnections
110+
msg += fmt.Sprintf("connections in use by cursors: %d, connections in use by transactions: %d, connections in use by other operations: %d, ",
111+
pinnedConnections.cursorConnections,
112+
pinnedConnections.transactionConnections,
113+
openConnectionCount,
114+
)
115+
}
116+
msg += fmt.Sprintf("idle connections: %d, wait duration: %s", w.availableConnections, w.waitDuration.String())
117+
return msg
106118
}
107119

108120
// Unwrap returns the underlying error.

x/mongo/driver/topology/pool.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type poolConfig struct {
7474
MaxConnecting uint64
7575
MaxIdleTime time.Duration
7676
MaintainInterval time.Duration
77+
LoadBalanced bool
7778
PoolMonitor *event.PoolMonitor
7879
Logger *logger.Logger
7980
handshakeErrFn func(error, uint64, *primitive.ObjectID)
@@ -93,6 +94,7 @@ type pool struct {
9394
minSize uint64
9495
maxSize uint64
9596
maxConnecting uint64
97+
loadBalanced bool
9698
monitor *event.PoolMonitor
9799
logger *logger.Logger
98100

@@ -206,6 +208,7 @@ func newPool(config poolConfig, connOpts ...ConnectionOption) *pool {
206208
minSize: config.MinPoolSize,
207209
maxSize: config.MaxPoolSize,
208210
maxConnecting: maxConnecting,
211+
loadBalanced: config.LoadBalanced,
209212
monitor: config.PoolMonitor,
210213
logger: config.Logger,
211214
handshakeErrFn: config.handshakeErrFn,
@@ -572,6 +575,7 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) {
572575
p.stateMu.RUnlock()
573576

574577
// Wait for either the wantConn to be ready or for the Context to time out.
578+
start := time.Now()
575579
select {
576580
case <-w.ready:
577581
if w.err != nil {
@@ -612,6 +616,8 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) {
612616
}
613617
return w.conn, nil
614618
case <-ctx.Done():
619+
duration := time.Since(start)
620+
615621
if mustLogPoolMessage(p) {
616622
keysAndValues := logger.KeyValues{
617623
logger.KeyReason, logger.ReasonConnCheckoutFailedTimout,
@@ -628,13 +634,20 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) {
628634
})
629635
}
630636

631-
return nil, WaitQueueTimeoutError{
632-
Wrapped: ctx.Err(),
633-
PinnedCursorConnections: atomic.LoadUint64(&p.pinnedCursorConnections),
634-
PinnedTransactionConnections: atomic.LoadUint64(&p.pinnedTransactionConnections),
635-
maxPoolSize: p.maxSize,
636-
totalConnectionCount: p.totalConnectionCount(),
637+
err := WaitQueueTimeoutError{
638+
Wrapped: ctx.Err(),
639+
maxPoolSize: p.maxSize,
640+
totalConnections: p.totalConnectionCount(),
641+
availableConnections: p.availableConnectionCount(),
642+
waitDuration: duration,
643+
}
644+
if p.loadBalanced {
645+
err.pinnedConnections = &pinnedConnections{
646+
cursorConnections: atomic.LoadUint64(&p.pinnedCursorConnections),
647+
transactionConnections: atomic.LoadUint64(&p.pinnedTransactionConnections),
648+
}
637649
}
650+
return nil, err
638651
}
639652
}
640653

x/mongo/driver/topology/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func NewServer(addr address.Address, topologyID primitive.ObjectID, opts ...Serv
176176
MaxConnecting: cfg.maxConnecting,
177177
MaxIdleTime: cfg.poolMaxIdleTime,
178178
MaintainInterval: cfg.poolMaintainInterval,
179+
LoadBalanced: cfg.loadBalanced,
179180
PoolMonitor: cfg.poolMonitor,
180181
Logger: cfg.logger,
181182
handshakeErrFn: s.ProcessHandshakeError,

0 commit comments

Comments
 (0)