class Mongo::Server::RoundTripTimeAverager

@api private

Constants

RTT_WEIGHT_FACTOR

The weighting factor (alpha) for calculating the average moving round trip time.

Attributes

average_round_trip_time[R]
last_round_trip_time[R]

Public Class Methods

new() click to toggle source
# File lib/mongo/server/round_trip_time_averager.rb, line 25
def initialize
  @last_round_trip_time = nil
  @average_round_trip_time = nil
end

Public Instance Methods

measure() { || ... } click to toggle source
# File lib/mongo/server/round_trip_time_averager.rb, line 33
def measure
  start = Time.now
  begin
    rv = yield
  rescue Exception => exc
  end
  last_round_trip_time = Time.now - start

  # If ismaster 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_round_trip_time
    update_average_round_trip_time
  end

  [rv, exc, last_round_trip_time, average_round_trip_time]
end

Private Instance Methods

update_average_round_trip_time() click to toggle source

This method is separate for testing purposes.

# File lib/mongo/server/round_trip_time_averager.rb, line 55
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