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:
@@ -5,6 +5,7 @@ class W3DHub
|
||||
Request = Struct.new(:context, :callback)
|
||||
Context = Data.define(
|
||||
:request_id,
|
||||
:method,
|
||||
:url,
|
||||
:headers,
|
||||
:body,
|
||||
@@ -12,52 +13,42 @@ class W3DHub
|
||||
)
|
||||
|
||||
def initialize
|
||||
@requests = {}
|
||||
@requests = []
|
||||
@running = true
|
||||
|
||||
@ractor = Ractor.new do
|
||||
raise "Something has gone quite wrong!" if Ractor.main?
|
||||
Thread.new do
|
||||
http_client = HttpClient.new
|
||||
|
||||
queue = []
|
||||
api_client = ApiClient.new
|
||||
Sync do
|
||||
while @running
|
||||
request = @requests.shift
|
||||
|
||||
# Ractor has no concept of non-blocking send/receive... :cry:
|
||||
Thread.new do
|
||||
while (context = Ractor.receive) # blocking
|
||||
# we cannot (easily) ensure we always are receive expected data
|
||||
next unless context.is_a?(Context)
|
||||
|
||||
queue << context
|
||||
end
|
||||
end
|
||||
|
||||
Async do
|
||||
loop do
|
||||
context = queue.shift
|
||||
|
||||
# goto sleep for an instant if there is no work to be doing
|
||||
unless context
|
||||
sleep 0.1
|
||||
# goto sleep for an second if there is no work to be doing
|
||||
unless request
|
||||
sleep 1
|
||||
next
|
||||
end
|
||||
|
||||
Sync do
|
||||
result = api_client.handle(context)
|
||||
Async do |task|
|
||||
assigned_request = request
|
||||
result = http_client.handle(task, assigned_request)
|
||||
|
||||
Ractor.yield(NetworkEvent.new(context, result))
|
||||
pp [assigned_request, result]
|
||||
|
||||
Store.main_thread_queue << -> { assigned_request.callback.call(result) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
monitor
|
||||
end
|
||||
|
||||
def add_request(url, headers, body, bearer_token, &block)
|
||||
def request(method, url, headers, body, bearer_token, &block)
|
||||
request_id = SecureRandom.hex
|
||||
|
||||
@requests << Request.new(
|
||||
request = Request.new(
|
||||
Context.new(
|
||||
request_id,
|
||||
method,
|
||||
url,
|
||||
headers,
|
||||
body,
|
||||
@@ -66,31 +57,9 @@ class W3DHub
|
||||
block
|
||||
)
|
||||
|
||||
@ractor.send(context)
|
||||
@requests << request
|
||||
|
||||
request_id
|
||||
end
|
||||
|
||||
def monitor
|
||||
raise "Something has gone quite wrong!!!" unless Ractor.main?
|
||||
|
||||
# Thread that spends its days sleeping **yawn**
|
||||
Thread.new do
|
||||
while (event = @ractor.take)
|
||||
pp event
|
||||
|
||||
next unless event.is_a?(NetworkEvent)
|
||||
|
||||
request = @request.find { |r| r.context.request_id == event.context.request_id }
|
||||
|
||||
next if request
|
||||
|
||||
@requests.delete(request)
|
||||
result = event.result
|
||||
|
||||
Store.main_thread_queue << ->(result) { request.callback(result) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user