@@ -438,6 +438,26 @@ func (s *Server) updateDescription(desc description.Server) {
438
438
s .subLock .Unlock ()
439
439
}
440
440
441
+ // createConnection creates a new connection instance but does not call connect on it. The caller must call connect
442
+ // before the connection can be used for network operations.
443
+ func (s * Server ) createConnection (ctx context.Context ) (* connection , error ) {
444
+ opts := []ConnectionOption {
445
+ WithConnectTimeout (func (time.Duration ) time.Duration { return s .cfg .heartbeatTimeout }),
446
+ WithReadTimeout (func (time.Duration ) time.Duration { return s .cfg .heartbeatTimeout }),
447
+ WithWriteTimeout (func (time.Duration ) time.Duration { return s .cfg .heartbeatTimeout }),
448
+ // We override whatever handshaker is currently attached to the options with a basic
449
+ // one because need to make sure we don't do auth.
450
+ WithHandshaker (func (h Handshaker ) Handshaker {
451
+ return operation .NewIsMaster ().AppName (s .cfg .appname ).Compressors (s .cfg .compressionOpts )
452
+ }),
453
+ // Override any command monitors specified in options with nil to avoid monitoring heartbeats.
454
+ WithMonitor (func (* event.CommandMonitor ) * event.CommandMonitor { return nil }),
455
+ }
456
+ opts = append (s .cfg .connectionOpts , opts ... )
457
+
458
+ return newConnection (ctx , s .address , opts ... )
459
+ }
460
+
441
461
// heartbeat sends a heartbeat to the server using the given connection. The connection can be nil.
442
462
func (s * Server ) heartbeat (conn * connection ) (description.Server , * connection ) {
443
463
const maxRetry = 2
@@ -467,25 +487,7 @@ func (s *Server) heartbeat(conn *connection) (description.Server, *connection) {
467
487
}
468
488
469
489
if conn == nil {
470
- opts := []ConnectionOption {
471
- WithConnectTimeout (func (time.Duration ) time.Duration { return s .cfg .heartbeatTimeout }),
472
- WithReadTimeout (func (time.Duration ) time.Duration { return s .cfg .heartbeatTimeout }),
473
- WithWriteTimeout (func (time.Duration ) time.Duration { return s .cfg .heartbeatTimeout }),
474
- }
475
- opts = append (opts , s .cfg .connectionOpts ... )
476
- // We override whatever handshaker is currently attached to the options with a basic
477
- // one because need to make sure we don't do auth.
478
- opts = append (opts , WithHandshaker (func (h Handshaker ) Handshaker {
479
- now = time .Now ()
480
- return operation .NewIsMaster ().AppName (s .cfg .appname ).Compressors (s .cfg .compressionOpts )
481
- }))
482
-
483
- // Override any command monitors specified in options with nil to avoid monitoring heartbeats.
484
- opts = append (opts , WithMonitor (func (* event.CommandMonitor ) * event.CommandMonitor {
485
- return nil
486
- }))
487
-
488
- conn , err = newConnection (ctx , s .address , opts ... )
490
+ conn , err = s .createConnection (ctx )
489
491
490
492
conn .connect (ctx )
491
493
0 commit comments