Class: HTTPX::Connection::HTTP2

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

Defined Under Namespace

Classes: Error, GoawayError, PingError

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_redact, #log_redact_body, #log_redact_headers

Methods included from HTTPX::Callbacks

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

Constructor Details

#initialize(buffer, options) ⇒ HTTP2

Returns a new instance of HTTP2.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/httpx/connection/http2.rb', line 33

def initialize(buffer, options)
  @options = options
  @settings = @options.http2_settings
  @pending = []
  @streams = {}
  @drains = {}
  @pings = []
  @buffer = buffer
  @handshake_completed = false
  @wait_for_handshake = @settings.key?(:wait_for_handshake) ? @settings.delete(:wait_for_handshake) : true
  @max_concurrent_requests = @options.max_concurrent_requests || MAX_CONCURRENT_REQUESTS
  @max_requests = @options.max_requests
  init_connection
end

Instance Attribute Details

#pendingObject (readonly)

Returns the value of attribute pending.



31
32
33
# File 'lib/httpx/connection/http2.rb', line 31

def pending
  @pending
end

#streamsObject (readonly)

Returns the value of attribute streams.



31
32
33
# File 'lib/httpx/connection/http2.rb', line 31

def streams
  @streams
end

Instance Method Details

#<<(data) ⇒ Object



111
112
113
# File 'lib/httpx/connection/http2.rb', line 111

def <<(data)
  @connection << data
end

#closeObject



95
96
97
98
99
100
101
# File 'lib/httpx/connection/http2.rb', line 95

def close
  unless @connection.state == :closed
    @connection.goaway
    emit(:timeout, @options.timeout[:close_handshake_timeout])
  end
  emit(:close)
end

#consumeObject



133
134
135
136
137
138
139
# File 'lib/httpx/connection/http2.rb', line 133

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

    handle(request, stream)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/httpx/connection/http2.rb', line 103

def empty?
  @connection.state == :closed || @streams.empty?
end

#exhausted?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/httpx/connection/http2.rb', line 107

def exhausted?
  !@max_requests.positive?
end

#handle_error(ex, request = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/httpx/connection/http2.rb', line 141

def handle_error(ex, request = nil)
  if ex.is_a?(OperationTimeoutError) && !@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
  end
  while (req, _ = @streams.shift)
    next if request && request == req

    emit(:error, req, ex)
  end
  while (req = @pending.shift)
    next if request && request == req

    emit(:error, req, ex)
  end
end

#interestsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/httpx/connection/http2.rb', line 54

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

    return if @buffer.empty?

    # 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 : :rw
  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
  return :w if !@pending.empty? && can_buffer_more_requests?

  # there are pending frames from the last run
  return :w unless @drains.empty?

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

    :r
  else
    # buffered frames
    :w
  end
end

#pingObject



161
162
163
164
165
166
# File 'lib/httpx/connection/http2.rb', line 161

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

#reset_requestsObject



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

def reset_requests; end

#send(request, head = false) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/httpx/connection/http2.rb', line 115

def send(request, head = false)
  unless can_buffer_more_requests?
    head ? @pending.unshift(request) : @pending << request
    return false
  end
  unless (stream = @streams[request])
    stream = @connection.new_stream
    handle_stream(stream, request)
    @streams[request] = stream
    @max_requests -= 1
  end
  handle(request, stream)
  true
rescue ::HTTP2::Error::StreamLimitExceeded
  @pending.unshift(request)
  false
end

#timeoutObject



48
49
50
51
52
# File 'lib/httpx/connection/http2.rb', line 48

def timeout
  return @options.timeout[:operation_timeout] if @handshake_completed

  @options.timeout[:settings_timeout]
end

#waiting_for_ping?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/httpx/connection/http2.rb', line 168

def waiting_for_ping?
  @pings.any?
end