class HTTPX::Connection::HTTP2

  1. lib/httpx/connection/http2.rb
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, options)
[show source]
   # File lib/httpx/connection/http2.rb
33 def initialize(buffer, options)
34   @options = options
35   @settings = @options.http2_settings
36   @pending = []
37   @streams = {}
38   @drains = {}
39   @pings = []
40   @buffer = buffer
41   @handshake_completed = false
42   @wait_for_handshake = @settings.key?(:wait_for_handshake) ? @settings.delete(:wait_for_handshake) : true
43   @max_concurrent_requests = @options.max_concurrent_requests || MAX_CONCURRENT_REQUESTS
44   @max_requests = @options.max_requests
45   init_connection
46 end

Public Instance methods

<<(data)
[show source]
    # File lib/httpx/connection/http2.rb
111 def <<(data)
112   @connection << data
113 end
close()
[show source]
    # File lib/httpx/connection/http2.rb
 95 def close
 96   unless @connection.state == :closed
 97     @connection.goaway
 98     emit(:timeout, @options.timeout[:close_handshake_timeout])
 99   end
100   emit(:close)
101 end
consume()
[show source]
    # File lib/httpx/connection/http2.rb
133 def consume
134   @streams.each do |request, stream|
135     next unless request.can_buffer?
136 
137     handle(request, stream)
138   end
139 end
empty?()
[show source]
    # File lib/httpx/connection/http2.rb
103 def empty?
104   @connection.state == :closed || @streams.empty?
105 end
exhausted?()
[show source]
    # File lib/httpx/connection/http2.rb
107 def exhausted?
108   !@max_requests.positive?
109 end
handle_error(ex, request = nil)
[show source]
    # File lib/httpx/connection/http2.rb
141 def handle_error(ex, request = nil)
142   if ex.is_a?(OperationTimeoutError) && !@handshake_completed && @connection.state != :closed
143     @connection.goaway(:settings_timeout, "closing due to settings timeout")
144     emit(:close_handshake)
145     settings_ex = SettingsTimeoutError.new(ex.timeout, ex.message)
146     settings_ex.set_backtrace(ex.backtrace)
147     ex = settings_ex
148   end
149   while (req, _ = @streams.shift)
150     next if request && request == req
151 
152     emit(:error, req, ex)
153   end
154   while (req = @pending.shift)
155     next if request && request == req
156 
157     emit(:error, req, ex)
158   end
159 end
interests()
[show source]
   # File lib/httpx/connection/http2.rb
54 def interests
55   if @connection.state == :closed
56     return unless @handshake_completed
57 
58     return if @buffer.empty?
59 
60     # HTTP/2 GOAWAY frame buffered.
61     return :w
62   end
63 
64   unless @connection.state == :connected && @handshake_completed
65     # HTTP/2 in intermediate state or still completing initialization-
66     return @buffer.empty? ? :r : :rw
67   end
68 
69   unless @connection.send_buffer.empty?
70     # HTTP/2 connection is buffering data chunks and failing to emit DATA frames,
71     # most likely because the flow control window is exhausted.
72     return :rw unless @buffer.empty?
73 
74     # waiting for WINDOW_UPDATE frames
75     return :r
76   end
77 
78   # there are pending bufferable requests
79   return :w if !@pending.empty? && can_buffer_more_requests?
80 
81   # there are pending frames from the last run
82   return :w unless @drains.empty?
83 
84   if @buffer.empty?
85     # skip if no more requests or pings to process
86     return if @streams.empty? && @pings.empty?
87 
88     :r
89   else
90     # buffered frames
91     :w
92   end
93 end
ping()
[show source]
    # File lib/httpx/connection/http2.rb
161 def ping
162   ping = SecureRandom.gen_random(8)
163   @connection.ping(ping.dup)
164 ensure
165   @pings << ping
166 end
reset_requests()
[show source]
    # File lib/httpx/connection/http2.rb
172 def reset_requests; end
send(request, head = false)
[show source]
    # File lib/httpx/connection/http2.rb
115 def send(request, head = false)
116   unless can_buffer_more_requests?
117     head ? @pending.unshift(request) : @pending << request
118     return false
119   end
120   unless (stream = @streams[request])
121     stream = @connection.new_stream
122     handle_stream(stream, request)
123     @streams[request] = stream
124     @max_requests -= 1
125   end
126   handle(request, stream)
127   true
128 rescue ::HTTP2::Error::StreamLimitExceeded
129   @pending.unshift(request)
130   false
131 end
timeout()
[show source]
   # File lib/httpx/connection/http2.rb
48 def timeout
49   return @options.timeout[:operation_timeout] if @handshake_completed
50 
51   @options.timeout[:settings_timeout]
52 end
waiting_for_ping?()
[show source]
    # File lib/httpx/connection/http2.rb
168 def waiting_for_ping?
169   @pings.any?
170 end