Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions x/mongo/driver/topology/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,27 +795,30 @@ func (s *Server) check() (description.Server, error) {
var duration time.Duration

start := time.Now()

// Create a new connection if this is the first check, the connection was closed after an error during the previous
// check, or the previous check was cancelled.
if s.conn == nil || s.conn.closed() || s.checkWasCancelled() {
// Create a new connection if this is the first check, the connection was closed after an error during the previous
// check, or the previous check was cancelled.
connID := "0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A connection ID of "0" doesn't match the existing pattern of "<addr>[-<number>]" (see here). Using an empty string seems more correct for when the connection is nil.

if s.conn != nil {
s.publishServerHeartbeatStartedEvent(s.conn.ID(), false)
connID = s.conn.ID()
}
s.publishServerHeartbeatStartedEvent(connID, false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do something similar for the subsequent publishServerHeartbeatSucceededEvent and publishServerHeartbeatFailedEvent so they always get published, even if the conn is nil. That's especially important for the failed event so we can report the error.

// Create a new connection and add it's handshake RTT as a sample.
err = s.setupHeartbeatConnection()
duration = time.Since(start)
connID = "0"
if s.conn != nil {
connID = s.conn.ID()
}
if err == nil {
// Use the description from the connection handshake as the value for this check.
s.rttMonitor.addSample(s.conn.helloRTT)
descPtr = &s.conn.desc
if s.conn != nil {
s.publishServerHeartbeatSucceededEvent(s.conn.ID(), duration, s.conn.desc, false)
}
s.publishServerHeartbeatSucceededEvent(connID, duration, s.conn.desc, false)
} else {
err = unwrapConnectionError(err)
if s.conn != nil {
s.publishServerHeartbeatFailedEvent(s.conn.ID(), duration, err, false)
}
s.publishServerHeartbeatFailedEvent(connID, duration, err, false)
}
} else {
// An existing connection is being used. Use the server description properties to execute the right heartbeat.
Expand Down
7 changes: 6 additions & 1 deletion x/mongo/driver/topology/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,12 @@ func TestServerHeartbeatTimeout(t *testing.T) {
}),
WithServerMonitor(func(*event.ServerMonitor) *event.ServerMonitor {
return &event.ServerMonitor{
ServerHeartbeatStarted: func(e *event.ServerHeartbeatStartedEvent) {
ServerHeartbeatSucceeded: func(e *event.ServerHeartbeatSucceededEvent) {
if !errors.dequeue() {
wg.Done()
}
},
ServerHeartbeatFailed: func(e *event.ServerHeartbeatFailedEvent) {
if !errors.dequeue() {
wg.Done()
}
Expand Down