Class: FinalDestination::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/final_destination/resolver.rb

Class Method Summary collapse

Class Method Details

.lookup(addr, timeout: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/final_destination/resolver.rb', line 5

def self.lookup(addr, timeout: nil)
  timeout ||= default_dns_query_timeout

  @mutex.synchronize do
    @result = nil

    @queue ||= Queue.new
    ensure_lookup_thread

    @lookup = addr
    @parent = Thread.current

    # Wakeup the worker thread
    @queue << ""

    # This sleep will be interrupted by the lookup thread
    # if completed within timeout
    sleep timeout
    if !@result
      @thread.kill
      @thread.join
      @thread = nil
      if @error
        @error.backtrace.push(*caller)
        raise @error
      else
        raise Timeout::Error
      end
    end
    @result
  end
end