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
103 def <<(data)
104   @connection << data
105 end
close()
[show source]
   # File lib/httpx/connection/http2.rb
87 def close
88   unless @connection.state == :closed
89     @connection.goaway
90     emit(:timeout, @options.timeout[:close_handshake_timeout])
91   end
92   emit(:close, true)
93 end
consume()
[show source]
    # File lib/httpx/connection/http2.rb
125 def consume
126   @streams.each do |request, stream|
127     next unless request.can_buffer?
128 
129     handle(request, stream)
130   end
131 end
empty?()
[show source]
   # File lib/httpx/connection/http2.rb
95 def empty?
96   @connection.state == :closed || @streams.empty?
97 end
exhausted?()
[show source]
    # File lib/httpx/connection/http2.rb
 99 def exhausted?
100   !@max_requests.positive?
101 end
handle_error(ex, request = nil)
[show source]
    # File lib/httpx/connection/http2.rb
133 def handle_error(ex, request = nil)
134   if ex.is_a?(OperationTimeoutError) && !@handshake_completed && @connection.state != :closed
135     @connection.goaway(:settings_timeout, "closing due to settings timeout")
136     emit(:close_handshake)
137     settings_ex = SettingsTimeoutError.new(ex.timeout, ex.message)
138     settings_ex.set_backtrace(ex.backtrace)
139     ex = settings_ex
140   end
141   @streams.each_key do |req|
142     next if request && request == req
143 
144     emit(:error, req, ex)
145   end
146   while (req = @pending.shift)
147     next if request && request == req
148 
149     emit(:error, req, ex)
150   end
151 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     return :w
61   end
62 
63   unless @connection.state == :connected && @handshake_completed
64     return @buffer.empty? ? :r : :rw
65   end
66 
67   unless @connection.send_buffer.empty?
68     return :rw unless @buffer.empty?
69 
70     # waiting for WINDOW_UPDATE frames
71     return :r
72   end
73 
74   return :w if !@pending.empty? && can_buffer_more_requests?
75 
76   return :w unless @drains.empty?
77 
78   if @buffer.empty?
79     return if @streams.empty? && @pings.empty?
80 
81     :r
82   else
83     :w
84   end
85 end
ping()
[show source]
    # File lib/httpx/connection/http2.rb
153 def ping
154   ping = SecureRandom.gen_random(8)
155   @connection.ping(ping.dup)
156 ensure
157   @pings << ping
158 end
send(request, head = false)
[show source]
    # File lib/httpx/connection/http2.rb
107 def send(request, head = false)
108   unless can_buffer_more_requests?
109     head ? @pending.unshift(request) : @pending << request
110     return false
111   end
112   unless (stream = @streams[request])
113     stream = @connection.new_stream
114     handle_stream(stream, request)
115     @streams[request] = stream
116     @max_requests -= 1
117   end
118   handle(request, stream)
119   true
120 rescue ::HTTP2::Error::StreamLimitExceeded
121   @pending.unshift(request)
122   false
123 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
160 def waiting_for_ping?
161   @pings.any?
162 end