From 3c3a79463c3b0c4c519e11e73b55056ee11a6a2d Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 13 Aug 2024 14:30:47 +1200 Subject: [PATCH] Fix persistent client cache. (#42) --- lib/async/http/faraday/clients.rb | 23 +++++++++------- test/async/http/faraday/clients.rb | 44 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 test/async/http/faraday/clients.rb diff --git a/lib/async/http/faraday/clients.rb b/lib/async/http/faraday/clients.rb index 2845cec..5eb8b08 100644 --- a/lib/async/http/faraday/clients.rb +++ b/lib/async/http/faraday/clients.rb @@ -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 @@ -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. @@ -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 @@ -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. diff --git a/test/async/http/faraday/clients.rb b/test/async/http/faraday/clients.rb new file mode 100644 index 0000000..064c9c1 --- /dev/null +++ b/test/async/http/faraday/clients.rb @@ -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