Class: EventMachine::DnsResolver::Request

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

Overview

Request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, value, type) ⇒ Request

Returns a new instance of Request.



124
125
126
127
128
129
130
131
132
133
# File 'lib/em-dns-resolver.rb', line 124

def initialize(socket, value, type)
  @socket = socket
  @value = value
  @type = type
  @tries = 0
  @last_send = Time.at(0)
  @retry_interval = 3
  @max_tries = 5
  EM.next_tick { tick }
end

Instance Attribute Details

#max_triesObject

Returns the value of attribute max_tries.



123
124
125
# File 'lib/em-dns-resolver.rb', line 123

def max_tries
  @max_tries
end

#retry_intervalObject

Returns the value of attribute retry_interval.



122
123
124
# File 'lib/em-dns-resolver.rb', line 122

def retry_interval
  @retry_interval
end

Instance Method Details

#receive_answer(msg) ⇒ Object

Called by DnsSocket#receive_data



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/em-dns-resolver.rb', line 151

def receive_answer(msg)
  result = []
  msg.each_answer do |name,ttl,data|
    case data
    when Resolv::DNS::Resource::IN::A, Resolv::DNS::Resource::IN::AAAA
      result << data.address.to_s
    when Resolv::DNS::Resource::IN::PTR
      result << data.name.to_s
    end
  end
  if result.empty?
    fail "rcode=#{msg.rcode}"
  else
    succeed result
  end
end

#tickObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/em-dns-resolver.rb', line 134

def tick
  # @value already contains the response
  if @socket.nil?
    succeed [ @value ]
    return
  end

  # 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