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
27 def initialize(buffer, options)
28   @options = options
29   @settings = @options.http2_settings
30   @pending = []
31   @streams = {}
32   @drains  = {}
33   @pings = []
34   @buffer = buffer
35   @handshake_completed = false
36   @wait_for_handshake = @settings.key?(:wait_for_handshake) ? @settings.delete(:wait_for_handshake) : true
37   @max_concurrent_requests = @options.max_concurrent_requests || MAX_CONCURRENT_REQUESTS
38   @max_requests = @options.max_requests
39   init_connection
40 end

Public Instance methods

<<(data)
[show source]
   # File lib/httpx/connection/http2.rb
91 def <<(data)
92   @connection << data
93 end
can_buffer_more_requests?()
[show source]
   # File lib/httpx/connection/http2.rb
95 def can_buffer_more_requests?
96   (@handshake_completed || !@wait_for_handshake) &&
97     @streams.size < @max_concurrent_requests &&
98     @streams.size < @max_requests
99 end
close()
[show source]
   # File lib/httpx/connection/http2.rb
75 def close
76   unless @connection.state == :closed
77     @connection.goaway
78     emit(:timeout, @options.timeout[:close_handshake_timeout])
79   end
80   emit(:close, true)
81 end
consume()
[show source]
    # File lib/httpx/connection/http2.rb
118 def consume
119   @streams.each do |request, stream|
120     next if request.state == :done
121 
122     handle(request, stream)
123   end
124 end
empty?()
[show source]
   # File lib/httpx/connection/http2.rb
83 def empty?
84   @connection.state == :closed || @streams.empty?
85 end
exhausted?()
[show source]
   # File lib/httpx/connection/http2.rb
87 def exhausted?
88   !@max_requests.positive?
89 end
handle_error(ex, request = nil)
[show source]
    # File lib/httpx/connection/http2.rb
126 def handle_error(ex, request = nil)
127   if ex.instance_of?(TimeoutError) && !@handshake_completed && @connection.state != :closed
128     @connection.goaway(:settings_timeout, "closing due to settings timeout")
129     emit(:close_handshake)
130     settings_ex = SettingsTimeoutError.new(ex.timeout, ex.message)
131     settings_ex.set_backtrace(ex.backtrace)
132     ex = settings_ex
133   end
134   @streams.each_key do |req|
135     next if request && request == req
136 
137     emit(:error, req, ex)
138   end
139   @pending.each do |req|
140     next if request && request == req
141 
142     emit(:error, req, ex)
143   end
144 end
interests()
[show source]
   # File lib/httpx/connection/http2.rb
48 def interests
49   # waiting for WINDOW_UPDATE frames
50   return :r if @buffer.full?
51 
52   if @connection.state == :closed
53     return unless @handshake_completed
54 
55     return :w
56   end
57 
58   unless @connection.state == :connected && @handshake_completed
59     return @buffer.empty? ? :r : :rw
60   end
61 
62   return :w if !@pending.empty? && can_buffer_more_requests?
63 
64   return :w unless @drains.empty?
65 
66   if @buffer.empty?
67     return if @streams.empty? && @pings.empty?
68 
69     return :r
70   end
71 
72   :rw
73 end
ping()
[show source]
    # File lib/httpx/connection/http2.rb
146 def ping
147   ping = SecureRandom.gen_random(8)
148   @connection.ping(ping)
149 ensure
150   @pings << ping
151 end
send(request)
[show source]
    # File lib/httpx/connection/http2.rb
101 def send(request)
102   unless can_buffer_more_requests?
103     @pending << request
104     return
105   end
106   unless (stream = @streams[request])
107     stream = @connection.new_stream
108     handle_stream(stream, request)
109     @streams[request] = stream
110     @max_requests -= 1
111   end
112   handle(request, stream)
113   true
114 rescue ::HTTP2::Error::StreamLimitExceeded
115   @pending.unshift(request)
116 end
timeout()
[show source]
   # File lib/httpx/connection/http2.rb
42 def timeout
43   return @options.timeout[:operation_timeout] if @handshake_completed
44 
45   @options.timeout[:settings_timeout]
46 end