class HTTPX::Connection::HTTP2

  1. lib/httpx/connection/http2.rb
  2. lib/httpx/plugins/persistent.rb
  3. show all
Superclass: Object

Included modules

  1. Callbacks
  2. Loggable

Constants

MAX_CONCURRENT_REQUESTS = ::HTTP2::DEFAULT_MAX_CONCURRENT_STREAMS  

Public Instance Aliases

reset -> init_connection

Attributes

pending [R]
streams [R]

Public Class methods

new(buffer, pending, options)
[show source]
   # File lib/httpx/connection/http2.rb
53 def initialize(buffer, pending, options)
54   @callbacks = @last_error = nil
55   @options = options
56   @settings = @options.http2_settings
57   @pending = pending
58   @streams = {}
59   @drains = {}
60   @pings = []
61   @streams_to_close_after_receive = []
62   @buffer = buffer
63   @handshake_completed = false
64   @wait_for_handshake = @settings.key?(:wait_for_handshake) ? @settings.delete(:wait_for_handshake) : true
65   @max_concurrent_requests = @options.max_concurrent_requests || MAX_CONCURRENT_REQUESTS
66   @max_requests = @options.max_requests
67   init_connection
68 end

Public Instance methods

<<(data)
[show source]
    # File lib/httpx/connection/http2.rb
181 def <<(data)
182   @connection << data
183 
184   while (stream, request, error = @streams_to_close_after_receive.shift)
185     # these streams were marked for cancellation due to errors found while processing the
186     # data received by the peer.
187     emit_stream_error(stream, request, error)
188   end
189 
190   # if the connection transitioned to :closed, start tearing it down. Explicitly bypass the
191   # :closing transition to give it time to complete the inflight streams.
192   return unless @connection.state == :closed
193 
194   close
195 end
close()
[show source]
    # File lib/httpx/connection/http2.rb
135 def close
136   if @connection.closed?
137     # if there are inflight streams waiting to be completed AFTER the peer alread shut down the
138     # connection from their end, raise-or-retry depending on whether the error is recoverable.
139     if (ex = @last_error)
140       if @streams.any? || @pending.any?
141         if ex.is_a?(GoawayError) && !ex.unrecoverable?
142           inflight_unprocessed_requests = @streams.map do |req, _|
143             req.transition(:idle)
144             req
145           end
146           @streams.clear
147 
148           # only signal exhaustion when inflight requests had to be moved back.
149           unless inflight_unprocessed_requests.empty?
150             @pending.unshift(*inflight_unprocessed_requests)
151             emit(:exhausted, ex)
152           end
153         else
154           while (req, _ = @streams.shift)
155             emit(:error, req, ex)
156           end
157           while (req = @pending.shift)
158             emit(:error, req, ex)
159           end
160         end
161       end
162 
163       # clean pending payload now that everything's gone.
164       @buffer.clear
165     end
166   else
167     @connection.goaway
168     emit(:timeout, @options.timeout.close_handshake_timeout)
169   end
170   emit(:close)
171 end
consume()
[show source]
    # File lib/httpx/connection/http2.rb
219 def consume
220   @streams.each do |request, stream|
221     next unless request.can_buffer?
222 
223     handle(request, stream)
224   end
225 
226   send_pending
227 end
empty?()
[show source]
    # File lib/httpx/connection/http2.rb
173 def empty?
174   @connection.closed? || @streams.empty?
175 end
exhausted?()
[show source]
    # File lib/httpx/connection/http2.rb
177 def exhausted?
178   !@max_requests.positive?
179 end
handle_error(ex, request = nil)
[show source]
    # File lib/httpx/connection/http2.rb
229 def handle_error(ex, request = nil)
230   last_stream_id = Float::INFINITY
231 
232   case ex
233   when OperationTimeoutError
234     if !@handshake_completed && @connection.state != :closed
235       # timed out waiting for HTTP/2 connection handshake to complete.
236       @connection.goaway(:settings_timeout, "closing due to settings timeout")
237       emit(:close_handshake)
238       settings_ex = SettingsTimeoutError.new(ex.timeout, ex.message)
239       settings_ex.set_backtrace(ex.backtrace)
240       ex = settings_ex
241     elsif @connection.state == :closing
242       # failed to receive data for inflight streams in time.
243       ex = @last_error || ex
244     end
245   when EOFError
246     if @connection.state == :closing
247       # socket closed while waiting for inflight streams to finish
248       ex = @last_error || ex
249     end
250   when GoawayError
251     last_stream_id = ex.last_stream_id
252   end
253 
254   inflight_unprocessed_requests = [] #: Array[Request]
255   unprocessed_streams = [] #: Array[::HTTP2::Stream]
256 
257   @streams.delete_if do |req, stream|
258     # allow inflight streams to finish
259     next(false) if !last_stream_id.infinite? && stream.id <= last_stream_id
260 
261     @drains.delete(stream)
262 
263     next(true) if request && request == req
264 
265     if stream.id > last_stream_id
266       req.transition(:idle)
267       # unprocessed request
268       inflight_unprocessed_requests << req
269       unprocessed_streams << stream
270 
271       next(true)
272     end
273 
274     emit(:error, req, ex)
275     true
276   end
277 
278   @pending.unshift(*inflight_unprocessed_requests) if inflight_unprocessed_requests.any?
279 
280   unless @streams.empty?
281     unprocessed_streams.each(&:close)
282 
283     # streams covered by the GOAWAY are still being processed.
284     return
285   end
286 
287   if ex.is_a?(GoawayError) && !ex.unrecoverable?
288     # resend unprocessed requests on a different connection
289     emit(:exhausted, ex) if @pending.any?
290     return
291   end
292 
293   while (req = @pending.shift)
294     next if request && request == req
295 
296     emit(:error, req, ex)
297   end
298 end
interests()
[show source]
    # File lib/httpx/connection/http2.rb
 76 def interests
 77   if @connection.state == :closed
 78     return unless @handshake_completed
 79 
 80     if @buffer.empty?
 81       # a closed HTTP/2 connection can still receive DATA frames from inflight streams.
 82       return :r unless @streams.empty?
 83 
 84       return
 85     end
 86 
 87     # HTTP/2 GOAWAY frame buffered.
 88     return :w
 89   end
 90 
 91   unless @connection.state == :connected && @handshake_completed
 92     # HTTP/2 in intermediate state or still completing initialization-
 93     return @buffer.empty? ? :r : :w
 94   end
 95 
 96   unless @connection.send_buffer.empty?
 97     # HTTP/2 connection is buffering data chunks and failing to emit DATA frames,
 98     # most likely because the flow control window is exhausted.
 99     return :rw unless @buffer.empty?
100 
101     # waiting for WINDOW_UPDATE frames
102     return :r
103   end
104 
105   # there are pending bufferable requests
106   if !@pending.empty? && can_buffer_pending_requests?
107     # only wait for writable if not waiting on the peer.
108     return waiting_for_peer? ? :rw : :w
109   end
110 
111   unless @drains.empty?
112     return :w unless @buffer.empty?
113 
114     streams_with_no_window = @drains.none? do |stream, _|
115       # if an existing stream has buffered bytes, it must wait for
116       # WINDOW_UPDATE frames
117       stream.send_buffer.empty?
118     end
119 
120     return streams_with_no_window ? :r : :rw
121 
122   end
123 
124   if @buffer.empty?
125     # skip if no more requests or pings to process
126     return unless waiting_for_peer?
127 
128     :r
129   else
130     # buffered frames
131     waiting_for_peer? ? :rw : :w
132   end
133 end
no_more_requests?()
[show source]
    # File lib/httpx/connection/http2.rb
311 def no_more_requests?
312   @streams.empty?
313 end
ping()
[show source]
    # File lib/httpx/connection/http2.rb
300 def ping
301   ping = SecureRandom.gen_random(8)
302   @connection.ping(ping.dup)
303 ensure
304   @pings << ping
305 end
reset_requests()
[show source]
    # File lib/httpx/connection/http2.rb
315 def reset_requests; end
send(request, head = false)
[show source]
    # File lib/httpx/connection/http2.rb
197 def send(request, head = false)
198   unless can_buffer_pending_requests?
199     head ? @pending.unshift(request) : @pending << request
200     return false
201   end
202   unless (stream = @streams[request])
203     stream = @connection.new_stream(**request.http2_stream_options)
204     handle_stream(stream, request)
205     @streams[request] = stream
206     @max_requests -= 1
207     emit(:request_accepted, request)
208   end
209   handle(request, stream)
210   true
211 rescue ::HTTP2::Error::StreamLimitExceeded
212   @pending.unshift(request)
213   false
214 rescue ::HTTP2::Error::Error, ArgumentError => e
215   emit(:error, request, e)
216   false
217 end
timeout()
[show source]
   # File lib/httpx/connection/http2.rb
70 def timeout
71   return @options.timeout.operation_timeout if @handshake_completed
72 
73   @options.timeout.settings_timeout
74 end
waiting_for_ping?()
[show source]
    # File lib/httpx/connection/http2.rb
307 def waiting_for_ping?
308   @pings.any?
309 end