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.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# 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_triesObject

Returns the value of attribute max_tries.



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

def max_tries
  @max_tries
end

#retry_intervalObject

Returns the value of attribute retry_interval.



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

def retry_interval
  @retry_interval
end

Instance Method Details

#receive_answer(msg) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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

#tickObject



176
177
178
179
180
181
182
183
184
185
# 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