@@ -74,6 +74,7 @@ type poolConfig struct {
74
74
MaxConnecting uint64
75
75
MaxIdleTime time.Duration
76
76
MaintainInterval time.Duration
77
+ LoadBalanced bool
77
78
PoolMonitor * event.PoolMonitor
78
79
Logger * logger.Logger
79
80
handshakeErrFn func (error , uint64 , * primitive.ObjectID )
@@ -93,6 +94,7 @@ type pool struct {
93
94
minSize uint64
94
95
maxSize uint64
95
96
maxConnecting uint64
97
+ loadBalanced bool
96
98
monitor * event.PoolMonitor
97
99
logger * logger.Logger
98
100
@@ -206,6 +208,7 @@ func newPool(config poolConfig, connOpts ...ConnectionOption) *pool {
206
208
minSize : config .MinPoolSize ,
207
209
maxSize : config .MaxPoolSize ,
208
210
maxConnecting : maxConnecting ,
211
+ loadBalanced : config .LoadBalanced ,
209
212
monitor : config .PoolMonitor ,
210
213
logger : config .Logger ,
211
214
handshakeErrFn : config .handshakeErrFn ,
@@ -572,6 +575,7 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) {
572
575
p .stateMu .RUnlock ()
573
576
574
577
// Wait for either the wantConn to be ready or for the Context to time out.
578
+ start := time .Now ()
575
579
select {
576
580
case <- w .ready :
577
581
if w .err != nil {
@@ -612,6 +616,8 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) {
612
616
}
613
617
return w .conn , nil
614
618
case <- ctx .Done ():
619
+ duration := time .Since (start )
620
+
615
621
if mustLogPoolMessage (p ) {
616
622
keysAndValues := logger.KeyValues {
617
623
logger .KeyReason , logger .ReasonConnCheckoutFailedTimout ,
@@ -628,13 +634,20 @@ func (p *pool) checkOut(ctx context.Context) (conn *connection, err error) {
628
634
})
629
635
}
630
636
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
+ }
637
649
}
650
+ return nil , err
638
651
}
639
652
}
640
653
0 commit comments