Methods
Public Class
Public Instance
Included modules
Public Class methods
new(origin, addresses, options)
[show source]
# File lib/httpx/io/tcp.rb 15 def initialize(origin, addresses, options) 16 @state = :idle 17 @keep_open = false 18 @addresses = [] 19 @ip_index = -1 20 @ip = @attempt_started_at = nil 21 @hostname = origin.host 22 @options = options 23 @fallback_protocol = @options.fallback_protocol 24 @port = origin.port 25 @interests = :w 26 if (io = @options.io) 27 io = 28 case io 29 when Hash 30 io[origin.authority] 31 else 32 io 33 end 34 raise Error, "Given IO objects do not match the request authority" unless io 35 36 # @type var io: TCPSocket | OpenSSL::SSL::SSLSocket 37 38 _, _, _, ip = io.addr 39 @io = io 40 @addresses << (@ip = Resolver::Entry.new(ip)) 41 @keep_open = true 42 @state = :connected 43 else 44 add_addresses(addresses) 45 end 46 @ip_index = @addresses.size - 1 47 end
Public Instance methods
add_addresses(addrs)
[show source]
# File lib/httpx/io/tcp.rb 53 def add_addresses(addrs) 54 return if addrs.empty? 55 56 ip_index = @ip_index || (@addresses.size - 1) 57 if addrs.first.ipv6? 58 # should be the next in line 59 @addresses = [*@addresses[0, ip_index], *addrs, *@addresses[ip_index..-1]] 60 else 61 @addresses.unshift(*addrs) 62 end 63 @ip_index += addrs.size 64 end
addresses?()
eliminates expired entries and returns whether there are still any left.
[show source]
# File lib/httpx/io/tcp.rb 67 def addresses? 68 prev_addr_size = @addresses.size 69 70 @addresses.delete_if(&:expired?).sort! do |addr1, addr2| 71 if addr1.ipv6? 72 addr2.ipv6? ? 0 : 1 73 else 74 addr2.ipv6? ? -1 : 0 75 end 76 end 77 78 @ip_index = @addresses.size - 1 if prev_addr_size != @addresses.size 79 80 @addresses.any? 81 end
can_disconnect?()
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.
[show source]
# File lib/httpx/io/tcp.rb 229 def can_disconnect? 230 @keep_open || @state == :closed 231 end
close()
[show source]
# File lib/httpx/io/tcp.rb 203 def close 204 return if @keep_open 205 206 # mark tcp as closed, so that it can be disconnected. 207 # this bypasses the state machine API as not not allow the transition 208 # from idle to closed in normal circumstances. 209 @state = :closed if @state == :idle 210 211 return if closed? 212 213 begin 214 @io.close 215 rescue IOError => e 216 log { "error closing socket" } 217 log { e.full_message(highlight: false) } 218 ensure 219 # @fiber-switch-guard 220 # ensure that all :closed IOs don't leave dangling sockets 221 # behind. This may happen in a fiber scheduler scenario where 222 # connection is reused across fibers. 223 transition(:closed) if @io.closed? 224 end 225 end
closed?()
[show source]
# File lib/httpx/io/tcp.rb 237 def closed? 238 @state == :idle || @state == :closed 239 end
connect()
[show source]
# File lib/httpx/io/tcp.rb 112 def connect 113 return unless closed? 114 115 if @addresses.empty? 116 # an idle connection trying to connect with no available addresses is a connection 117 # out of the initial context which is back to the DNS resolution loop. This may 118 # happen in a fiber-aware context where a connection reconnects with expired addresses, 119 # and context is passed back to a fiber on the same connection while waiting for the 120 # DNS answer. 121 log { "tried connecting while resolving, skipping..." } 122 123 return 124 end 125 126 if !@io || @io.closed? 127 transition(:idle) 128 @io = build_socket 129 end 130 try_connect 131 rescue Errno::EHOSTUNREACH, 132 Errno::ENETUNREACH => e 133 next_address!(e) 134 @options.resolver_cache.evict(@hostname, @ip) 135 @io = build_socket 136 retry 137 rescue Errno::ECONNREFUSED, 138 Errno::EADDRNOTAVAIL, 139 SocketError, 140 IOError => e 141 @ip_index -= 1 142 143 raise e if @ip_index.negative? 144 145 log { "failed connecting to #{@ip} (#{e.message}), trying next..." } 146 @io = build_socket 147 retry 148 rescue Errno::ETIMEDOUT => e 149 @ip_index -= 1 150 151 raise ConnectTimeoutError.new(@options.timeout.connect_timeout, e.message) if @ip_index.negative? 152 153 log { "failed connecting to #{@ip} (#{e.message}), trying next..." } 154 155 @io = build_socket 156 retry 157 end
connect_attempt_time_left()
time left until the attempt to connect to the current address should be abandoned.
[show source]
# File lib/httpx/io/tcp.rb 84 def connect_attempt_time_left 85 return unless @state == :idle && @ip_index.positive? && (attempt_started_at = @attempt_started_at) 86 87 timeouts = @options.timeout 88 timeout = timeouts.connect_timeout || timeouts.request_timeout || timeouts.total_request_timeout 89 90 return unless timeout && timeout.finite? 91 92 timeout.fdiv(@addresses.size) - Utils.elapsed_time(attempt_started_at) 93 end
connected?()
[show source]
# File lib/httpx/io/tcp.rb 233 def connected? 234 @state == :connected 235 end
inspect()
simplecov:disable
[show source]
# File lib/httpx/io/tcp.rb 242 def inspect 243 "#<#{self.class}:#{object_id} " \ 244 "#{@ip}:#{@port} " \ 245 "@state=#{@state} " \ 246 "@hostname=#{@hostname} " \ 247 "@addresses=#{@addresses} " \ 248 "@state=#{@state}>" 249 end
next_address!(e)
abandons the connection attempt to the current address.
[show source]
# File lib/httpx/io/tcp.rb 96 def next_address!(e) 97 @io&.close 98 @ip_index -= 1 99 raise e if @ip_index.negative? 100 101 log { "failed connecting to #{@ip} (#{e.message}), trying next..." } 102 end
read(size, buffer)
[show source]
# File lib/httpx/io/tcp.rb 177 def read(size, buffer) 178 siz = @io.read_nonblock(size, buffer, exception: false) 179 180 case siz 181 when String 182 log { "READ: #{buffer.bytesize} bytes..." } 183 buffer.bytesize 184 when :wait_readable, :wait_writable 185 buffer.clear 186 0 187 end 188 end
write(buffer)
[show source]
# File lib/httpx/io/tcp.rb 190 def write(buffer) 191 siz = @io.write_nonblock(buffer, exception: false) 192 193 case siz 194 when Integer 195 log { "WRITE: #{siz} bytes..." } 196 buffer.shift!(siz) 197 siz 198 when :wait_readable, :wait_writable 199 0 200 end 201 end