Skip to content

Commit

Permalink
Initial implementation (not working).
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 21, 2024
1 parent 16d6ad1 commit c321b89
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

gemspec

gem "faraday", git: "https://github.com/lostisland/faraday.git", branch: "mg/parallel-manager-execute"

group :maintenance, optional: true do
gem "bake-modernize"
gem "bake-gem"
Expand Down
54 changes: 52 additions & 2 deletions lib/async/http/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

require 'faraday'
require 'faraday/adapter'

require 'async/barrier'
require 'kernel/sync'

require 'async/http/client'
Expand Down Expand Up @@ -48,8 +50,41 @@ def read
end
end

class ParallelManager
def initialize(options = {})
@options = options
@barrier = nil
end

def run
raise NotImplementedError, "Please update your Faraday version!"
end

def async(&block)
@barrier.async(&block)
end

def execute(&block)
Sync do
@barrier = Async::Barrier.new

yield

@barrier.wait
ensure
@barrier&.stop
end
end
end

# An adapter that allows Faraday to use Async::HTTP as the underlying HTTP client.
class Adapter < ::Faraday::Adapter
self.supports_parallel = true

def self.setup_parallel_manager(**options)
ParallelManager.new(options)
end

# The exceptions that are considered connection errors and result in a `Faraday::ConnectionFailed` exception.
CONNECTION_EXCEPTIONS = [
Errno::EADDRNOTAVAIL,
Expand Down Expand Up @@ -98,6 +133,23 @@ def call(env)
# For compatibility with the default adapter:
env.url.path = '/' if env.url.path.empty?

if parallel_manager = env.parallel_manager
parallel_manager.async do
perform_request(env)

# Do I need this line?
env.response.finish(env)
end
else
perform_request(env)
end

return env.response
end

private

def perform_request(env)
with_client(env) do |endpoint, client|
if body = env.body
# We need to ensure the body is wrapped in a Readable object so that it can be read in chunks:
Expand Down Expand Up @@ -149,8 +201,6 @@ def call(env)
raise ::Faraday::ConnectionFailed, e
end

private

def with_client(env)
Sync do
endpoint = Endpoint.new(env.url)
Expand Down
43 changes: 43 additions & 0 deletions test/async/http/faraday/adapter/parallel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'async/http/faraday'

require 'sus/fixtures/async/reactor_context'
require 'sus/fixtures/async/http/server_context'

describe Async::HTTP::Faraday::Adapter do
with "a local http server" do
include Sus::Fixtures::Async::ReactorContext
include Sus::Fixtures::Async::HTTP::ServerContext


let(:app) do
Protocol::HTTP::Middleware.for do |request|
Protocol::HTTP::Response[200, {}, ['Hello World']]
end
end

it "client can get resource" do
adapter = Faraday.new(bound_url) do |builder|
builder.adapter :async_http
end

response1 = response2 = response3 = nil

adapter.in_parallel do
response1 = adapter.get("/index")
response2 = adapter.get("/index")
resposne3 = adapter.get("/index")
end

expect(response1.body).to be == 'Hello World'
expect(response2.body).to be == 'Hello World'
expect(response3.body).to be == 'Hello World'
ensure
adapter&.close
end
end
end

0 comments on commit c321b89

Please sign in to comment.