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.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/httpx/channel.rb', line 62 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 55 |
# File 'lib/httpx/channel.rb', line 44 def by(uri, ) type = .transport || begin case uri.scheme when "http" then "tcp" when "https" then "ssl" else raise Error, "#{uri}: #{uri.scheme}: unrecognized channel" end end io = IO.registry(type).new(uri, ) new(io, ) end |
Instance Method Details
#call ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/httpx/channel.rb', line 124 def call case @state when :closed return when :closing dwrite transition(:closed) emit(:close) when :open consume end nil end |
#close ⇒ Object
105 106 107 108 |
# File 'lib/httpx/channel.rb', line 105 def close @parser.close if @parser transition(:closing) end |
#interests ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/httpx/channel.rb', line 86 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
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/httpx/channel.rb', line 73 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
110 111 112 113 114 |
# File 'lib/httpx/channel.rb', line 110 def reset transition(:closing) transition(:closed) emit(:close) end |
#send(request, **args) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/httpx/channel.rb', line 116 def send(request, **args) if @parser && !@write_buffer.full? parser.send(request, **args) else @pending << [request, args] end end |
#to_io ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/httpx/channel.rb', line 97 def to_io case @state when :idle transition(:open) end @io.to_io end |
#upgrade_parser(protocol) ⇒ Object
138 139 140 141 |
# File 'lib/httpx/channel.rb', line 138 def upgrade_parser(protocol) @parser.reset if @parser @parser = build_parser(protocol) end |