Skip to content

Commit

Permalink
Fix persistent client cache. (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Aug 13, 2024
1 parent 38ec627 commit 3c3a794
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
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

0 comments on commit 3c3a794

Please sign in to comment.