Skip to content

Commit

Permalink
GODRIVER-1695 always use heartbeatTimeout for heartbeat connection (#470
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Isabella Siu committed Jul 30, 2020
1 parent 35c4b30 commit d3ceaf3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
21 changes: 9 additions & 12 deletions x/mongo/driver/topology/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,18 +546,15 @@ func (s *Server) createConnection() (*connection, error) {
WithConnectTimeout(func(time.Duration) time.Duration { return s.cfg.heartbeatTimeout }),
WithReadTimeout(func(time.Duration) time.Duration { return s.cfg.heartbeatTimeout }),
WithWriteTimeout(func(time.Duration) time.Duration { return s.cfg.heartbeatTimeout }),
}
opts = append(opts, s.cfg.connectionOpts...)
// We override whatever handshaker is currently attached to the options with a basic
// one because need to make sure we don't do auth.
opts = append(opts, WithHandshaker(func(h Handshaker) Handshaker {
return operation.NewIsMaster().AppName(s.cfg.appname).Compressors(s.cfg.compressionOpts)
}))

// Override any command monitors specified in options with nil to avoid monitoring heartbeats.
opts = append(opts, WithMonitor(func(*event.CommandMonitor) *event.CommandMonitor {
return nil
}))
// We override whatever handshaker is currently attached to the options with a basic
// one because need to make sure we don't do auth.
WithHandshaker(func(h Handshaker) Handshaker {
return operation.NewIsMaster().AppName(s.cfg.appname).Compressors(s.cfg.compressionOpts)
}),
// Override any command monitors specified in options with nil to avoid monitoring heartbeats.
WithMonitor(func(*event.CommandMonitor) *event.CommandMonitor { return nil }),
}
opts = append(s.cfg.connectionOpts, opts...)

return newConnection(s.address, opts...)
}
Expand Down
22 changes: 22 additions & 0 deletions x/mongo/driver/topology/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,28 @@ func TestServer(t *testing.T) {
require.Nil(t, err, "error from NewServer: %v", err)
require.Equal(t, name, s.cfg.appname, "expected appname to be: %v, got: %v", name, s.cfg.appname)
})
t.Run("createConnection overwrites WithSocketTimeout", func(t *testing.T) {
socketTimeout := 40 * time.Second

s, err := NewServer(
address.Address("localhost"),
WithConnectionOptions(func(connOpts ...ConnectionOption) []ConnectionOption {
return append(
connOpts,
WithReadTimeout(func(time.Duration) time.Duration { return socketTimeout }),
WithWriteTimeout(func(time.Duration) time.Duration { return socketTimeout }),
)
}),
)
assert.Nil(t, err, "NewServer error: %v", err)

conn, err := s.createConnection()
assert.Nil(t, err, "createConnection error: %v", err)

assert.Equal(t, s.cfg.heartbeatTimeout, 10*time.Second, "expected heartbeatTimeout to be: %v, got: %v", 10*time.Second, s.cfg.heartbeatTimeout)
assert.Equal(t, s.cfg.heartbeatTimeout, conn.readTimeout, "expected readTimeout to be: %v, got: %v", s.cfg.heartbeatTimeout, conn.readTimeout)
assert.Equal(t, s.cfg.heartbeatTimeout, conn.writeTimeout, "expected writeTimeout to be: %v, got: %v", s.cfg.heartbeatTimeout, conn.writeTimeout)
})
}

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

0 comments on commit d3ceaf3

Please sign in to comment.