Class: EventMachine::DNS::Request

Inherits:
Object
  • Object
show all
Includes:
EventMachine::Deferrable
Defined in:
lib/em/resolver.rb

Constant Summary

Constants included from EventMachine::Deferrable

EventMachine::Deferrable::Pool

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventMachine::Deferrable

#callback, #cancel_callback, #cancel_errback, #cancel_timeout, #errback, #fail, future, #set_deferred_status, #succeed, #timeout

Constructor Details

#initialize(socket, hostname) ⇒ Request

Returns a new instance of Request.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/em/resolver.rb', line 122

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_triesObject

Returns the value of attribute max_tries.



120
121
122
# File 'lib/em/resolver.rb', line 120

def max_tries
  @max_tries
end

#retry_intervalObject

Returns the value of attribute retry_interval.



120
121
122
# File 'lib/em/resolver.rb', line 120

def retry_interval
  @retry_interval
end

Instance Method Details

#receive_answer(msg) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/em/resolver.rb', line 147

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

#tickObject



137
138
139
140
141
142
143
144
145
# File 'lib/em/resolver.rb', line 137

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