class Mongo::SRV::Monitor

Polls SRV records for the URI that a cluster was created for and updates the list of servers in the cluster when records change.

@api private

Constants

MIN_RESCAN_FREQUENCY

Attributes

options[R]

Public Class Methods

finalize(thread) click to toggle source
# File lib/mongo/srv/monitor.rb, line 77
def self.finalize(thread)
  Proc.new do
    thread.kill
  end
end
new(cluster, resolver, srv_records, options = nil) click to toggle source
# File lib/mongo/srv/monitor.rb, line 29
def initialize(cluster, resolver, srv_records, options = nil)
  @options = options || {}
  @cluster = cluster
  @resolver = resolver
  @records = srv_records
  @no_records_found = false
end

Public Instance Methods

scan!() click to toggle source
# File lib/mongo/srv/monitor.rb, line 48
def scan!
  @old_hosts = @records.hosts

  begin
    @records = @resolver.get_records(@records.hostname)
  rescue Resolv::ResolvTimeout => e
    log_warn("Timed out trying to resolve hostname #{@records.hostname}")
    return
  rescue Resolv::ResolvError => e
    log_warn("Unable to resolve hostname #{@records.hostname}")
    return
  end

  if @records.empty?
    @no_records_found = true
    return
  end

  @no_records_found = false

  (@old_hosts - @records.hosts).each do |host|
    @cluster.remove(host)
  end

  (@records.hosts - @old_hosts).each do |host|
    @cluster.add(host)
  end
end
start_monitor!() click to toggle source
# File lib/mongo/srv/monitor.rb, line 37
def start_monitor!
  @thread = Thread.new do
    loop do
      sleep(rescan_frequency)
      scan!
    end
  end

  ObjectSpace.define_finalizer(self, self.class.finalize(@thread))
end

Private Instance Methods

rescan_frequency() click to toggle source
# File lib/mongo/srv/monitor.rb, line 85
def rescan_frequency
  if @no_records_found
    Server:: Monitor::HEARTBEAT_FREQUENCY
  elsif @records.min_ttl.nil?
    MIN_RESCAN_FREQUENCY
  else
    [@records.min_ttl, MIN_RESCAN_FREQUENCY].max
  end
end