{:ok, Env.t()} | {:error, any()}
def request(method, url, body, headers, options) when is_binary(url) do
uri = URI.parse(url)
- adapter_opts = AdapterHelper.options(uri, options[:adapter] || [])
+ adapter_opts = AdapterHelper.options(uri, options || [])
- case AdapterHelper.get_conn(uri, adapter_opts) do
- {:ok, adapter_opts} ->
- options = put_in(options[:adapter], adapter_opts)
- params = options[:params] || []
- request = build_request(method, headers, options, url, body, params)
+ options = put_in(options[:adapter], adapter_opts)
+ params = options[:params] || []
+ request = build_request(method, headers, options, url, body, params)
- adapter = Application.get_env(:tesla, :adapter)
- client = Tesla.client([Pleroma.HTTP.Middleware.FollowRedirects], adapter)
+ adapter = Application.get_env(:tesla, :adapter)
- request(client, request)
+ client = Tesla.client(adapter_middlewares(adapter), adapter)
- # Connection release is handled in a custom FollowRedirects middleware
- err ->
- err
- end
+ maybe_limit(
+ fn ->
+ request(client, request)
+ end,
+ adapter,
+ adapter_opts
+ )
end
@spec request(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}
|> Builder.add_param(:query, :query, params)
|> Builder.convert_to_keyword()
end
+
+ @prefix Pleroma.Gun.ConnectionPool
+ defp maybe_limit(fun, Tesla.Adapter.Gun, opts) do
+ ConcurrentLimiter.limit(:"#{@prefix}.#{opts[:pool] || :default}", fun)
+ end
+
+ defp maybe_limit(fun, _, _) do
+ fun.()
+ end
+
+ defp adapter_middlewares(Tesla.Adapter.Gun) do
+ [Tesla.Middleware.FollowRedirects, Pleroma.Tesla.Middleware.ConnectionPool]
+ end
+
+ defp adapter_middlewares(_), do: []
end