Class: HTTPX::Resolver::System

Inherits:
Resolver
  • Object
show all
Defined in:
lib/httpx/resolver/system.rb

Overview

Implementation of a synchronous name resolver which relies on the system resolver, which is lib’c getaddrinfo function (abstracted in ruby via Addrinfo.getaddrinfo).

Its main advantage is relying on the reference implementation for name resolution across most/all OSs which deploy ruby (it’s what TCPSocket also uses), its main disadvantage is the inability to set timeouts / check socket for readiness events, hence why it relies on using the Timeout module, which poses a lot of problems for the selector loop, specially when network is unstable.

Constant Summary collapse

RESOLV_ERRORS =
[Resolv::ResolvError,
Resolv::DNS::Requester::RequestError,
Resolv::DNS::EncodeError,
Resolv::DNS::DecodeError].freeze
DONE =
1
ERROR =
2

Constants inherited from Resolver

Resolver::FAMILY_TYPES, Resolver::RECORD_TYPES

Constants included from Loggable

Loggable::COLORS, Loggable::USE_DEBUG_LOG

Instance Attribute Summary collapse

Attributes inherited from Resolver

#current_selector, #current_session, #family, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resolver

#each_connection, #emit_addresses, #inflight?

Methods included from Loggable

#log, #log_exception, #log_redact

Methods included from Callbacks

#callbacks_for?, #emit, #on, #once

Constructor Details

#initialize(options) ⇒ System

Returns a new instance of System.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/httpx/resolver/system.rb', line 34

def initialize(options)
  super(0, options)
  @resolver_options = @options.resolver_options
  resolv_options = @resolver_options.dup
  timeouts = resolv_options.delete(:timeouts) || Resolver::RESOLVE_TIMEOUT
  @_timeouts = Array(timeouts)
  @timeouts = Hash.new { |tims, host| tims[host] = @_timeouts.dup }
  resolv_options.delete(:cache)
  @queries = []
  @ips = []
  @pipe_mutex = Thread::Mutex.new
  @state = :idle
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



32
33
34
# File 'lib/httpx/resolver/system.rb', line 32

def state
  @state
end

Class Method Details

.multi?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/httpx/resolver/system.rb', line 27

def multi?
  false
end

Instance Method Details

#<<(connection) ⇒ Object



98
99
100
101
# File 'lib/httpx/resolver/system.rb', line 98

def <<(connection)
  @connections << connection
  resolve
end

#callObject



74
75
76
77
78
79
80
# File 'lib/httpx/resolver/system.rb', line 74

def call
  case @state
  when :open
    consume
  end
  nil
end

#closeObject



62
63
64
# File 'lib/httpx/resolver/system.rb', line 62

def close
  transition(:closed)
end

#closed?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/httpx/resolver/system.rb', line 66

def closed?
  @state == :closed
end

#early_resolve(connection) ⇒ Object



103
104
105
106
# File 'lib/httpx/resolver/system.rb', line 103

def early_resolve(connection, **)
  self << connection
  true
end

#empty?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/httpx/resolver/system.rb', line 58

def empty?
  true
end

#handle_socket_timeout(interval) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/httpx/resolver/system.rb', line 108

def handle_socket_timeout(interval)
  error = HTTPX::ResolveTimeoutError.new(interval, "timed out while waiting on select")
  error.set_backtrace(caller)
  @queries.each do |host, connection|
    @connections.delete(connection)
    emit_resolve_error(connection, host, error)
  end

  while (connection = @connections.shift)
    emit_resolve_error(connection, connection.peer.host, error)
  end
end

#interestsObject



82
83
84
85
86
# File 'lib/httpx/resolver/system.rb', line 82

def interests
  return if @queries.empty?

  :r
end

#multiObject



54
55
56
# File 'lib/httpx/resolver/system.rb', line 54

def multi
  self
end

#resolvers {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



48
49
50
51
52
# File 'lib/httpx/resolver/system.rb', line 48

def resolvers
  return enum_for(__method__) unless block_given?

  yield self
end

#timeoutObject



88
89
90
91
92
93
94
95
96
# File 'lib/httpx/resolver/system.rb', line 88

def timeout
  return unless @queries.empty?

  _, connection = @queries.first

  return unless connection

  @timeouts[connection.peer.host].first
end

#to_ioObject



70
71
72
# File 'lib/httpx/resolver/system.rb', line 70

def to_io
  @pipe_read.to_io
end