Skip to content

Commit 3c3a794

Browse files
authored
Fix persistent client cache. (#42)
1 parent 38ec627 commit 3c3a794

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

lib/async/http/faraday/clients.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def with_client(endpoint)
6060
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for.
6161
# @yields {|client| ...} A client for the given endpoint.
6262
def with_proxied_client(proxy_endpoint, endpoint)
63-
client = client_for(proxy_endpoint)
63+
client = make_client(proxy_endpoint)
6464
proxied_client = client.proxied_client(endpoint)
6565

6666
yield proxied_client
@@ -89,6 +89,17 @@ def close
8989
clients.each(&:close)
9090
end
9191

92+
# Lookup or create a client for the given endpoint.
93+
#
94+
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to create the client for.
95+
def make_client(endpoint)
96+
key = host_key(endpoint)
97+
98+
fetch(key) do
99+
super
100+
end
101+
end
102+
92103
# Get a client for the given endpoint. If a client already exists for the host, it will be reused.
93104
#
94105
# @yields {|client| ...} A client for the given endpoint.
@@ -104,7 +115,7 @@ def with_proxied_client(proxy_endpoint, endpoint)
104115
key = [host_key(proxy_endpoint), host_key(endpoint)]
105116

106117
proxied_client = fetch(key) do
107-
client_for(proxy_endpoint).proxied_client(endpoint)
118+
make_client(proxy_endpoint).proxied_client(endpoint)
108119
end
109120

110121
yield proxied_client
@@ -127,14 +138,6 @@ def host_key(endpoint)
127138

128139
return url
129140
end
130-
131-
def client_for(endpoint)
132-
key = host_key(endpoint)
133-
134-
fetch(key) do
135-
make_client
136-
end
137-
end
138141
end
139142

140143
# An interface for creating and managing per-thread persistent HTTP clients.

test/async/http/faraday/clients.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
require 'async/http/faraday/clients'
7+
8+
describe Async::HTTP::Faraday::PersistentClients do
9+
let(:clients) {subject.new}
10+
11+
with "#make_client" do
12+
it "caches the client" do
13+
endpoint = Async::HTTP::Endpoint.parse('http://example.com')
14+
client = clients.make_client(endpoint)
15+
16+
expect(clients.make_client(endpoint)).to be_equal(client)
17+
end
18+
end
19+
20+
with "#with_client" do
21+
it "caches the client" do
22+
endpoint = Async::HTTP::Endpoint.parse('http://example.com')
23+
24+
clients.with_client(endpoint) do |client|
25+
clients.with_client(endpoint) do |other|
26+
expect(other).to be_equal(client)
27+
end
28+
end
29+
end
30+
end
31+
32+
with "#with_proxied_client" do
33+
it "caches the client" do
34+
endpoint = Async::HTTP::Endpoint.parse('http://example.com')
35+
proxy_endpoint = Async::HTTP::Endpoint.parse('http://proxy.example.com')
36+
37+
clients.with_proxied_client(proxy_endpoint, endpoint) do |client|
38+
clients.with_proxied_client(proxy_endpoint, endpoint) do |other|
39+
expect(other).to be_equal(client)
40+
end
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)