lib/grumlin/request_dispatcher.rb in grumlin-0.3.0 vs lib/grumlin/request_dispatcher.rb in grumlin-0.4.0

- old
+ new

@@ -27,16 +27,16 @@ end def add_request(request) raise "ERROR" if @requests.key?(request[:requestId]) - Async::Queue.new.tap do |queue| - @requests[request[:requestId]] = { request: request, result: [], queue: queue } + Async::Channel.new.tap do |channel| + @requests[request[:requestId]] = { request: request, result: [], channel: channel } end end - # builds a response object, when it's ready sends it to the client via a queue + # builds a response object, when it's ready sends it to the client via a channel # TODO: sometimes response does not include requestID, no idea how to handle it so far. def add_response(response) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength request_id = response[:requestId] raise "ERROR" unless ongoing_request?(request_id) @@ -44,26 +44,26 @@ check_errors!(response[:status]) case SUCCESS[response.dig(:status, :code)] when :success - request[:queue] << [:result, request[:result] + [response.dig(:result, :data)]] + request[:channel] << request[:result] + [response.dig(:result, :data)] close_request(request_id) when :partial_content then request[:result] << response.dig(:result, :data) when :no_content - request[:queue] << [:result, []] + request[:channel] << [] close_request(request_id) end rescue StandardError => e - request[:queue] << [:error, e] + request[:channel].exception(e) close_request(request_id) end def close_request(request_id) raise "ERROR" unless ongoing_request?(request_id) request = @requests.delete(request_id) - request[:queue] << nil + request[:channel].close end def ongoing_request?(request_id) @requests.key?(request_id) end