Class: HTTPX::Connection::HTTP2

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

Direct Known Subclasses

Plugins::H2C::H2CParser

Constant Summary collapse

Error =
Class.new(Error) do
  def initialize(id, code)
    super("stream #{id} closed with error: #{code}")
  end
end

Constants included from Loggable

Loggable::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log, #log_exception

Methods included from HTTPX::Callbacks

#emit, #on, #once

Constructor Details

#initialize(buffer, options) ⇒ HTTP2

Returns a new instance of HTTP2.



19
20
21
22
23
24
25
26
27
28
# File 'lib/httpx/connection/http2.rb', line 19

def initialize(buffer, options)
  @options = Options.new(options)
  @max_concurrent_requests = @options.max_concurrent_requests
  @pending = []
  @streams = {}
  @drains  = {}
  @buffer = buffer
  @handshake_completed = false
  init_connection
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object (private)



269
270
271
272
273
274
275
# File 'lib/httpx/connection/http2.rb', line 269

def method_missing(meth, *args, &blk)
  if @connection.respond_to?(meth)
    @connection.__send__(meth, *args, &blk)
  else
    super
  end
end

Instance Attribute Details

#pendingObject (readonly)

Returns the value of attribute pending.



17
18
19
# File 'lib/httpx/connection/http2.rb', line 17

def pending
  @pending
end

#streamsObject (readonly)

Returns the value of attribute streams.



17
18
19
# File 'lib/httpx/connection/http2.rb', line 17

def streams
  @streams
end

Instance Method Details

#<<(data) ⇒ Object



38
39
40
# File 'lib/httpx/connection/http2.rb', line 38

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

#closeObject



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

def close
  @connection.goaway
end

#consumeObject



66
67
68
69
70
# File 'lib/httpx/connection/http2.rb', line 66

def consume
  @streams.each do |request, stream|
    handle(request, stream)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/httpx/connection/http2.rb', line 34

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

#handle_error(ex) ⇒ Object



72
73
74
75
76
# File 'lib/httpx/connection/http2.rb', line 72

def handle_error(ex)
  @streams.each_key do |request|
    emit(:error, request, ex)
  end
end

#reenqueue!Object



57
58
59
60
61
62
63
64
# File 'lib/httpx/connection/http2.rb', line 57

def reenqueue!
  requests = @streams.keys
  @streams.clear
  init_connection
  requests.each do |request|
    send(request)
  end
end

#send(request) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/httpx/connection/http2.rb', line 42

def send(request, **)
  if !@handshake_completed ||
     @connection.active_stream_count >= @max_concurrent_requests
    @pending << request
    return
  end
  unless (stream = @streams[request])
    stream = @connection.new_stream
    handle_stream(stream, request)
    @streams[request] = stream
  end
  handle(request, stream)
  true
end