Module: EventMachine::DnsCache

Defined in:
lib/em/dns_cache.rb

Defined Under Namespace

Classes: Cache, MxQuery, Request, Socket

Constant Summary collapse

MAX_WAITING =
100

Class Method Summary collapse

Class Method Details

.add_cache_entry(cache_type, domain, value, expiration) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/em/dns_cache.rb', line 61

def self.add_cache_entry cache_type, domain, value, expiration
  cache = if cache_type == :mx
            @mx_cache
          elsif cache_type == :a
            @a_cache
          else
            raise "bad cache type"
          end

  v = EM::DefaultDeferrable.new
  v.succeed( value.dup.freeze )
  cache.add domain, v, expiration
end

.add_nameserver(ns) ⇒ Object



42
43
44
# File 'lib/em/dns_cache.rb', line 42

def self.add_nameserver ns
  @nameservers << ns unless @nameservers.include?(ns)
end

.add_nameservers_from_file(file = '/etc/resolv.conf') ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/em/dns_cache.rb', line 46

def self.add_nameservers_from_file file='/etc/resolv.conf'
  IO::readlines(file).each do |line|
    if line =~ /^nameserver (.+)$/
      $1.split(/\s+/).each { |ns|
        @nameservers << ns unless ns.empty?
      }
    end
  end
end

.lazy_initializeObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/em/dns_cache.rb', line 218

def self.lazy_initialize
  # Will throw an exception if EM is not running.
  # We wire a signaller into the socket handler to tell us when that socket
  # goes away. (Which can happen, among other things, if the reactor
  # stops and restarts.)
  #
  raise "EventMachine reactor not running" unless EM.reactor_running?

  unless @u
    us = proc {@u = nil}
    @u = EM::open_datagram_socket( "0.0.0.0", 0, Socket ) {|c|
      c.unbind_signaller = us
    }
  end

end

.look_pendingObject



120
121
122
123
124
125
126
127
128
# File 'lib/em/dns_cache.rb', line 120

def self.look_pending
  EM.next_tick {
    while @waiting < MAX_WAITING && !@pending.empty?
      pending1 = @pending.shift
      pending1.call
      puts "#{@pending.size} pending requests now" if @verbose
    end
  }
end

.parse_local_mx_records(txt) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/em/dns_cache.rb', line 236

def self.parse_local_mx_records txt
  domain = nil
  addrs = []

  add_it = proc {
    a = addrs.sort {|m,n| m.last <=> n.last}.map {|y| y.first}
    add_cache_entry :mx, domain, a, -1
  }

  txt = StringIO.new( txt ) if txt.is_a?(String)
  txt.each_line {|ln|
    if ln =~ /\A\s*([\d\w\.\-\_]+)\s+(\d+)\s*\Z/
      if domain
        addrs << [$1.dup, $2.dup.to_i]
      end
    elsif ln =~ /\A\s*([^\s\:]+)\s*\:\s*\Z/
      add_it.call if domain
      domain = $1.dup
      addrs.clear
    end
  }

  add_it.call if domain
end

.resolve(domain) ⇒ Object

Needs to be DRYed up with resolve_mx.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/em/dns_cache.rb', line 77

def self.resolve domain
  if d = @a_cache.retrieve(domain)
    puts "Cache hit for #{domain}" if @verbose
    look_pending
    d
  else
=begin
    d = @a_cache[domain]
    if d.first < Time.now
      STDOUT.puts "Expiring stale cache entry for #{domain}" if @verbose
      @a_cache.delete domain
      resolve domain
    else
      STDOUT.puts "Fulfilled #{domain} from cache" if @verbose
      d.last
    end
  else
=end
    if @waiting >= MAX_WAITING
      puts "Postponing #{domain} because already waiting for #{@waiting} queries" if @verbose
      d = EM::DefaultDeferrable.new
      @pending << lambda {
        d_inner = resolve domain
        d_inner.callback &d.method(:succeed)
        d_inner.errback &d.method(:fail)
      }
      puts "#{@pending.size} pending requests now" if @verbose
      d
    else
      d = resolve_do domain
      @waiting += 1
      STDOUT.puts "Now waiting for #{@waiting}" if @verbose
      on_one_done = lambda {
        @waiting -= 1
        look_pending
      }
      d.callback &on_one_done
      d.errback &on_one_done
      d
    end
  end
end

.resolve_do(domain) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/em/dns_cache.rb', line 130

def self.resolve_do domain
  STDOUT.puts "Fulfilling #{domain} from network" if @verbose
  d = EM::DefaultDeferrable.new
  #d.timeout(5)
  d.callback { d.cancel_timeout }
  d.errback { d.cancel_timeout }
  @a_cache.add domain, d, 300 # Hard-code a 5 minute expiration
  #@a_cache[domain] = [Time.now+120, d] # Hard code a 120-second expiration.

  lazy_initialize
  m = Resolv::DNS::Message.new
  m.rd = 1
  m.add_question domain, Resolv::DNS::Resource::IN::A
  m = m.encode
  d_inner = EM::DefaultDeferrable.new
  @nameservers.each {|ns|
    @message_ix = (@message_ix + 1) % 60000
    Request.new d_inner, @message_ix
    msg = m.dup
    msg[0,2] = [@message_ix].pack("n")
    @u.send_datagram msg, ns, 53
  }

  d_inner.callback {|resp|
    r = []
    resp.each_answer {|name,ttl,data|
      r << data.address.to_s if data.kind_of?(Resolv::DNS::Resource::IN::A)
    }

    # Freeze the array since we'll be keeping it in cache and passing it
    # around to multiple users. And alternative would have been to dup it.
    r.freeze
    puts "Succeeding with #{r.inspect}"
    d.succeed r
  }
  d_inner.errback &d.method(:fail)


  d
end

.resolve_mx(domain) ⇒ Object

Needs to be DRYed up with resolve.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/em/dns_cache.rb', line 174

def self.resolve_mx domain
  if d = @mx_cache.retrieve(domain)
    d
  else
=begin
       if @mx_cache.has_key?(domain)
         d = @mx_cache[domain]
         if d.first < Time.now
           STDOUT.puts "Expiring stale cache entry for #{domain}" if @verbose
           @mx_cache.delete domain
           resolve_mx domain
         else
           STDOUT.puts "Fulfilled #{domain} from cache" if @verbose
           d.last
         end
       else
=end
    STDOUT.puts "Fulfilling #{domain} from network" if @verbose
    d = EM::DefaultDeferrable.new
    d.timeout(5)
    #@mx_cache[domain] = [Time.now+120, d] # Hard code a 120-second expiration.
    @mx_cache.add domain, d, 300 # Hard-code a 5 minute expiration

    mx_query = MxQuery.new d

    lazy_initialize
    m = Resolv::DNS::Message.new
    m.rd = 1
    m.add_question domain, Resolv::DNS::Resource::IN::MX
    m = m.encode
    @nameservers.each {|ns|
      @message_ix = (@message_ix + 1) % 60000
      Request.new mx_query, @message_ix
      msg = m.dup
      msg[0,2] = [@message_ix].pack("n")
      @u.send_datagram msg, ns, 53
    }


    d
  end
end

.verbose(v = true) ⇒ Object



56
57
58
# File 'lib/em/dns_cache.rb', line 56

def self.verbose v=true
  @verbose = v
end