-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclients.rb
More file actions
202 lines (164 loc) · 5.48 KB
/
Copy pathclients.rb
File metadata and controls
202 lines (164 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2024-2025, by Samuel Williams.
require "faraday"
require "faraday/adapter"
require "kernel/sync"
require "async/http/client"
require "async/http/proxy"
module Async
module HTTP
module Faraday
# An interface for creating and managing HTTP clients.
class Clients
# Create a new instance of the class.
def self.call(...)
new(...)
end
# Create a new interface for managing HTTP clients.
#
# @parameter options [Hash] The options to create the clients with.
# @parameter block [Proc] An optional block to call with the client before it is used.
def initialize(**options, &block)
@options = options
@block = block
end
# Close all clients.
def close
end
# Make a new client for the given endpoint.
#
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to create the client for.
def make_client(endpoint)
client = Client.new(endpoint, **@options)
return @block&.call(client) || client
end
# Get a client for the given endpoint.
#
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for.
# @yields {|client| ...} A client for the given endpoint.
def with_client(endpoint)
client = make_client(endpoint)
yield client
ensure
client&.close
end
# Get a client for the given proxy endpoint and endpoint.
#
# @parameter proxy_endpoint [IO::Endpoint::Generic] The proxy endpoint to use.
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for.
# @yields {|client| ...} A client for the given endpoint.
def with_proxied_client(proxy_endpoint, endpoint)
client = make_client(proxy_endpoint)
proxied_client = client.proxied_client(endpoint)
yield proxied_client
ensure
proxied_client&.close
client&.close
end
end
# An interface for creating and managing persistent HTTP clients.
class PersistentClients < Clients
# Create a new instance of the class.
def initialize(...)
super
@clients = {}
end
# Close all clients.
def close
super
clients = @clients.values
@clients.clear
clients.reverse_each(&:close)
end
# Lookup or create a client for the given endpoint.
#
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to create the client for.
def make_client(endpoint)
key = host_key(endpoint)
fetch(key) do
super
end
end
# Get a client for the given endpoint. If a client already exists for the host, it will be reused.
#
# @yields {|client| ...} A client for the given endpoint.
def with_client(endpoint)
yield make_client(endpoint)
end
# Get a client for the given proxy endpoint and endpoint. If a client already exists for the host, it will be reused.
#
# @parameter proxy_endpoint [IO::Endpoint::Generic] The proxy endpoint to use.
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for.
def with_proxied_client(proxy_endpoint, endpoint)
key = [host_key(proxy_endpoint), host_key(endpoint)]
proxied_client = fetch(key) do
make_client(proxy_endpoint).proxied_client(endpoint)
end
yield proxied_client
end
private
def fetch(key)
@clients.fetch(key) do
@clients[key] = yield
end
end
def host_key(endpoint)
url = endpoint.url.dup
url.path = ""
url.fragment = nil
url.query = nil
return url
end
end
# An interface for creating and managing per-thread persistent HTTP clients.
class PerThreadPersistentClients
# Create a new instance of the class.
#
# @parameter options [Hash] The options to create the clients with.
# @parameter block [Proc] An optional block to call with the client before it is used.
def initialize(**options, &block)
@options = options
@block = block
@key = :"#{self.class}_#{object_id}"
end
# Get a client for the given endpoint. If a client already exists for the host, it will be reused.
#
# The client instance will be will be cached per-thread.
#
# @yields {|client| ...} A client for the given endpoint.
def with_client(endpoint, &block)
clients.with_client(endpoint, &block)
end
# Get a client for the given proxy endpoint and endpoint. If a client already exists for the host, it will be reused.
#
# The client instance will be will be cached per-thread.
#
# @parameter proxy_endpoint [IO::Endpoint::Generic] The proxy endpoint to use.
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for.
def with_proxied_client(proxy_endpoint, endpoint, &block)
clients.with_proxied_client(proxy_endpoint, endpoint, &block)
end
# Close all clients.
#
# This will close all clients associated with all threads.
def close
Thread.list.each do |thread|
if clients = thread.thread_variable_get(@key)
thread.thread_variable_set(@key, nil)
clients.close
end
end
end
private
def make_clients
PersistentClients.new(**@options, &@block)
end
def clients
thread = Thread.current
return thread.thread_variable_get(@key) || thread.thread_variable_set(@key, make_clients)
end
end
end
end
end