Skip to content

Commit 3ea25e0

Browse files
authored
RUBY-1588 Do not update round trip time on failed ismaster calls (#1167)
1 parent 0c05983 commit 3ea25e0

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

lib/mongo/server/description.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,7 @@ class Description
189189
# call took to complete.
190190
#
191191
# @since 2.0.0
192-
def initialize(address, config = {}, average_round_trip_time = 0)
193-
if average_round_trip_time.nil?
194-
raise ArgumentError, 'Average round trip time cannot be nil'
195-
end
192+
def initialize(address, config = {}, average_round_trip_time = nil)
196193
@address = address
197194
@config = config
198195
unless unknown?

lib/mongo/server/round_trip_time_averager.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ def measure
3636
rv = yield
3737
rescue Exception => exc
3838
end
39-
@last_round_trip_time = Time.now - start
39+
last_round_trip_time = Time.now - start
4040

41-
update_average_round_trip_time
41+
# If ismaster fails, we need to return the last round trip time
42+
# because it is used in the heartbeat failed SDAM event,
43+
# but we must not update the round trip time recorded in the server.
44+
unless exc
45+
@last_round_trip_time = last_round_trip_time
46+
update_average_round_trip_time
47+
end
4248

4349
[rv, exc, last_round_trip_time, average_round_trip_time]
4450
end

spec/mongo/server/description_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@
327327
described_class.new(address, { 'secondary' => false }, 4.5)
328328
end
329329

330-
it 'defaults to 0' do
331-
expect(described_class.new(address).average_round_trip_time).to eq(0)
330+
it 'defaults to nil' do
331+
expect(described_class.new(address).average_round_trip_time).to be nil
332332
end
333333

334334
it 'can be set via the constructor' do

spec/mongo/server/round_trip_time_averager_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,23 @@
2121
end
2222
end
2323
end
24+
25+
describe '#measure' do
26+
context 'block does not raise' do
27+
it 'updates average rtt' do
28+
expect(averager).to receive(:update_average_round_trip_time)
29+
averager.measure do
30+
end
31+
end
32+
end
33+
34+
context 'block raises' do
35+
it 'does not update average rtt' do
36+
expect(averager).not_to receive(:update_average_round_trip_time)
37+
averager.measure do
38+
raise "Problem"
39+
end
40+
end
41+
end
42+
end
2443
end

0 commit comments

Comments
 (0)