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.



35
36
37
38
39
40
41
42
43
# File 'lib/async/http/faraday/adapter.rb', line 35

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

Instance Method Details

#call(env) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/async/http/faraday/adapter.rb', line 85

def call(env)
	super
	
	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
			body = Body::Buffered.wrap(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



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

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

#closeObject



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

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



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

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

#make_client(endpoint) ⇒ Object



45
46
47
# File 'lib/async/http/faraday/adapter.rb', line 45

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

#proxy_client_for(proxy_endpoint, endpoint) ⇒ Object



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

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