async-http-faraday 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/async/http/faraday/adapter.rb +69 -8
- data/lib/async/http/faraday/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 461ee3d89afc1d0c8077b02f2bddfa4e9fd91e311fa810cb005aea26c0135b57
|
4
|
+
data.tar.gz: fc58f167759eaeadfe849ea631986a2f112bc959ec72ea2b7b9703b5126baf8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 421f27bf1a380003fee2c76ba501a74984e01abc7a380bb5bd8f75452bbf2fa5cc3d25a135bd47813e8248ecff92d625f285e078e04c335c4df21831726fe324
|
7
|
+
data.tar.gz: 30993cd6afe2bd13f20178d8ca1b1560a47a20cc68f2e4bfbb456a4f59b5c55590d8a1319db0210abdca3c3dabc1a703867ac550d0f801efec3c7c3397039c07
|
@@ -23,7 +23,9 @@
|
|
23
23
|
require 'faraday'
|
24
24
|
require 'faraday/adapter'
|
25
25
|
require 'kernel/sync'
|
26
|
-
|
26
|
+
|
27
|
+
require 'async/http/client'
|
28
|
+
require 'async/http/proxy'
|
27
29
|
|
28
30
|
require_relative 'agent'
|
29
31
|
|
@@ -44,24 +46,83 @@ module Async
|
|
44
46
|
SocketError
|
45
47
|
].freeze
|
46
48
|
|
47
|
-
def initialize(*arguments, **options, &block)
|
48
|
-
super
|
49
|
+
def initialize(*arguments, timeout: nil, **options, &block)
|
50
|
+
super(*arguments, **options)
|
51
|
+
|
52
|
+
@timeout = timeout
|
53
|
+
|
54
|
+
@clients = {}
|
55
|
+
|
56
|
+
@options = options
|
57
|
+
end
|
58
|
+
|
59
|
+
def make_client(endpoint)
|
60
|
+
Client.new(endpoint, **@connection_options)
|
61
|
+
end
|
62
|
+
|
63
|
+
def host_key(endpoint)
|
64
|
+
url = endpoint.url.dup
|
49
65
|
|
50
|
-
|
51
|
-
|
52
|
-
|
66
|
+
url.path = ""
|
67
|
+
url.fragment = nil
|
68
|
+
url.query = nil
|
69
|
+
|
70
|
+
return url
|
71
|
+
end
|
72
|
+
|
73
|
+
def client_for(endpoint)
|
74
|
+
key = host_key(endpoint)
|
75
|
+
|
76
|
+
@clients.fetch(key) do
|
77
|
+
@clients[key] = make_client(endpoint)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def proxy_client_for(proxy_endpoint, endpoint)
|
82
|
+
key = [host_key(proxy_endpoint), host_key(endpoint)]
|
83
|
+
|
84
|
+
@clients.fetch(key) do
|
85
|
+
client = client_for(proxy_endpoint)
|
86
|
+
@clients[key] = client.proxied_client(endpoint)
|
87
|
+
end
|
53
88
|
end
|
54
89
|
|
55
90
|
def close
|
56
|
-
|
91
|
+
# The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
|
92
|
+
clients = @clients.values
|
93
|
+
|
94
|
+
@clients.clear
|
95
|
+
|
96
|
+
clients.each(&:close)
|
57
97
|
end
|
58
98
|
|
59
99
|
def call(env)
|
60
100
|
super
|
61
101
|
|
62
102
|
Sync do
|
103
|
+
endpoint = Endpoint.new(env.url)
|
104
|
+
|
105
|
+
if proxy = env.request.proxy
|
106
|
+
proxy_endpoint = Endpoint.new(proxy.uri)
|
107
|
+
client = self.proxy_client_for(proxy_endpoint, endpoint)
|
108
|
+
else
|
109
|
+
client = self.client_for(endpoint)
|
110
|
+
end
|
111
|
+
|
112
|
+
if body = env.body
|
113
|
+
body = Body::Buffered.wrap(body)
|
114
|
+
end
|
115
|
+
|
116
|
+
if headers = env.request_headers
|
117
|
+
headers = ::Protocol::HTTP::Headers[headers]
|
118
|
+
end
|
119
|
+
|
120
|
+
method = env.method.upcase
|
121
|
+
|
122
|
+
request = ::Protocol::HTTP::Request.new(endpoint.scheme, endpoint.authority, method, endpoint.path, nil, headers, body)
|
123
|
+
|
63
124
|
with_timeout do
|
64
|
-
response =
|
125
|
+
response = client.call(request)
|
65
126
|
|
66
127
|
save_response(env, response.status, response.read, response.headers)
|
67
128
|
end
|