Skip to content

Commit c0caefe

Browse files
GODRIVER-2966 [master] Remove the heartbeat events after a connection is closed / a check is canceled. (#1409)
Co-authored-by: Qingyang Hu <103950869+qingyang-hu@users.noreply.github.com>
1 parent d6ed7ca commit c0caefe

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

x/mongo/driver/topology/server.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -795,27 +795,30 @@ func (s *Server) check() (description.Server, error) {
795795
var duration time.Duration
796796

797797
start := time.Now()
798+
799+
// Create a new connection if this is the first check, the connection was closed after an error during the previous
800+
// check, or the previous check was cancelled.
798801
if s.conn == nil || s.conn.closed() || s.checkWasCancelled() {
799-
// Create a new connection if this is the first check, the connection was closed after an error during the previous
800-
// check, or the previous check was cancelled.
802+
connID := "0"
801803
if s.conn != nil {
802-
s.publishServerHeartbeatStartedEvent(s.conn.ID(), false)
804+
connID = s.conn.ID()
803805
}
806+
s.publishServerHeartbeatStartedEvent(connID, false)
804807
// Create a new connection and add it's handshake RTT as a sample.
805808
err = s.setupHeartbeatConnection()
806809
duration = time.Since(start)
810+
connID = "0"
811+
if s.conn != nil {
812+
connID = s.conn.ID()
813+
}
807814
if err == nil {
808815
// Use the description from the connection handshake as the value for this check.
809816
s.rttMonitor.addSample(s.conn.helloRTT)
810817
descPtr = &s.conn.desc
811-
if s.conn != nil {
812-
s.publishServerHeartbeatSucceededEvent(s.conn.ID(), duration, s.conn.desc, false)
813-
}
818+
s.publishServerHeartbeatSucceededEvent(connID, duration, s.conn.desc, false)
814819
} else {
815820
err = unwrapConnectionError(err)
816-
if s.conn != nil {
817-
s.publishServerHeartbeatFailedEvent(s.conn.ID(), duration, err, false)
818-
}
821+
s.publishServerHeartbeatFailedEvent(connID, duration, err, false)
819822
}
820823
} else {
821824
// An existing connection is being used. Use the server description properties to execute the right heartbeat.

x/mongo/driver/topology/server_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ func TestServerHeartbeatTimeout(t *testing.T) {
177177
}),
178178
WithServerMonitor(func(*event.ServerMonitor) *event.ServerMonitor {
179179
return &event.ServerMonitor{
180-
ServerHeartbeatStarted: func(e *event.ServerHeartbeatStartedEvent) {
180+
ServerHeartbeatSucceeded: func(e *event.ServerHeartbeatSucceededEvent) {
181+
if !errors.dequeue() {
182+
wg.Done()
183+
}
184+
},
185+
ServerHeartbeatFailed: func(e *event.ServerHeartbeatFailedEvent) {
181186
if !errors.dequeue() {
182187
wg.Done()
183188
}

0 commit comments

Comments
 (0)