Class: Async::HTTP::Faraday::Adapter

Inherits:
Faraday::Adapter
  • Object
show all
Defined in:
lib/async/http/faraday/adapter.rb

Overview

An adapter that allows Faraday to use Async::HTTP as the underlying HTTP client.

Constant Summary collapse

CONNECTION_EXCEPTIONS =

The exceptions that are considered connection errors and result in a Faraday::ConnectionFailed exception.

[
  ::Protocol::HTTP::Error,
  Errno::EADDRNOTAVAIL,
  Errno::ECONNABORTED,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::EINVAL,
  Errno::ENETUNREACH,
  Errno::EPIPE,
  IOError,
  SocketError
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAdapter

Create a Faraday compatible adapter.



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/async/http/faraday/adapter.rb', line 127

def initialize(...)
  super
  
  @timeout = @connection_options.delete(:timeout)
  @read_timeout = @connection_options.delete(:read_timeout)
  
  if clients = @connection_options.delete(:clients)
    @clients = clients.call(**@connection_options, &@config_block)
  else
    @clients = PerThreadPersistentClients.new(**@connection_options, &@config_block)
  end
end

Instance Attribute Details

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



144
145
146
# File 'lib/async/http/faraday/adapter.rb', line 144

def read_timeout
  @read_timeout
end

#The maximum time to send a request and wait for a response.(maximumtimetosendarequest) ⇒ Object (readonly)



141
# File 'lib/async/http/faraday/adapter.rb', line 141

attr :timeout

#The maximum time to wait for an individual IO operation.(maximumtimetowait) ⇒ Object (readonly)



144
# File 'lib/async/http/faraday/adapter.rb', line 144

attr :read_timeout

#timeoutObject (readonly)

Returns the value of attribute timeout.



141
142
143
# File 'lib/async/http/faraday/adapter.rb', line 141

def timeout
  @timeout
end

Class Method Details

.setup_parallel_manager(**options) ⇒ Object

Create a new parallel manager, which is used to handle multiple concurrent requests.



104
105
106
# File 'lib/async/http/faraday/adapter.rb', line 104

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

Instance Method Details

#call(env) ⇒ Object

Make a request using the adapter.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/async/http/faraday/adapter.rb', line 158

def call(env)
  super
  
  # 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)
      env.response.finish(env)
    end
  else
    perform_request(env)
  end
  
  @app.call(env)
end

#closeObject

Close all clients.



147
148
149
150
# File 'lib/async/http/faraday/adapter.rb', line 147

def close
  # The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
  @clients.close
end