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, #handle_error, #inflight?, #on_error

Methods included from Loggable

#log, #log_exception, #log_redact, #log_redact_body, #log_redact_headers

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

#callObject



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

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)


74
75
76
# File 'lib/httpx/resolver/system.rb', line 74

def closed?
  @state == :closed
end

#early_resolve(connection) ⇒ Object



119
# File 'lib/httpx/resolver/system.rb', line 119

def early_resolve(connection, **); end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @connections.empty?
end

#force_closeObject



66
67
68
69
70
71
72
# File 'lib/httpx/resolver/system.rb', line 66

def force_close(*)
  close
  @queries.clear
  @timeouts.clear
  @ips.clear
  super
end

#handle_socket_timeout(interval) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/httpx/resolver/system.rb', line 121

def handle_socket_timeout(interval)
  error = HTTPX::ResolveTimeoutError.new(interval, "timed out while waiting on select")
  error.set_backtrace(caller)
  @queries.each do |_, connection| # rubocop:disable Style/HashEachMethods
    emit_resolve_error(connection, connection.peer.host, error) if @connections.delete(connection)
  end

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

  close_or_resolve
end

#interestsObject



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

def interests
  return if @queries.empty?

  :r
end

#lazy_resolve(connection) ⇒ Object



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

def lazy_resolve(connection)
  @connections << connection
  resolve

  return if empty?

  @current_session.select_resolver(self, @current_selector)
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



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/httpx/resolver/system.rb', line 96

def timeout
  _, connection = @queries.first

  return unless connection

  timeouts = @timeouts[connection.peer.host]

  return if timeouts.empty?

  log(level: 2) { "resolver #{FAMILY_TYPES[@record_type]}: next timeout #{timeouts.first} secs... (#{timeouts.size - 1} left)" }

  timeouts.first
end

#to_ioObject



78
79
80
# File 'lib/httpx/resolver/system.rb', line 78

def to_io
  @pipe_read.to_io
end