Skip to content

Commit 2614f04

Browse files
author
Divjot Arora
authored
GODRIVER-1708 Set starting time for monitoring connection handshake (#475)
1 parent 06c646d commit 2614f04

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

x/mongo/driver/topology/server.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,17 @@ func (s *Server) updateDescription(desc description.Server) {
439439
}
440440

441441
// 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) {
442+
// before the connection can be used for network operations. The nowPtr pointer will be set to the time right before
443+
// the connection handshake starts and can be used by the caller to measure RTT for the connection handshake.
444+
func (s *Server) createConnection(ctx context.Context, nowPtr *time.Time) (*connection, error) {
444445
opts := []ConnectionOption{
445446
WithConnectTimeout(func(time.Duration) time.Duration { return s.cfg.heartbeatTimeout }),
446447
WithReadTimeout(func(time.Duration) time.Duration { return s.cfg.heartbeatTimeout }),
447448
WithWriteTimeout(func(time.Duration) time.Duration { return s.cfg.heartbeatTimeout }),
448449
// We override whatever handshaker is currently attached to the options with a basic
449450
// one because need to make sure we don't do auth.
450451
WithHandshaker(func(h Handshaker) Handshaker {
452+
*nowPtr = time.Now()
451453
return operation.NewIsMaster().AppName(s.cfg.appname).Compressors(s.cfg.compressionOpts)
452454
}),
453455
// Override any command monitors specified in options with nil to avoid monitoring heartbeats.
@@ -487,7 +489,7 @@ func (s *Server) heartbeat(conn *connection) (description.Server, *connection) {
487489
}
488490

489491
if conn == nil {
490-
conn, err = s.createConnection(ctx)
492+
conn, err = s.createConnection(ctx, &now)
491493

492494
conn.connect(ctx)
493495

x/mongo/driver/topology/server_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,34 @@ func TestServer(t *testing.T) {
313313
)
314314
assert.Nil(t, err, "NewServer error: %v", err)
315315

316-
conn, err := s.createConnection(context.Background())
316+
var now time.Time
317+
conn, err := s.createConnection(context.Background(), &now)
317318
assert.Nil(t, err, "createConnection error: %v", err)
318319

319320
assert.Equal(t, s.cfg.heartbeatTimeout, 10*time.Second, "expected heartbeatTimeout to be: %v, got: %v", 10*time.Second, s.cfg.heartbeatTimeout)
320321
assert.Equal(t, s.cfg.heartbeatTimeout, conn.readTimeout, "expected readTimeout to be: %v, got: %v", s.cfg.heartbeatTimeout, conn.readTimeout)
321322
assert.Equal(t, s.cfg.heartbeatTimeout, conn.writeTimeout, "expected writeTimeout to be: %v, got: %v", s.cfg.heartbeatTimeout, conn.writeTimeout)
322323
})
324+
t.Run("heartbeat connection RTT is accurately measured", func(t *testing.T) {
325+
dialer := &channelNetConnDialer{}
326+
327+
s, err := NewServer(
328+
address.Address("localhost"),
329+
WithConnectionOptions(func(connOpts ...ConnectionOption) []ConnectionOption {
330+
return append(
331+
connOpts,
332+
WithDialer(func(Dialer) Dialer {
333+
return dialer
334+
}),
335+
)
336+
}),
337+
)
338+
assert.Nil(t, err, "NewServer error: %v", err)
339+
340+
_, _ = s.heartbeat(nil)
341+
assert.True(t, s.averageRTTSet, "expected averageRTTSet to be true but was not")
342+
assert.True(t, s.averageRTT < time.Second, "expected average RTT to be less than 1s, got %v", s.averageRTT)
343+
})
323344
}
324345

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

0 commit comments

Comments
 (0)