Class: HTTPX::Channel
- Inherits:
-
Object
- Object
- HTTPX::Channel
- Extended by:
- Forwardable
- Defined in:
- lib/httpx/channel.rb
Overview
The Channel entity can be watched for IO events.
It contains the io object to read/write from, and knows what to do when it can.
It defers connecting until absolutely necessary. Connection should be triggered from the IO selector (until then, any request will be queued).
A channel boots up its parser after connection is established. All pending requests will be redirected there after connection.
A channel can be prevented from closing by the parser, that is, if there are pending requests. This will signal that the channel was prematurely closed, due to a possible number of conditions:
- Remote peer closed the connection ("Connection: close");
- Remote peer doesn't support pipelining;
A channel may also route requests for a different host for which the io was connected
to, provided that the IP is the same and the port and scheme as well. This will allow to
share the same socket to send HTTP/2 requests to different hosts.
TODO: For this to succeed, the certificates sent by the servers to the client must be
identical (or match both hosts).
Direct Known Subclasses
Defined Under Namespace
Constant Summary collapse
- BUFFER_SIZE =
1 << 14
Constants included from Registry
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
- #close ⇒ Object
-
#initialize(io, options) ⇒ Channel
constructor
A new instance of Channel.
- #interests ⇒ Object
- #match?(uri) ⇒ Boolean
- #reset ⇒ Object
- #send(request, **args) ⇒ Object
- #to_io ⇒ Object
- #upgrade_parser(protocol) ⇒ Object
Methods included from Callbacks
Methods included from Loggable
Methods included from Registry
Constructor Details
#initialize(io, options) ⇒ Channel
Returns a new instance of Channel.
61 62 63 64 65 66 67 68 69 |
# File 'lib/httpx/channel.rb', line 61 def initialize(io, ) @io = io @options = Options.new() @window_size = @options.window_size @read_buffer = Buffer.new(BUFFER_SIZE) @write_buffer = Buffer.new(BUFFER_SIZE) @pending = [] @state = :idle end |
Class Method Details
.by(uri, options) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/httpx/channel.rb', line 44 def by(uri, ) io = case uri.scheme when "http" IO.registry("tcp").new(uri.host, uri.port, ) when "https" IO.registry("ssl").new(uri.host, uri.port, ) else raise Error, "#{uri}: #{uri.scheme}: unrecognized channel" end new(io, ) end |
Instance Method Details
#call ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/httpx/channel.rb', line 122 def call case @state when :closed return when :closing dwrite transition(:closed) emit(:close) when :open consume end nil end |
#close ⇒ Object
103 104 105 106 |
# File 'lib/httpx/channel.rb', line 103 def close @parser.close if @parser transition(:closing) end |
#interests ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/httpx/channel.rb', line 84 def interests return :w if @state == :idle readable = !@read_buffer.full? writable = !@write_buffer.empty? if readable writable ? :rw : :r else writable ? :w : :r end end |
#match?(uri) ⇒ Boolean
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/httpx/channel.rb', line 71 def match?(uri) return false if @state == :closing ips = begin Resolv.getaddresses(uri.host) rescue StandardError [uri.host] end ips.include?(@io.ip) && uri.port == @io.port && uri.scheme == @io.scheme end |
#reset ⇒ Object
108 109 110 111 112 |
# File 'lib/httpx/channel.rb', line 108 def reset transition(:closing) transition(:closed) emit(:close) end |
#send(request, **args) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/httpx/channel.rb', line 114 def send(request, **args) if @parser && !@write_buffer.full? parser.send(request, **args) else @pending << [request, args] end end |
#to_io ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/httpx/channel.rb', line 95 def to_io case @state when :idle transition(:open) end @io.to_io end |
#upgrade_parser(protocol) ⇒ Object
136 137 138 139 |
# File 'lib/httpx/channel.rb', line 136 def upgrade_parser(protocol) @parser.reset if @parser @parser = build_parser(protocol) end |