Class: OpenAI::NetHTTPClient
- Inherits:
-
HTTPClient
- Object
- OpenAI::NetHTTPClient
- Defined in:
- lib/openai/net_http_client.rb,
sig/openai/net_http_client.rbs
Overview
The SDK's pooled Net::HTTP implementation.
Pass a block to configure each SDK-created connection before it is pooled and started.
Constant Summary collapse
- KEEP_ALIVE_TIMEOUT =
30- DEFAULT_MAX_CONNECTIONS =
[Etc.nprocessors, 99].max
- URI =
Instance Method Summary collapse
-
#close ⇒ void
Closes current pooled connections.
-
#execute(request) ⇒ OpenAI::HTTPClient::Response
Executes a request using a pooled Net::HTTP connection.
-
#initialize(size: self.class::DEFAULT_MAX_CONNECTIONS, &connection_configurator) ⇒ NetHTTPClient
constructor
A new instance of NetHTTPClient.
Constructor Details
#initialize(size: self.class::DEFAULT_MAX_CONNECTIONS, &connection_configurator) ⇒ NetHTTPClient
Returns a new instance of NetHTTPClient.
272 273 274 275 276 277 278 279 |
# File 'lib/openai/net_http_client.rb', line 272 def initialize(size: self.class::DEFAULT_MAX_CONNECTIONS, &connection_configurator) super() @mutex = Mutex.new @size = size @cert_store = OpenSSL::X509::Store.new.tap(&:set_default_paths) @connection_configurator = connection_configurator @pools = {} end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Closes current pooled connections. The client remains reusable and will create fresh pools on subsequent requests.
In-flight requests are allowed to finish before their connection closes.
184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/openai/net_http_client.rb', line 184 def close pools = @mutex.synchronize do current_pools = @pools @pools = {} current_pools end pools.each_value do |pool| pool.shutdown { |connection| connection.finish if connection.started? } end nil end |
#execute(request) ⇒ OpenAI::HTTPClient::Response
Executes a request using a pooled Net::HTTP connection.
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/openai/net_http_client.rb', line 201 def execute(request) url = request.url deadline = OpenAI::Internal::Util.monotonic_secs + request.timeout req = nil finished = false # rubocop:disable Metrics/BlockLength enum = Enumerator.new do |y| next if finished with_pool(url, deadline: deadline) do |conn| eof = false closing = nil ::Thread.handle_interrupt(Object => :never) do ::Thread.handle_interrupt(Object => :immediate) do req, closing = build_request(request) do calibrate_socket_timeout(conn, deadline) end calibrate_socket_timeout(conn, deadline) conn.start unless conn.started? calibrate_socket_timeout(conn, deadline) ::Kernel.catch(:jump) do conn.request(req) do |rsp| y << [req, rsp] ::Kernel.throw(:jump) if finished rsp.read_body do |bytes| y << bytes.force_encoding(Encoding::BINARY) ::Kernel.throw(:jump) if finished calibrate_socket_timeout(conn, deadline) end eof = true end end end ensure begin conn.finish if !eof && conn&.started? ensure closing&.call end end end rescue ConnectionConfigurationError => e raise e.original, cause: e.original.cause rescue Timeout::Error raise OpenAI::Errors::APITimeoutError.new(url: url, request: req) rescue *NETWORK_ERRORS raise OpenAI::Errors::APIConnectionError.new(url: url, request: req) end # rubocop:enable Metrics/BlockLength _, response = enum.next body = OpenAI::Internal::Util.fused_enum(enum, external: true) do finished = true loop { enum.next } end OpenAI::HTTPClient::Response.new( status: Integer(response.code), headers: response.each_header.to_h, body: body ) end |