Class: HTTPX::Connection::HTTP2

Inherits:
Object
  • Object
show all
Includes:
HTTPX::Callbacks, Loggable
Defined in:
lib/httpx/connection/http2.rb,
sig/connection/http2.rbs

Defined Under Namespace

Classes: Error, GoawayError, PingError, RefusedStreamError, RstStreamError

Constant Summary collapse

MAX_CONCURRENT_REQUESTS =

Returns:

  • (Integer)
::HTTP2::DEFAULT_MAX_CONCURRENT_STREAMS

Constants included from Loggable

Loggable::COLORS, Loggable::USE_DEBUG_LOG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log, #log_exception, log_identifiers

Methods included from HTTPX::Callbacks

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

Instance Attribute Details

#pending ⇒ Array[Request] (readonly)

Returns the value of attribute pending.

Returns:



51
52
53
# File 'lib/httpx/connection/http2.rb', line 51

def pending
  @pending
end

#streams ⇒ Hash[Request, HTTP2::Stream] (readonly)

Returns the value of attribute streams.

Returns:



51
52
53
# File 'lib/httpx/connection/http2.rb', line 51

def streams
  @streams
end

Instance Method Details

#<<(data) ⇒ void

This method returns an undefined value.

Parameters:

  • (string)


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/httpx/connection/http2.rb', line 181

def <<(data)
  @connection << data

  while (stream, request, error = @streams_to_close_after_receive.shift)
    # these streams were marked for cancellation due to errors found while processing the
    # data received by the peer.
    emit_stream_error(stream, request, error)
  end

  # if the connection transitioned to :closed, start tearing it down. Explicitly bypass the
  # :closing transition to give it time to complete the inflight streams.
  return unless @connection.state == :closed

  close
end

#close ⇒ void

This method returns an undefined value.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/httpx/connection/http2.rb', line 135

def close
  if @connection.closed?
    # if there are inflight streams waiting to be completed AFTER the peer alread shut down the
    # connection from their end, raise-or-retry depending on whether the error is recoverable.
    if (ex = @last_error)
      if @streams.any? || @pending.any?
        if ex.is_a?(GoawayError) && !ex.unrecoverable?
          inflight_unprocessed_requests = @streams.map do |req, _|
            req.transition(:idle)
            req
          end
          @streams.clear

          # only signal exhaustion when inflight requests had to be moved back.
          unless inflight_unprocessed_requests.empty?
            @pending.unshift(*inflight_unprocessed_requests)
            emit(:exhausted, ex)
          end
        else
          while (req, _ = @streams.shift)
            emit(:error, req, ex)
          end
          while (req = @pending.shift)
            emit(:error, req, ex)
          end
        end
      end

      # clean pending payload now that everything's gone.
      @buffer.clear
    end
  else
    @connection.goaway
    emit(:timeout, @options.timeout.close_handshake_timeout)
  end
  emit(:close)
end

#consume ⇒ void

This method returns an undefined value.



219
220
221
222
223
224
225
226
227
# File 'lib/httpx/connection/http2.rb', line 219

def consume
  @streams.each do |request, stream|
    next unless request.can_buffer?

    handle(request, stream)
  end

  send_pending
end

#empty? ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/httpx/connection/http2.rb', line 173

def empty?
  @connection.closed? || @streams.empty?
end

#exhausted? ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/httpx/connection/http2.rb', line 177

def exhausted?
  !@max_requests.positive?
end

#handle_error(ex, request = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • ex (StandardError)
  • request (Request, nil) (defaults to: nil)


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/httpx/connection/http2.rb', line 229

def handle_error(ex, request = nil)
  last_stream_id = Float::INFINITY

  case ex
  when OperationTimeoutError
    if !@handshake_completed && @connection.state != :closed
      # timed out waiting for HTTP/2 connection handshake to complete.
      @connection.goaway(:settings_timeout, "closing due to settings timeout")
      emit(:close_handshake)
      settings_ex = SettingsTimeoutError.new(ex.timeout, ex.message)
      settings_ex.set_backtrace(ex.backtrace)
      ex = settings_ex
    elsif @connection.state == :closing
      # failed to receive data for inflight streams in time.
      ex = @last_error || ex
    end
  when EOFError
    if @connection.state == :closing
      # socket closed while waiting for inflight streams to finish
      ex = @last_error || ex
    end
  when GoawayError
    last_stream_id = ex.last_stream_id
  end

  inflight_unprocessed_requests = [] #: Array[Request]
  unprocessed_streams = [] #: Array[::HTTP2::Stream]

  @streams.delete_if do |req, stream|
    # allow inflight streams to finish
    next(false) if !last_stream_id.infinite? && stream.id <= last_stream_id

    @drains.delete(stream)

    next(true) if request && request == req

    if stream.id > last_stream_id
      req.transition(:idle)
      # unprocessed request
      inflight_unprocessed_requests << req
      unprocessed_streams << stream

      next(true)
    end

    emit(:error, req, ex)
    true
  end

  @pending.unshift(*inflight_unprocessed_requests) if inflight_unprocessed_requests.any?

  unless @streams.empty?
    unprocessed_streams.each(&:close)

    # streams covered by the GOAWAY are still being processed.
    return
  end

  if ex.is_a?(GoawayError) && !ex.unrecoverable?
    # resend unprocessed requests on a different connection
    emit(:exhausted, ex) if @pending.any?
    return
  end

  while (req = @pending.shift)
    next if request && request == req

    emit(:error, req, ex)
  end
end

#interests ⇒ io_interests?

Returns:

  • (io_interests, nil)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/httpx/connection/http2.rb', line 76

def interests
  if @connection.state == :closed
    return unless @handshake_completed

    if @buffer.empty?
      # a closed HTTP/2 connection can still receive DATA frames from inflight streams.
      return :r unless @streams.empty?

      return
    end

    # HTTP/2 GOAWAY frame buffered.
    return :w
  end

  unless @connection.state == :connected && @handshake_completed
    # HTTP/2 in intermediate state or still completing initialization-
    return @buffer.empty? ? :r : :w
  end

  unless @connection.send_buffer.empty?
    # HTTP/2 connection is buffering data chunks and failing to emit DATA frames,
    # most likely because the flow control window is exhausted.
    return :rw unless @buffer.empty?

    # waiting for WINDOW_UPDATE frames
    return :r
  end

  # there are pending bufferable requests
  if !@pending.empty? && can_buffer_pending_requests?
    # only wait for writable if not waiting on the peer.
    return waiting_for_peer? ? :rw : :w
  end

  unless @drains.empty?
    return :w unless @buffer.empty?

    streams_with_no_window = @drains.none? do |stream, _|
      # if an existing stream has buffered bytes, it must wait for
      # WINDOW_UPDATE frames
      stream.send_buffer.empty?
    end

    return streams_with_no_window ? :r : :rw

  end

  if @buffer.empty?
    # skip if no more requests or pings to process
    return unless waiting_for_peer?

    :r
  else
    # buffered frames
    waiting_for_peer? ? :rw : :w
  end
end

#no_more_requests? ⇒ Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/httpx/connection/http2.rb', line 311

def no_more_requests?
  @streams.empty?
end

#ping ⇒ void

This method returns an undefined value.



300
301
302
303
304
305
# File 'lib/httpx/connection/http2.rb', line 300

def ping
  ping = SecureRandom.gen_random(8)
  @connection.ping(ping.dup)
ensure
  @pings << ping
end

#reset_requests ⇒ void

This method returns an undefined value.



315
# File 'lib/httpx/connection/http2.rb', line 315

def reset_requests; end

#send(request, head = false) ⇒ Boolean

Parameters:

  • request (Request)
  • head (Boolean) (defaults to: false)

Returns:

  • (Boolean)


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/httpx/connection/http2.rb', line 197

def send(request, head = false)
  unless can_buffer_pending_requests?
    head ? @pending.unshift(request) : @pending << request
    return false
  end
  unless (stream = @streams[request])
    stream = @connection.new_stream(**request.http2_stream_options)
    handle_stream(stream, request)
    @streams[request] = stream
    @max_requests -= 1
    emit(:request_accepted, request)
  end
  handle(request, stream)
  true
rescue ::HTTP2::Error::StreamLimitExceeded
  @pending.unshift(request)
  false
rescue ::HTTP2::Error::Error, ArgumentError => e
  emit(:error, request, e)
  false
end

#timeout ⇒ interval?

Returns:

  • (interval, nil)


70
71
72
73
74
# File 'lib/httpx/connection/http2.rb', line 70

def timeout
  return @options.timeout.operation_timeout if @handshake_completed

  @options.timeout.settings_timeout
end

#waiting_for_ping? ⇒ Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/httpx/connection/http2.rb', line 307

def waiting_for_ping?
  @pings.any?
end