123456789_123456789_123456789_123456789_123456789_

Class: EventMachine::DNS::Request

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Deferrable
Inherits: Object
Defined in: lib/em/resolver.rb

Constant Summary

Deferrable - Included

Pool

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Deferrable - Included

#callback

Specify a block to be executed if and when the Deferrable object receives a status of :succeeded.

#cancel_callback

Cancels an outstanding callback to &block if any.

#cancel_errback

Cancels an outstanding errback to &block if any.

#cancel_timeout

Cancels an outstanding timeout if any.

#errback

Specify a block to be executed if and when the Deferrable object receives a status of :failed.

#fail

Sugar for set_deferred_status(:failed, ...).

#set_deferred_failure
#set_deferred_status

Sets the "disposition" (status) of the Deferrable object.

#set_deferred_success
#succeed

Sugar for set_deferred_status(:succeeded, ...).

#timeout

Setting a timeout on a Deferrable causes it to go into the failed state after the Timeout expires (passing no arguments to the object's errbacks).

Constructor Details

.new(socket, hostname) ⇒ Request

[ GitHub ]

  
# File 'lib/em/resolver.rb', line 161

def initialize(socket, hostname)
  @socket = socket
  @hostname = hostname
  @tries = 0
  @last_send = Time.at(0)
  @retry_interval = 3
  @max_tries = 5

  if addrs = Resolver.hosts[hostname]
    succeed addrs
  else
    EM.next_tick { tick }
  end
end

Instance Attribute Details

#max_tries (rw)

[ GitHub ]

  
# File 'lib/em/resolver.rb', line 159

attr_accessor :retry_interval, :max_tries

#retry_interval (rw)

[ GitHub ]

  
# File 'lib/em/resolver.rb', line 159

attr_accessor :retry_interval, :max_tries

Instance Method Details

#id (private)

[ GitHub ]

  
# File 'lib/em/resolver.rb', line 211

def id
  begin
    @id = rand(65535)
    @socket.register_request(@id, self)
  rescue RequestIdAlreadyUsed
    retry
  end unless defined?(@id)

  @id
end

#packet (private)

[ GitHub ]

  
# File 'lib/em/resolver.rb', line 222

def packet
  msg = Resolv::DNS::Message.new
  msg.id = id
  msg.rd = 1
  msg.add_question @hostname, Resolv::DNS::Resource::IN::A
  msg
end

#receive_answer(msg)

[ GitHub ]

  
# File 'lib/em/resolver.rb', line 187

def receive_answer(msg)
  addrs = []
  msg.each_answer do |name,ttl,data|
    if data.kind_of?(Resolv::DNS::Resource::IN::A) ||
        data.kind_of?(Resolv::DNS::Resource::IN::AAAA)
      addrs << data.address.to_s
    end
  end

  if addrs.empty?
    fail "rcode=#{msg.rcode}"
  else
    succeed addrs
  end
end

#send (private)

[ GitHub ]

  
# File 'lib/em/resolver.rb', line 205

def send
  @tries += 1
  @last_send = Time.now
  @socket.send_packet(packet.encode)
end

#tick

[ GitHub ]

  
# File 'lib/em/resolver.rb', line 176

def tick
  # Break early if nothing to do
  return if @last_send + @retry_interval > Time.now
  if @tries < @max_tries
    send
  else
    @socket.deregister_request(@id, self)
    fail 'retries exceeded'
  end
end