The adapter now supports the in_parallel
method, which allows multiple requests to be made concurrently.
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")
response3 = adapter.get("/index")
end
puts response1.body # => "Hello World"
puts response2.body # => "Hello World"
puts response3.body # => "Hello World"
This is primarily for compatibility with existing code. If you are designing a new library, you should just use Async
directly:
Async do
response1 = Async{adapter.get("/index")}
response2 = Async{adapter.get("/index")}
response3 = Async{adapter.get("/index")}
puts response1.wait.body # => "Hello World"
puts response2.wait.body # => "Hello World"
puts response3.wait.body # => "Hello World"
end
The config_block
provided to the adapter must now return nil
, client
or a middleware wrapper around client
.
Faraday.new do |builder|
builder.adapter :async_http do |client|
# Option 1 (same as returning `nil`), use client as is:
client # Use `client` as is.
# Option 2, wrap client in a middleware:
Async::HTTP::Middleware::LocationRedirector.new(client)
end
end
The default adapter now uses a per-thread client cache internally, to improve compatibility with existing code that shares a single Faraday::Connection
instance across multiple threads.
adapter = Faraday.new do |builder|
builder.adapter :async_http
end
3.times do
Thread.new do
Async do
# Each thread has it's own client cache.
adapter.get('http://example.com')
end
end
end