From cee945bead8d6e95e05ccb661330cd5561caf7a3 Mon Sep 17 00:00:00 2001 From: Alex S Date: Tue, 20 Aug 2019 18:37:22 +0300 Subject: [PATCH] set adapter opts in connection --- lib/pleroma/http/connection.ex | 32 ++++++++++++++++++++++++++++---- lib/pleroma/http/http.ex | 28 ++++++---------------------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex index 69589bc5e..ef2ee918d 100644 --- a/lib/pleroma/http/connection.ex +++ b/lib/pleroma/http/connection.ex @@ -35,9 +35,33 @@ defmodule Pleroma.HTTP.Connection do adapter_options = Pleroma.Config.get([:http, :adapter], []) proxy_url = Pleroma.Config.get([:http, :proxy_url], nil) - @options - |> Keyword.merge(adapter_options) - |> Keyword.merge(options) - |> Keyword.merge(proxy: proxy_url) + options = + @options + |> Keyword.merge(adapter_options) + |> Keyword.merge(options) + |> Keyword.merge(proxy: proxy_url) + + pool = options[:pool] + url = options[:url] + + if not is_nil(url) and not is_nil(pool) and Pleroma.Gun.Connections.alive?(pool) do + get_conn_for_gun(url, options, pool) + else + options + end + end + + defp get_conn_for_gun(url, options, pool) do + case Pleroma.Gun.Connections.get_conn(url, options, pool) do + nil -> + options + + conn -> + %{host: host, port: port} = URI.parse(url) + + Keyword.put(options, :conn, conn) + |> Keyword.put(:close_conn, false) + |> Keyword.put(:original, "#{host}:#{port}") + end end end diff --git a/lib/pleroma/http/http.ex b/lib/pleroma/http/http.ex index 89afba720..da86ebcc7 100644 --- a/lib/pleroma/http/http.ex +++ b/lib/pleroma/http/http.ex @@ -34,11 +34,13 @@ defmodule Pleroma.HTTP do adapter_gun? = Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun - pool = options[:adapter][:pool] - options = - if adapter_gun? and not is_nil(pool) and Pleroma.Gun.Connections.alive?(pool) do - get_conn_for_gun(url, options, pool) + if adapter_gun? do + adapter_opts = + Keyword.get(options, :adapter, []) + |> Keyword.put(:url, url) + + Keyword.put(options, :adapter, adapter_opts) else options end @@ -63,24 +65,6 @@ defmodule Pleroma.HTTP do end end - defp get_conn_for_gun(url, options, pool) do - case Pleroma.Gun.Connections.get_conn(url, options, pool) do - nil -> - options - - conn -> - %{host: host, port: port} = URI.parse(url) - - adapter_opts = - Keyword.get(options, :adapter, []) - |> Keyword.put(:conn, conn) - |> Keyword.put(:close_conn, false) - |> Keyword.put(:original, "#{host}:#{port}") - - Keyword.put(options, :adapter, adapter_opts) - end - end - defp process_sni_options(options, nil), do: options defp process_sni_options(options, url) do