Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix persistent client cache. #42

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions lib/async/http/faraday/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def with_client(endpoint)
# @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 = client_for(proxy_endpoint)
client = make_client(proxy_endpoint)
proxied_client = client.proxied_client(endpoint)

yield proxied_client
Expand Down Expand Up @@ -89,6 +89,17 @@ def close
clients.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.
Expand All @@ -104,7 +115,7 @@ def with_proxied_client(proxy_endpoint, endpoint)
key = [host_key(proxy_endpoint), host_key(endpoint)]

proxied_client = fetch(key) do
client_for(proxy_endpoint).proxied_client(endpoint)
make_client(proxy_endpoint).proxied_client(endpoint)
end

yield proxied_client
Expand All @@ -127,14 +138,6 @@ def host_key(endpoint)

return url
end

def client_for(endpoint)
key = host_key(endpoint)

fetch(key) do
make_client
end
end
end

# An interface for creating and managing per-thread persistent HTTP clients.
Expand Down
44 changes: 44 additions & 0 deletions test/async/http/faraday/clients.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'async/http/faraday/clients'

describe Async::HTTP::Faraday::PersistentClients do
let(:clients) {subject.new}

with "#make_client" do
it "caches the client" do
endpoint = Async::HTTP::Endpoint.parse('http://example.com')
client = clients.make_client(endpoint)

expect(clients.make_client(endpoint)).to be_equal(client)
end
end

with "#with_client" do
it "caches the client" do
endpoint = Async::HTTP::Endpoint.parse('http://example.com')

clients.with_client(endpoint) do |client|
clients.with_client(endpoint) do |other|
expect(other).to be_equal(client)
end
end
end
end

with "#with_proxied_client" do
it "caches the client" do
endpoint = Async::HTTP::Endpoint.parse('http://example.com')
proxy_endpoint = Async::HTTP::Endpoint.parse('http://proxy.example.com')

clients.with_proxied_client(proxy_endpoint, endpoint) do |client|
clients.with_proxied_client(proxy_endpoint, endpoint) do |other|
expect(other).to be_equal(client)
end
end
end
end
end
Loading