Skip to content

Commit d3ceaf3

Browse files
author
Isabella Siu
committed
GODRIVER-1695 always use heartbeatTimeout for heartbeat connection (#470)
1 parent 35c4b30 commit d3ceaf3

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

x/mongo/driver/topology/server.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -546,18 +546,15 @@ func (s *Server) createConnection() (*connection, error) {
546546
WithConnectTimeout(func(time.Duration) time.Duration { return s.cfg.heartbeatTimeout }),
547547
WithReadTimeout(func(time.Duration) time.Duration { return s.cfg.heartbeatTimeout }),
548548
WithWriteTimeout(func(time.Duration) time.Duration { return s.cfg.heartbeatTimeout }),
549-
}
550-
opts = append(opts, s.cfg.connectionOpts...)
551-
// We override whatever handshaker is currently attached to the options with a basic
552-
// one because need to make sure we don't do auth.
553-
opts = append(opts, WithHandshaker(func(h Handshaker) Handshaker {
554-
return operation.NewIsMaster().AppName(s.cfg.appname).Compressors(s.cfg.compressionOpts)
555-
}))
556-
557-
// Override any command monitors specified in options with nil to avoid monitoring heartbeats.
558-
opts = append(opts, WithMonitor(func(*event.CommandMonitor) *event.CommandMonitor {
559-
return nil
560-
}))
549+
// We override whatever handshaker is currently attached to the options with a basic
550+
// one because need to make sure we don't do auth.
551+
WithHandshaker(func(h Handshaker) Handshaker {
552+
return operation.NewIsMaster().AppName(s.cfg.appname).Compressors(s.cfg.compressionOpts)
553+
}),
554+
// Override any command monitors specified in options with nil to avoid monitoring heartbeats.
555+
WithMonitor(func(*event.CommandMonitor) *event.CommandMonitor { return nil }),
556+
}
557+
opts = append(s.cfg.connectionOpts, opts...)
561558

562559
return newConnection(s.address, opts...)
563560
}

x/mongo/driver/topology/server_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,28 @@ func TestServer(t *testing.T) {
312312
require.Nil(t, err, "error from NewServer: %v", err)
313313
require.Equal(t, name, s.cfg.appname, "expected appname to be: %v, got: %v", name, s.cfg.appname)
314314
})
315+
t.Run("createConnection overwrites WithSocketTimeout", func(t *testing.T) {
316+
socketTimeout := 40 * time.Second
317+
318+
s, err := NewServer(
319+
address.Address("localhost"),
320+
WithConnectionOptions(func(connOpts ...ConnectionOption) []ConnectionOption {
321+
return append(
322+
connOpts,
323+
WithReadTimeout(func(time.Duration) time.Duration { return socketTimeout }),
324+
WithWriteTimeout(func(time.Duration) time.Duration { return socketTimeout }),
325+
)
326+
}),
327+
)
328+
assert.Nil(t, err, "NewServer error: %v", err)
329+
330+
conn, err := s.createConnection()
331+
assert.Nil(t, err, "createConnection error: %v", err)
332+
333+
assert.Equal(t, s.cfg.heartbeatTimeout, 10*time.Second, "expected heartbeatTimeout to be: %v, got: %v", 10*time.Second, s.cfg.heartbeatTimeout)
334+
assert.Equal(t, s.cfg.heartbeatTimeout, conn.readTimeout, "expected readTimeout to be: %v, got: %v", s.cfg.heartbeatTimeout, conn.readTimeout)
335+
assert.Equal(t, s.cfg.heartbeatTimeout, conn.writeTimeout, "expected writeTimeout to be: %v, got: %v", s.cfg.heartbeatTimeout, conn.writeTimeout)
336+
})
315337
}
316338

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

0 commit comments

Comments
 (0)