mirror of
https://github.com/cyberarm/w3d_hub_linux_launcher.git
synced 2026-03-22 04:06:18 +00:00
Pivot away from Ractor (stil overly complicated to work with, even in Ruby 4) and towards 1 thread for http requests and 1 thread for application tasks, WIP refactor if Api to be non-blocking async- everything is broken atm 😁
This commit is contained in:
61
lib/network_manager/http_client.rb
Normal file
61
lib/network_manager/http_client.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
class W3DHub
|
||||
class NetworkManager
|
||||
# non-blocking, http requests.
|
||||
class HttpClient
|
||||
def initialize
|
||||
@http_clients = {}
|
||||
end
|
||||
|
||||
def handle(task, request)
|
||||
result = CyberarmEngine::Result.new
|
||||
context = request.context
|
||||
|
||||
task.with_timeout(30) do
|
||||
uri = URI(context.url)
|
||||
|
||||
pp uri
|
||||
|
||||
response = provision_http_client(uri.origin).send(
|
||||
context.method,
|
||||
uri.path,
|
||||
context.headers,
|
||||
context.body
|
||||
)
|
||||
|
||||
pp response
|
||||
|
||||
if response.success?
|
||||
result.data = response.body.read
|
||||
else
|
||||
result.error = response
|
||||
end
|
||||
rescue Async::TimeoutError => e
|
||||
result.error = e
|
||||
rescue StandardError => e
|
||||
result.error = e
|
||||
ensure
|
||||
response&.close
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def provision_http_client(hostname)
|
||||
return @http_clients[hostname.downcase] if @http_clients[hostname.downcase]
|
||||
|
||||
ssl_context = W3DHub.ca_bundle_path ? OpenSSL::SSL::SSLContext.new : nil
|
||||
ssl_context&.set_params(
|
||||
ca_file: W3DHub.ca_bundle_path,
|
||||
verify_mode: OpenSSL::SSL::VERIFY_PEER
|
||||
)
|
||||
|
||||
endpoint = Async::HTTP::Endpoint.parse(hostname, ssl_context: ssl_context)
|
||||
@http_clients[hostname.downcase] = Async::HTTP::Client.new(endpoint)
|
||||
end
|
||||
|
||||
def wrapped_error(error)
|
||||
WrappedError.new(error.class, error.message.to_s, error.backtrace)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user