Class: HTTPX::Connection::HTTP2
- Inherits:
-
Object
- Object
- HTTPX::Connection::HTTP2
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 =
::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
#callbacks, #callbacks_for?, #emit, #on, #once
Instance Attribute Details
#pending ⇒ Array[Request]
Returns the value of attribute pending.
51
52
53
|
# File 'lib/httpx/connection/http2.rb', line 51
def pending
@pending
end
|
#streams ⇒ Hash[Request, HTTP2::Stream]
Returns the value of attribute streams.
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.
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)
emit_stream_error(stream, request, error)
end
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 (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
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
@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
173
174
175
|
# File 'lib/httpx/connection/http2.rb', line 173
def empty?
@connection.closed? || @streams.empty?
end
|
#exhausted? ⇒ 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.
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
@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
ex = @last_error || ex
end
when EOFError
if @connection.state == :closing
ex = @last_error || ex
end
when GoawayError
last_stream_id = ex.last_stream_id
end
inflight_unprocessed_requests = [] unprocessed_streams = []
@streams.delete_if do |req, stream|
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)
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)
return
end
if ex.is_a?(GoawayError) && !ex.unrecoverable?
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?
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?
return :r unless @streams.empty?
return
end
return :w
end
unless @connection.state == :connected && @handshake_completed
return @buffer.empty? ? :r : :w
end
unless @connection.send_buffer.empty?
return :rw unless @buffer.empty?
return :r
end
if !@pending.empty? && can_buffer_pending_requests?
return waiting_for_peer? ? :rw : :w
end
unless @drains.empty?
return :w unless @buffer.empty?
streams_with_no_window = @drains.none? do |stream, _|
stream.send_buffer.empty?
end
return streams_with_no_window ? :r : :rw
end
if @buffer.empty?
return unless waiting_for_peer?
:r
else
waiting_for_peer? ? :rw : :w
end
end
|
#no_more_requests? ⇒ 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
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?
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
307
308
309
|
# File 'lib/httpx/connection/http2.rb', line 307
def waiting_for_ping?
@pings.any?
end
|