Class: HTTPX::Channel::HTTP2

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

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

Methods included from HTTPX::Callbacks

#emit, #on, #once

Constructor Details

#initialize(buffer, options) ⇒ HTTP2

Returns a new instance of HTTP2.



18
19
20
21
22
23
24
25
26
# File 'lib/httpx/channel/http2.rb', line 18

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



241
242
243
244
245
246
247
# File 'lib/httpx/channel/http2.rb', line 241

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.



16
17
18
# File 'lib/httpx/channel/http2.rb', line 16

def pending
  @pending
end

#streamsObject (readonly)

Returns the value of attribute streams.



16
17
18
# File 'lib/httpx/channel/http2.rb', line 16

def streams
  @streams
end

Instance Method Details

#<<(data) ⇒ Object



36
37
38
# File 'lib/httpx/channel/http2.rb', line 36

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

#closeObject



28
29
30
# File 'lib/httpx/channel/http2.rb', line 28

def close
  @connection.goaway
end

#consumeObject



62
63
64
65
66
# File 'lib/httpx/channel/http2.rb', line 62

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

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/httpx/channel/http2.rb', line 32

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

#handle_error(ex) ⇒ Object



68
69
70
71
72
# File 'lib/httpx/channel/http2.rb', line 68

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

#reenqueue!Object



53
54
55
56
57
58
59
60
# File 'lib/httpx/channel/http2.rb', line 53

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

#send(request) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/httpx/channel/http2.rb', line 40

def send(request, **)
  if @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)
end