Skip to content

Commit 61c3712

Browse files
author
Isabella Siu
committed
GODRIVER-1695 always use heartbeatTimeout for heartbeat connection (#470)
1 parent f8480f1 commit 61c3712

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

x/mongo/driver/topology/server.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,26 @@ func (s *Server) updateDescription(desc description.Server) {
438438
s.subLock.Unlock()
439439
}
440440

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+
441461
// heartbeat sends a heartbeat to the server using the given connection. The connection can be nil.
442462
func (s *Server) heartbeat(conn *connection) (description.Server, *connection) {
443463
const maxRetry = 2
@@ -467,25 +487,7 @@ func (s *Server) heartbeat(conn *connection) (description.Server, *connection) {
467487
}
468488

469489
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)
489491

490492
conn.connect(ctx)
491493

x/mongo/driver/topology/server_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/google/go-cmp/cmp"
1919
"github.com/stretchr/testify/require"
20+
"go.mongodb.org/mongo-driver/internal/testutil/assert"
2021
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
2122
"go.mongodb.org/mongo-driver/x/mongo/driver"
2223
"go.mongodb.org/mongo-driver/x/mongo/driver/address"
@@ -297,6 +298,28 @@ func TestServer(t *testing.T) {
297298
require.Nil(t, err, "error from NewServer: %v", err)
298299
require.Equal(t, name, s.cfg.appname, "expected appname to be: %v, got: %v", name, s.cfg.appname)
299300
})
301+
t.Run("createConnection overwrites WithSocketTimeout", func(t *testing.T) {
302+
socketTimeout := 40 * time.Second
303+
304+
s, err := NewServer(
305+
address.Address("localhost"),
306+
WithConnectionOptions(func(connOpts ...ConnectionOption) []ConnectionOption {
307+
return append(
308+
connOpts,
309+
WithReadTimeout(func(time.Duration) time.Duration { return socketTimeout }),
310+
WithWriteTimeout(func(time.Duration) time.Duration { return socketTimeout }),
311+
)
312+
}),
313+
)
314+
assert.Nil(t, err, "NewServer error: %v", err)
315+
316+
conn, err := s.createConnection(context.Background())
317+
assert.Nil(t, err, "createConnection error: %v", err)
318+
319+
assert.Equal(t, s.cfg.heartbeatTimeout, 10*time.Second, "expected heartbeatTimeout to be: %v, got: %v", 10*time.Second, s.cfg.heartbeatTimeout)
320+
assert.Equal(t, s.cfg.heartbeatTimeout, conn.readTimeout, "expected readTimeout to be: %v, got: %v", s.cfg.heartbeatTimeout, conn.readTimeout)
321+
assert.Equal(t, s.cfg.heartbeatTimeout, conn.writeTimeout, "expected writeTimeout to be: %v, got: %v", s.cfg.heartbeatTimeout, conn.writeTimeout)
322+
})
300323
}
301324

302325
func includesMetadata(t *testing.T, wm []byte) bool {

0 commit comments

Comments
 (0)