16
16
require 'async/http/client'
17
17
require 'async/http/proxy'
18
18
19
+ require_relative 'client_cache'
20
+
19
21
module Async
20
22
module HTTP
21
23
module Faraday
@@ -65,70 +67,17 @@ class Adapter < ::Faraday::Adapter
65
67
#
66
68
# @parameter timeout [Integer] The timeout for requests.
67
69
# @parameter options [Hash] Additional options to pass to the underlying Async::HTTP::Client.
68
- def initialize ( *arguments , timeout : nil , **options , &block )
69
- super ( *arguments , **options )
70
-
71
- @timeout = timeout
72
-
73
- @clients = { }
74
-
75
- @options = options
76
- end
77
-
78
- # Make a new client for the given endpoint.
79
- #
80
- # @parameter endpoint [IO::Endpoint::Generic] The endpoint to create the client for.
81
- def make_client ( endpoint )
82
- Client . new ( endpoint , **@connection_options )
83
- end
84
-
85
- # Get the host key for the given endpoint.
86
- #
87
- # This is used to cache clients for the same host.
88
- #
89
- # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the host key for.
90
- def host_key ( endpoint )
91
- url = endpoint . url . dup
92
-
93
- url . path = ""
94
- url . fragment = nil
95
- url . query = nil
96
-
97
- return url
98
- end
99
-
100
- # Get a client for the given endpoint. If a client already exists for the host, it will be reused.
101
- #
102
- # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for.
103
- def client_for ( endpoint )
104
- key = host_key ( endpoint )
105
-
106
- @clients . fetch ( key ) do
107
- @clients [ key ] = make_client ( endpoint )
108
- end
109
- end
110
-
111
- # Get a client for the given proxy endpoint and endpoint. If a client already exists for the host, it will be reused.
112
- #
113
- # @parameter proxy_endpoint [IO::Endpoint::Generic] The proxy endpoint to use.
114
- # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for.
115
- def proxy_client_for ( proxy_endpoint , endpoint )
116
- key = [ host_key ( proxy_endpoint ) , host_key ( endpoint ) ]
70
+ def initialize ( ...)
71
+ super
117
72
118
- @clients . fetch ( key ) do
119
- client = client_for ( proxy_endpoint )
120
- @clients [ key ] = client . proxied_client ( endpoint )
121
- end
73
+ @timeout = @connection_options . delete ( :timeout )
74
+ @clients = ClientCache . new ( **@connection_options )
122
75
end
123
76
124
77
# Close all clients.
125
78
def close
126
79
# The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
127
- clients = @clients . values
128
-
129
- @clients . clear
130
-
131
- clients . each ( &:close )
80
+ @clients . close
132
81
end
133
82
134
83
# Make a request using the adapter.
@@ -148,9 +97,9 @@ def call(env)
148
97
149
98
if proxy = env . request . proxy
150
99
proxy_endpoint = Endpoint . new ( proxy . uri )
151
- client = self . proxy_client_for ( proxy_endpoint , endpoint )
100
+ client = @clients . proxy_client_for ( proxy_endpoint , endpoint )
152
101
else
153
- client = self . client_for ( endpoint )
102
+ client = @clients . client_for ( endpoint )
154
103
end
155
104
156
105
if body = env . body
0 commit comments