Class: Quicsilver::Client
- Inherits:
-
Object
- Object
- Quicsilver::Client
- Includes:
- Protocol::ControlStreamParser
- Defined in:
- lib/quicsilver/client/client.rb,
lib/quicsilver/client/request.rb,
lib/quicsilver/client/connection_pool.rb
Defined Under Namespace
Classes: ConnectionPool, Request
Constant Summary collapse
- StreamFailedToOpenError =
Class.new(StandardError)
- GoAwayError =
Class.new(StandardError)
- FINISHED_EVENTS =
%w[RECEIVE_FIN RECEIVE STREAM_RESET STOP_SENDING].freeze
- DEFAULT_REQUEST_TIMEOUT =
seconds
30- DEFAULT_CONNECTION_TIMEOUT =
ms
5000
Constants included from Protocol::ControlStreamParser
Protocol::ControlStreamParser::HTTP2_SETTINGS
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#connection_timeout ⇒ Object
readonly
Returns the value of attribute connection_timeout.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#peer_goaway_id ⇒ Object
readonly
Returns the value of attribute peer_goaway_id.
-
#peer_max_field_section_size ⇒ Object
readonly
Returns the value of attribute peer_max_field_section_size.
-
#peer_settings ⇒ Object
readonly
Returns the value of attribute peer_settings.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#request_timeout ⇒ Object
readonly
Returns the value of attribute request_timeout.
-
#unsecure ⇒ Object
readonly
Returns the value of attribute unsecure.
Class Method Summary collapse
- .close_pool ⇒ Object
- .request(hostname, port, method, path, headers: {}, body: nil, **options, &block) ⇒ Object
Instance Method Summary collapse
- #authority ⇒ Object
- #build_request(method, path, headers: {}, body: nil) ⇒ Object
-
#close_connection ⇒ Object
:nodoc:.
- #connected? ⇒ Boolean
- #connection_info ⇒ Object
- #connection_uptime ⇒ Object
-
#disconnect ⇒ Object
Disconnect and close the underlying QUIC connection.
- #draining? ⇒ Boolean
-
#handle_stream_event(stream_id, event, data, _early_data) ⇒ Object
Called directly by C extension via dispatch_to_ruby.
-
#initialize(hostname, port = 4433, **options) ⇒ Client
constructor
A new instance of Client.
-
#open_connection ⇒ Object
:nodoc:.
-
#receive_control_data(stream_id, data) ⇒ Object
:nodoc:.
Methods included from Protocol::ControlStreamParser
#parse_control_frames, #parse_peer_goaway, #parse_peer_settings
Constructor Details
#initialize(hostname, port = 4433, **options) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/quicsilver/client/client.rb', line 18 def initialize(hostname, port = 4433, **) @hostname = hostname @port = port @unsecure = .fetch(:unsecure, true) @connection_timeout = .fetch(:connection_timeout, DEFAULT_CONNECTION_TIMEOUT) @request_timeout = .fetch(:request_timeout, DEFAULT_REQUEST_TIMEOUT) @max_body_size = [:max_body_size] @max_header_size = [:max_header_size] @connection_data = nil @connected = false @connection_start_time = nil @response_buffers = {} @pending_requests = {} # handle => Request @mutex = Mutex.new # Server control stream state @peer_settings = {} @peer_max_field_section_size = nil @peer_goaway_id = nil @settings_received = false @control_stream_id = nil @uni_stream_types = {} end |
Class Attribute Details
.pool ⇒ Object
52 53 54 |
# File 'lib/quicsilver/client/client.rb', line 52 def pool @pool ||= ConnectionPool.new end |
Instance Attribute Details
#connection_timeout ⇒ Object (readonly)
Returns the value of attribute connection_timeout.
7 8 9 |
# File 'lib/quicsilver/client/client.rb', line 7 def connection_timeout @connection_timeout end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
7 8 9 |
# File 'lib/quicsilver/client/client.rb', line 7 def hostname @hostname end |
#peer_goaway_id ⇒ Object (readonly)
Returns the value of attribute peer_goaway_id.
8 9 10 |
# File 'lib/quicsilver/client/client.rb', line 8 def peer_goaway_id @peer_goaway_id end |
#peer_max_field_section_size ⇒ Object (readonly)
Returns the value of attribute peer_max_field_section_size.
8 9 10 |
# File 'lib/quicsilver/client/client.rb', line 8 def peer_max_field_section_size @peer_max_field_section_size end |
#peer_settings ⇒ Object (readonly)
Returns the value of attribute peer_settings.
8 9 10 |
# File 'lib/quicsilver/client/client.rb', line 8 def peer_settings @peer_settings end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
7 8 9 |
# File 'lib/quicsilver/client/client.rb', line 7 def port @port end |
#request_timeout ⇒ Object (readonly)
Returns the value of attribute request_timeout.
7 8 9 |
# File 'lib/quicsilver/client/client.rb', line 7 def request_timeout @request_timeout end |
#unsecure ⇒ Object (readonly)
Returns the value of attribute unsecure.
7 8 9 |
# File 'lib/quicsilver/client/client.rb', line 7 def unsecure @unsecure end |
Class Method Details
.close_pool ⇒ Object
56 57 58 59 |
# File 'lib/quicsilver/client/client.rb', line 56 def close_pool @pool&.close @pool = nil end |
.request(hostname, port, method, path, headers: {}, body: nil, **options, &block) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/quicsilver/client/client.rb', line 68 def request(hostname, port, method, path, headers: {}, body: nil, **, &block) client = pool.checkout(hostname, port, **) client.public_send(method, path, headers: headers, body: body, &block) ensure pool.checkin(client) if client end |
Instance Method Details
#authority ⇒ Object
147 148 149 |
# File 'lib/quicsilver/client/client.rb', line 147 def "#{@hostname}:#{@port}" end |
#build_request(method, path, headers: {}, body: nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/quicsilver/client/client.rb', line 118 def build_request(method, path, headers: {}, body: nil) ensure_connected! raise GoAwayError, "Connection is draining (GOAWAY received)" if draining? stream = open_stream raise StreamFailedToOpenError unless stream request = Request.new(self, stream) @mutex.synchronize { @pending_requests[stream.handle] = request } send_to_stream(stream, method, path, headers, body) request end |
#close_connection ⇒ Object
:nodoc:
174 175 176 177 178 |
# File 'lib/quicsilver/client/client.rb', line 174 def close_connection Quicsilver.close_connection_handle(@connection_data) if @connection_data @connection_data = nil @connected = false end |
#connected? ⇒ Boolean
133 134 135 |
# File 'lib/quicsilver/client/client.rb', line 133 def connected? @connected && @connection_data && connection_alive? end |
#connection_info ⇒ Object
137 138 139 140 |
# File 'lib/quicsilver/client/client.rb', line 137 def connection_info info = @connection_data ? Quicsilver.connection_status(@connection_data[1]) : {} info.merge(hostname: @hostname, port: @port, uptime: connection_uptime) end |
#connection_uptime ⇒ Object
142 143 144 145 |
# File 'lib/quicsilver/client/client.rb', line 142 def connection_uptime return 0 unless @connection_start_time Time.now - @connection_start_time end |
#disconnect ⇒ Object
Disconnect and close the underlying QUIC connection.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/quicsilver/client/client.rb', line 77 def disconnect return unless @connected @connected = false @mutex.synchronize do @pending_requests.each_value { |req| req.fail(0, "Connection closed") } @pending_requests.clear @response_buffers.clear end close_connection end |
#draining? ⇒ Boolean
104 105 106 |
# File 'lib/quicsilver/client/client.rb', line 104 def draining? !@peer_goaway_id.nil? end |
#handle_stream_event(stream_id, event, data, _early_data) ⇒ Object
Called directly by C extension via dispatch_to_ruby
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/quicsilver/client/client.rb', line 181 def handle_stream_event(stream_id, event, data, _early_data) # :nodoc: return unless FINISHED_EVENTS.include?(event) # Server unidirectional streams (control, QPACK) — process incrementally if (stream_id & 0x02) != 0 && (event == "RECEIVE" || event == "RECEIVE_FIN") begin receive_control_data(stream_id, data) rescue Protocol::FrameError => e Quicsilver.logger.error("Control stream error: #{e.} (0x#{e.error_code.to_s(16)})") end return end @mutex.synchronize do case event when "RECEIVE" (@response_buffers[stream_id] ||= "".b) << data when "RECEIVE_FIN" event = Transport::StreamEvent.new(data, "RECEIVE_FIN") buffer = @response_buffers.delete(stream_id) full_data = (buffer || "".b) + event.data response_parser = Protocol::ResponseParser.new(full_data, max_body_size: @max_body_size, max_header_size: @max_header_size) response_parser.parse response = { status: response_parser.status, headers: response_parser.headers, body: response_parser.body.read } request = @pending_requests.delete(event.handle) request&.complete(response) when "STREAM_RESET" event = Transport::StreamEvent.new(data, "STREAM_RESET") request = @pending_requests.delete(event.handle) request&.fail(event.error_code, "Stream reset by peer") when "STOP_SENDING" event = Transport::StreamEvent.new(data, "STOP_SENDING") request = @pending_requests.delete(event.handle) request&.fail(event.error_code, "Peer sent STOP_SENDING") end end rescue => e Quicsilver.logger.error("Error handling client stream: #{e.class} - #{e.}") Quicsilver.logger.debug(e.backtrace.first(5).join("\n")) end |
#open_connection ⇒ Object
:nodoc:
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/quicsilver/client/client.rb', line 152 def open_connection return self if @connected Quicsilver.open_connection config = Quicsilver.create_configuration(@unsecure) raise ConnectionError, "Failed to create configuration" if config.nil? start_connection(config) @connected = true @connection_start_time = Time.now send_control_stream Quicsilver.event_loop.start self rescue => e cleanup_failed_connection raise e.is_a?(ConnectionError) || e.is_a?(TimeoutError) ? e : ConnectionError.new("Connection failed: #{e.}") ensure Quicsilver.close_configuration(config) if config end |
#receive_control_data(stream_id, data) ⇒ Object
:nodoc:
108 109 110 111 112 113 114 115 116 |
# File 'lib/quicsilver/client/client.rb', line 108 def receive_control_data(stream_id, data) # :nodoc: buf = @uni_stream_types.key?(stream_id) ? data : identify_and_strip_stream_type(stream_id, data) return if buf.nil? || buf.empty? case @uni_stream_types[stream_id] when :control parse_control_frames(buf) end end |