|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2018-2024, by Samuel Williams. |
| 5 | +# Copyright, 2018, by Andreas Garnaes. |
| 6 | +# Copyright, 2019, by Denis Talakevich. |
| 7 | +# Copyright, 2019-2020, by Igor Sidorov. |
| 8 | +# Copyright, 2023, by Genki Takiuchi. |
| 9 | +# Copyright, 2023, by Flavio Fernandes. |
| 10 | +# Copyright, 2024, by Jacob Frautschi. |
| 11 | + |
| 12 | +require 'faraday' |
| 13 | +require 'faraday/adapter' |
| 14 | +require 'kernel/sync' |
| 15 | + |
| 16 | +require 'async/http/client' |
| 17 | +require 'async/http/proxy' |
| 18 | + |
| 19 | +module Async |
| 20 | + module HTTP |
| 21 | + module Faraday |
| 22 | + class Clients |
| 23 | + def self.call(...) |
| 24 | + new(...) |
| 25 | + end |
| 26 | + |
| 27 | + def initialize(**options, &block) |
| 28 | + @options = options |
| 29 | + @block = block |
| 30 | + end |
| 31 | + |
| 32 | + def close |
| 33 | + end |
| 34 | + |
| 35 | + # Make a new client for the given endpoint. |
| 36 | + # |
| 37 | + # @parameter endpoint [IO::Endpoint::Generic] The endpoint to create the client for. |
| 38 | + def make_client(endpoint) |
| 39 | + client = Client.new(endpoint, **@options) |
| 40 | + @block&.call(client) |
| 41 | + return client |
| 42 | + end |
| 43 | + |
| 44 | + # Get a client for the given endpoint. If a client already exists for the host, it will be reused. |
| 45 | + # |
| 46 | + # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for. |
| 47 | + def with_client(endpoint) |
| 48 | + client = make_client(endpoint) |
| 49 | + |
| 50 | + yield client |
| 51 | + ensure |
| 52 | + client&.close |
| 53 | + end |
| 54 | + |
| 55 | + # Get a client for the given proxy endpoint and endpoint. If a client already exists for the host, it will be reused. |
| 56 | + # |
| 57 | + # @parameter proxy_endpoint [IO::Endpoint::Generic] The proxy endpoint to use. |
| 58 | + # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for. |
| 59 | + def with_proxied_client(proxy_endpoint, endpoint) |
| 60 | + client = client_for(proxy_endpoint) |
| 61 | + proxied_client = client.proxied_client(endpoint) |
| 62 | + |
| 63 | + yield proxied_client |
| 64 | + ensure |
| 65 | + proxied_client&.close |
| 66 | + client&.close |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + class PersistentClients < Clients |
| 71 | + def initialize(...) |
| 72 | + super |
| 73 | + |
| 74 | + @clients = {} |
| 75 | + end |
| 76 | + |
| 77 | + def close |
| 78 | + super |
| 79 | + |
| 80 | + clients = @clients.values |
| 81 | + @clients.clear |
| 82 | + |
| 83 | + clients.each(&:close) |
| 84 | + end |
| 85 | + |
| 86 | + # Get the host key for the given endpoint. |
| 87 | + # |
| 88 | + # This is used to cache clients for the same host. |
| 89 | + # |
| 90 | + # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the host key for. |
| 91 | + def host_key(endpoint) |
| 92 | + url = endpoint.url.dup |
| 93 | + |
| 94 | + url.path = "" |
| 95 | + url.fragment = nil |
| 96 | + url.query = nil |
| 97 | + |
| 98 | + return url |
| 99 | + end |
| 100 | + |
| 101 | + # Get a client for the given endpoint. If a client already exists for the host, it will be reused. |
| 102 | + # |
| 103 | + # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for. |
| 104 | + def client_for(endpoint) |
| 105 | + key = host_key(endpoint) |
| 106 | + |
| 107 | + fetch(key) do |
| 108 | + make_client |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + def with_client(endpoint) |
| 113 | + yield make_client(endpoint) |
| 114 | + end |
| 115 | + |
| 116 | + # Get a client for the given proxy endpoint and endpoint. If a client already exists for the host, it will be reused. |
| 117 | + # |
| 118 | + # @parameter proxy_endpoint [IO::Endpoint::Generic] The proxy endpoint to use. |
| 119 | + # @parameter endpoint [IO::Endpoint::Generic] The endpoint to get the client for. |
| 120 | + def with_proxied_client(proxy_endpoint, endpoint) |
| 121 | + key = [host_key(proxy_endpoint), host_key(endpoint)] |
| 122 | + |
| 123 | + proxied_client = fetch(key) do |
| 124 | + client_for(proxy_endpoint).proxied_client(endpoint) |
| 125 | + end |
| 126 | + |
| 127 | + yield proxied_client |
| 128 | + end |
| 129 | + |
| 130 | + protected |
| 131 | + |
| 132 | + def fetch(key) |
| 133 | + @clients.fetch(key) do |
| 134 | + @clients[key] = yield |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + class PerThreadPersistentClients |
| 140 | + def initialize(**options, &block) |
| 141 | + @options = options |
| 142 | + @block = block |
| 143 | + |
| 144 | + @key = :"#{self.class}_#{object_id}" |
| 145 | + end |
| 146 | + |
| 147 | + def with_client(endpoint, &block) |
| 148 | + clients.with_client(endpoint, &block) |
| 149 | + end |
| 150 | + |
| 151 | + def with_proxied_client(proxy_endpoint, endpoint, &block) |
| 152 | + clients.with_proxied_client(proxy_endpoint, endpoint, &block) |
| 153 | + end |
| 154 | + |
| 155 | + def close |
| 156 | + Thread.list.each do |thread| |
| 157 | + if clients = thread[@key] |
| 158 | + clients.close |
| 159 | + |
| 160 | + thread[@key] = nil |
| 161 | + end |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + protected |
| 166 | + |
| 167 | + def clients |
| 168 | + Thread.current[@key] ||= PersistentClients.new(**@options, &@block) |
| 169 | + end |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | +end |
0 commit comments