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

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

Constant Summary collapse

CONNECTION_EXCEPTIONS =
[
  Errno::EADDRNOTAVAIL,
  Errno::ECONNABORTED,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::EINVAL,
  Errno::ENETUNREACH,
  Errno::EPIPE,
  IOError,
  SocketError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(*arguments, **options, &block) ⇒ Adapter

Returns a new instance of Adapter.



48
49
50
51
52
53
54
# File 'lib/async/http/faraday/adapter.rb', line 48

def initialize(*arguments, **options, &block)
  super
  
  @internet = Async::HTTP::Internet.new
  @persistent = PERSISTENT && options.fetch(:persistent, true)
  @timeout = options[:timeout]
end

Instance Method Details

#call(env) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/async/http/faraday/adapter.rb', line 60

def call(env)
  super
  
  parent = Async::Task.current?
  
  Sync do
    with_timeout do
      response = @internet.call(env[:method].to_s.upcase, env[:url].to_s, env[:request_headers], env[:body] || [])
    
      save_response(env, response.status, response.read, response.headers)
    end
  ensure
    # If we are the top level task, even if we are persistent, we must close the connection:
    if parent.nil? || !@persistent
      Async.logger.debug(self) {"Closing persistent connections."}
      @internet.close
    end
  end
  
  return @app.call(env)
rescue Errno::ETIMEDOUT, Async::TimeoutError => e
  raise ::Faraday::TimeoutError, e
rescue OpenSSL::SSL::SSLError => e
  raise ::Faraday::SSLError, e
rescue *CONNECTION_EXCEPTIONS => e
  raise ::Faraday::ConnectionFailed, e
end

#closeObject



56
57
58
# File 'lib/async/http/faraday/adapter.rb', line 56

def close
  @internet.close
end