Class: Mongo::Server::RoundTripTimeCalculator Private
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/mongo/server/round_trip_time_calculator.rb |
Overview
Constant Summary
-
MIN_SAMPLES =
private
3
-
RTT_SAMPLES_FOR_MINIMUM =
private
10
-
RTT_WEIGHT_FACTOR =
private
The weighting factor (alpha) for calculating the average moving round trip time.
0.2.freeze
Class Method Summary
- .new ⇒ RoundTripTimeCalculator constructor Internal use only
Instance Attribute Summary
- #average_round_trip_time readonly Internal use only
- #last_round_trip_time readonly Internal use only
- #minimum_round_trip_time readonly Internal use only
Instance Method Summary
- #measure Internal use only
- #update_average_round_trip_time Internal use only
- #update_minimum_round_trip_time Internal use only
Instance Attribute Details
#average_round_trip_time (readonly)
# File 'lib/mongo/server/round_trip_time_calculator.rb', line 43
attr_reader :average_round_trip_time
#last_round_trip_time (readonly)
# File 'lib/mongo/server/round_trip_time_calculator.rb', line 42
attr_reader :last_round_trip_time
#minimum_round_trip_time (readonly)
# File 'lib/mongo/server/round_trip_time_calculator.rb', line 44
attr_reader :minimum_round_trip_time
Instance Method Details
#measure
# File 'lib/mongo/server/round_trip_time_calculator.rb', line 46
def measure start = Utils.monotonic_time begin rv = yield rescue Error::SocketError, Error::SocketTimeoutError # If we encountered a network error, the round-trip is not # complete and thus RTT for it does not make sense. raise rescue Error, Error::AuthError => exc # For other errors, RTT is valid. end last_rtt = Utils.monotonic_time - start # If hello fails, we need to return the last round trip time # because it is used in the heartbeat failed SDAM event, # but we must not update the round trip time recorded in the server. unless exc @last_round_trip_time = last_rtt @lock.synchronize do update_average_round_trip_time update_minimum_round_trip_time end end if exc raise exc else rv end end
#update_average_round_trip_time
# File 'lib/mongo/server/round_trip_time_calculator.rb', line 77
def update_average_round_trip_time @average_round_trip_time = if average_round_trip_time RTT_WEIGHT_FACTOR * last_round_trip_time + (1 - RTT_WEIGHT_FACTOR) * average_round_trip_time else last_round_trip_time end end
#update_minimum_round_trip_time
# File 'lib/mongo/server/round_trip_time_calculator.rb', line 85
def update_minimum_round_trip_time @rtts.push(last_round_trip_time) unless last_round_trip_time.nil? @minimum_round_trip_time = 0 and return if @rtts.size < MIN_SAMPLES @rtts.shift if @rtts.size > RTT_SAMPLES_FOR_MINIMUM @minimum_round_trip_time = @rtts.compact.min end