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, timeout: nil, **options, &block) ⇒ Adapter

Returns a new instance of Adapter.



52
53
54
55
56
57
58
59
60
# File 'lib/async/http/faraday/adapter.rb', line 52

def initialize(*arguments, timeout: nil, **options, &block)
  super(*arguments, **options)
  
  @timeout = timeout
  
  @clients = {}
  
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/async/http/faraday/adapter.rb', line 102

def call(env)
  super
  
  # for compatibility with the default adapter
  env.url.path = '/' if env.url.path.empty?
  
  Sync do
    endpoint = Endpoint.new(env.url)
    
    if proxy = env.request.proxy
      proxy_endpoint = Endpoint.new(proxy.uri)
      client = self.proxy_client_for(proxy_endpoint, endpoint)
    else
      client = self.client_for(endpoint)
    end
    
    if body = env.body
      # We need to wrap the body in a Readable object so that it can be read in chunks:
      # Faraday's body only responds to `#read`.
      # body = ::Protocol::HTTP::Body::Buffered.wrap(body)
      body = BodyWrapper.new(body)
    end
    
    if headers = env.request_headers
      headers = ::Protocol::HTTP::Headers[headers]
    end
    
    method = env.method.to_s.upcase
    
    request = ::Protocol::HTTP::Request.new(endpoint.scheme, endpoint.authority, method, endpoint.path, nil, headers, body)
    
    with_timeout do
      response = client.call(request)
      
      save_response(env, response.status, encoded_body(response), response.headers)
    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

#client_for(endpoint) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/async/http/faraday/adapter.rb', line 76

def client_for(endpoint)
  key = host_key(endpoint)
  
  @clients.fetch(key) do
    @clients[key] = make_client(endpoint)
  end
end

#closeObject



93
94
95
96
97
98
99
100
# File 'lib/async/http/faraday/adapter.rb', line 93

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 = @clients.values
  
  @clients.clear
  
  clients.each(&:close)
end

#host_key(endpoint) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/async/http/faraday/adapter.rb', line 66

def host_key(endpoint)
  url = endpoint.url.dup
  
  url.path = ""
  url.fragment = nil
  url.query = nil
  
  return url
end

#make_client(endpoint) ⇒ Object



62
63
64
# File 'lib/async/http/faraday/adapter.rb', line 62

def make_client(endpoint)
  Client.new(endpoint, **@connection_options)
end

#proxy_client_for(proxy_endpoint, endpoint) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/async/http/faraday/adapter.rb', line 84

def proxy_client_for(proxy_endpoint, endpoint)
  key = [host_key(proxy_endpoint), host_key(endpoint)]
  
  @clients.fetch(key) do
    client = client_for(proxy_endpoint)
    @clients[key] = client.proxied_client(endpoint)
  end
end