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 Loggable
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 70 |
# 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 on(:error) { |ex| on_error(ex) } 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
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/httpx/channel.rb', line 123 def call case @state when :closed return when :closing dwrite transition(:closed) emit(:close) when :open consume end nil end |
#close ⇒ Object
104 105 106 107 |
# File 'lib/httpx/channel.rb', line 104 def close @parser.close if @parser transition(:closing) end |
#interests ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/httpx/channel.rb', line 85 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
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/httpx/channel.rb', line 72 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
109 110 111 112 113 |
# File 'lib/httpx/channel.rb', line 109 def reset transition(:closing) transition(:closed) emit(:close) end |
#send(request, **args) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/httpx/channel.rb', line 115 def send(request, **args) if @parser && !@write_buffer.full? parser.send(request, **args) else @pending << [request, args] end end |
#to_io ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/httpx/channel.rb', line 96 def to_io case @state when :idle transition(:open) end @io.to_io end |
#upgrade_parser(protocol) ⇒ Object
137 138 139 140 |
# File 'lib/httpx/channel.rb', line 137 def upgrade_parser(protocol) @parser.reset if @parser @parser = build_parser(protocol) end |