Class: HTTPX::TCP
- Inherits:
-
Object
- Object
- HTTPX::TCP
- Includes:
- Loggable
- Defined in:
- lib/httpx/io/tcp.rb,
sig/io/tcp.rbs
Constant Summary
Constants included from Loggable
Loggable::COLORS, Loggable::USE_DEBUG_LOG
Instance Attribute Summary collapse
-
#addresses ⇒ Array[Resolver::Entry]
readonly
Returns the value of attribute addresses.
-
#interests ⇒ io_interests
readonly
Returns the value of attribute interests.
-
#ip ⇒ Resolver::Entry?
(also: #host)
readonly
Returns the value of attribute ip.
-
#port ⇒ Integer
readonly
Returns the value of attribute port.
-
#state ⇒ Symbol
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #add_addresses(addrs) ⇒ void
-
#addresses? ⇒ Boolean
eliminates expired entries and returns whether there are still any left.
-
#can_disconnect? ⇒ Boolean
signals that the connection that contains this IO can be checked back into the pool.
- #close ⇒ void
- #closed? ⇒ Boolean
- #connect ⇒ void
-
#connect_attempt_time_left ⇒ Float?
time left until the attempt to connect to the current address should be abandoned.
- #connected? ⇒ Boolean
-
#initialize(origin, addresses, options) {|arg0| ... } ⇒ TCP
constructor
TODO: lift when https://github.com/ruby/rbs/issues/1497 fixed.
-
#inspect ⇒ ::String
:nocov:.
-
#next_address!(e) ⇒ void
abandons the connection attempt to the current address.
- #protocol ⇒ String
- #read(size, buffer) ⇒ 0, ...
- #socket ⇒ Object
- #to_io ⇒ IO
- #write(buffer) ⇒ Integer?
Methods included from Loggable
#log, #log_exception, log_identifiers
Constructor Details
#initialize(origin, addresses, options) {|arg0| ... } ⇒ TCP
TODO: lift when https://github.com/ruby/rbs/issues/1497 fixed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'sig/io/tcp.rbs', line 31 def initialize(origin, addresses, ) @state = :idle @keep_open = false @addresses = [] @ip_index = -1 @ip = @attempt_started_at = nil @hostname = origin.host @options = @fallback_protocol = @options.fallback_protocol @port = origin.port @interests = :w if (io = @options.io) io = case io when Hash io[origin.] else io end raise Error, "Given IO objects do not match the request authority" unless io # @type var io: TCPSocket | OpenSSL::SSL::SSLSocket _, _, _, ip = io.addr @io = io @addresses << (@ip = Resolver::Entry.new(ip)) @keep_open = true @state = :connected else add_addresses(addresses) end @ip_index = @addresses.size - 1 end |
Instance Attribute Details
#addresses ⇒ Array[Resolver::Entry] (readonly)
Returns the value of attribute addresses.
11 12 13 |
# File 'lib/httpx/io/tcp.rb', line 11 def addresses @addresses end |
#interests ⇒ io_interests (readonly)
Returns the value of attribute interests.
11 12 13 |
# File 'lib/httpx/io/tcp.rb', line 11 def interests @interests end |
#ip ⇒ Resolver::Entry? (readonly) Also known as: host
Returns the value of attribute ip.
11 12 13 |
# File 'lib/httpx/io/tcp.rb', line 11 def ip @ip end |
#port ⇒ Integer (readonly)
Returns the value of attribute port.
11 12 13 |
# File 'lib/httpx/io/tcp.rb', line 11 def port @port end |
#state ⇒ Symbol (readonly)
Returns the value of attribute state.
11 12 13 |
# File 'lib/httpx/io/tcp.rb', line 11 def state @state end |
Instance Method Details
#add_addresses(addrs) ⇒ void
This method returns an undefined value.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/httpx/io/tcp.rb', line 53 def add_addresses(addrs) return if addrs.empty? ip_index = @ip_index || (@addresses.size - 1) if addrs.first.ipv6? # should be the next in line @addresses = [*@addresses[0, ip_index], *addrs, *@addresses[ip_index..-1]] else @addresses.unshift(*addrs) end @ip_index += addrs.size end |
#addresses? ⇒ Boolean
eliminates expired entries and returns whether there are still any left.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/httpx/io/tcp.rb', line 67 def addresses? prev_addr_size = @addresses.size @addresses.delete_if(&:expired?).sort! do |addr1, addr2| if addr1.ipv6? addr2.ipv6? ? 0 : 1 else addr2.ipv6? ? -1 : 0 end end @ip_index = @addresses.size - 1 if prev_addr_size != @addresses.size @addresses.any? end |
#can_disconnect? ⇒ Boolean
signals that the connection that contains this IO can be checked back into the pool. that includes sockets opened outside of the scope of the session, or closed IOs.
229 230 231 |
# File 'lib/httpx/io/tcp.rb', line 229 def can_disconnect? @keep_open || @state == :closed end |
#close ⇒ void
This method returns an undefined value.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/httpx/io/tcp.rb', line 203 def close return if @keep_open # mark tcp as closed, so that it can be disconnected. # this bypasses the state machine API as not not allow the transition # from idle to closed in normal circumstances. @state = :closed if @state == :idle return if closed? begin @io.close rescue IOError => e log { "error closing socket" } log { e.(highlight: false) } ensure # @fiber-switch-guard # ensure that all :closed IOs don't leave dangling sockets # behind. This may happen in a fiber scheduler scenario where # connection is reused across fibers. transition(:closed) if @io.closed? end end |
#closed? ⇒ Boolean
237 238 239 |
# File 'lib/httpx/io/tcp.rb', line 237 def closed? @state == :idle || @state == :closed end |
#connect ⇒ void
This method returns an undefined value.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 |
# File 'lib/httpx/io/tcp.rb', line 112 def connect return unless closed? if @addresses.empty? # an idle connection trying to connect with no available addresses is a connection # out of the initial context which is back to the DNS resolution loop. This may # happen in a fiber-aware context where a connection reconnects with expired addresses, # and context is passed back to a fiber on the same connection while waiting for the # DNS answer. log { "tried connecting while resolving, skipping..." } return end if !@io || @io.closed? transition(:idle) @io = build_socket end try_connect rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e next_address!(e) @options.resolver_cache.evict(@hostname, @ip) @io = build_socket retry rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL, SocketError, IOError => e @ip_index -= 1 raise e if @ip_index.negative? log { "failed connecting to #{@ip} (#{e.}), trying next..." } @io = build_socket retry rescue Errno::ETIMEDOUT => e @ip_index -= 1 raise ConnectTimeoutError.new(@options.timeout.connect_timeout, e.) if @ip_index.negative? log { "failed connecting to #{@ip} (#{e.}), trying next..." } @io = build_socket retry end |
#connect_attempt_time_left ⇒ Float?
time left until the attempt to connect to the current address should be abandoned.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/httpx/io/tcp.rb', line 84 def connect_attempt_time_left return unless @state == :idle && @ip_index.positive? && (attempt_started_at = @attempt_started_at) timeouts = @options.timeout timeout = timeouts.connect_timeout || timeouts.request_timeout || timeouts.total_request_timeout return unless timeout && timeout.finite? timeout.fdiv(@addresses.size) - Utils.elapsed_time(attempt_started_at) end |
#connected? ⇒ Boolean
233 234 235 |
# File 'lib/httpx/io/tcp.rb', line 233 def connected? @state == :connected end |
#inspect ⇒ ::String
:nocov:
242 243 244 245 246 247 248 249 |
# File 'lib/httpx/io/tcp.rb', line 242 def inspect "#<#{self.class}:#{object_id} " \ "#{@ip}:#{@port} " \ "@state=#{@state} " \ "@hostname=#{@hostname} " \ "@addresses=#{@addresses} " \ "@state=#{@state}>" end |
#next_address!(e) ⇒ void
This method returns an undefined value.
abandons the connection attempt to the current address.
96 97 98 99 100 101 102 |
# File 'lib/httpx/io/tcp.rb', line 96 def next_address!(e) @io&.close @ip_index -= 1 raise e if @ip_index.negative? log { "failed connecting to #{@ip} (#{e.}), trying next..." } end |
#protocol ⇒ String
108 109 110 |
# File 'lib/httpx/io/tcp.rb', line 108 def protocol @fallback_protocol end |
#read(size, buffer) ⇒ 0, ...
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/httpx/io/tcp.rb', line 177 def read(size, buffer) siz = @io.read_nonblock(size, buffer, exception: false) case siz when String log { "READ: #{buffer.bytesize} bytes..." } buffer.bytesize when :wait_readable, :wait_writable buffer.clear 0 end end |
#socket ⇒ Object
49 50 51 |
# File 'lib/httpx/io/tcp.rb', line 49 def socket @io end |
#to_io ⇒ IO
104 105 106 |
# File 'lib/httpx/io/tcp.rb', line 104 def to_io @io.to_io end |
#write(buffer) ⇒ Integer?
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/httpx/io/tcp.rb', line 190 def write(buffer) siz = @io.write_nonblock(buffer, exception: false) case siz when Integer log { "WRITE: #{siz} bytes..." } buffer.shift!(siz) siz when :wait_readable, :wait_writable 0 end end |