diff options
author | Alex S <alex.strizhakov@gmail.com> | 2019-08-14 13:37:27 +0300 |
---|---|---|
committer | Ariadne Conill <ariadne@dereferenced.org> | 2019-08-18 22:34:13 +0000 |
commit | aa0ab31b5ba24f9d482847193a309037907bb71d (patch) | |
tree | 0fd55bf4cd3ec33907fd369bb96accaeacb04f76 /lib | |
parent | 246906165776874ca7e4064b197ddf1237af2d1c (diff) | |
download | pleroma-aa0ab31b5ba24f9d482847193a309037907bb71d.tar.gz |
added gun connections holder genserver
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/application.ex | 3 | ||||
-rw-r--r-- | lib/pleroma/gun/connections.ex | 25 | ||||
-rw-r--r-- | lib/pleroma/http/http.ex | 23 |
3 files changed, 48 insertions, 3 deletions
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index 25e56b9e2..f755a1355 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -35,7 +35,8 @@ defmodule Pleroma.Application do Pleroma.Emoji, Pleroma.Captcha, Pleroma.FlakeId, - Pleroma.ScheduledActivityWorker + Pleroma.ScheduledActivityWorker, + Pleroma.Gun.Connections ] ++ cachex_children() ++ hackney_pool_children() ++ diff --git a/lib/pleroma/gun/connections.ex b/lib/pleroma/gun/connections.ex index 60ec68d89..d5a9d8607 100644 --- a/lib/pleroma/gun/connections.ex +++ b/lib/pleroma/gun/connections.ex @@ -26,8 +26,8 @@ defmodule Pleroma.Gun.Connections do {:ok, %__MODULE__{conns: %{}}} end - @spec get_conn(atom(), String.t(), keyword()) :: pid() - def get_conn(name \\ __MODULE__, url, opts \\ []) do + @spec get_conn(String.t(), keyword(), atom()) :: pid() + def get_conn(url, opts \\ [], name \\ __MODULE__) do opts = Enum.into(opts, %{}) uri = URI.parse(url) @@ -39,6 +39,27 @@ defmodule Pleroma.Gun.Connections do ) end + # TODO: only for testing, add this parameter to the config + @spec try_to_get_gun_conn(String.t(), keyword(), atom()) :: nil | pid() + def try_to_get_gun_conn(url, opts \\ [], name \\ __MODULE__), + do: try_to_get_gun_conn(url, opts, name, 0) + + @spec try_to_get_gun_conn(String.t(), keyword(), atom(), pos_integer()) :: nil | pid() + def try_to_get_gun_conn(_url, _, _, 3), do: nil + + def try_to_get_gun_conn(url, opts, name, acc) do + case Pleroma.Gun.Connections.get_conn(url, opts, name) do + nil -> try_to_get_gun_conn(url, acc + 1) + conn -> conn + end + end + + @spec alive?(atom()) :: boolean() + def alive?(name \\ __MODULE__) do + pid = Process.whereis(name) + if pid, do: Process.alive?(pid), else: false + end + @spec get_state(atom()) :: t() def get_state(name \\ __MODULE__) do GenServer.call(name, {:state}) diff --git a/lib/pleroma/http/http.ex b/lib/pleroma/http/http.ex index 6d7934841..1846749c0 100644 --- a/lib/pleroma/http/http.ex +++ b/lib/pleroma/http/http.ex @@ -32,6 +32,15 @@ defmodule Pleroma.HTTP do process_request_options(options) |> process_sni_options(url) + adapter_gun? = Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun + + options = + if adapter_gun? and Pleroma.Gun.Connections.alive?() do + get_conn_for_gun(url, options) + else + options + end + params = Keyword.get(options, :params, []) %{} @@ -52,6 +61,20 @@ defmodule Pleroma.HTTP do end end + defp get_conn_for_gun(url, options) do + case Pleroma.Gun.Connections.try_to_get_gun_conn(url) do + nil -> + options + + conn -> + adapter_opts = + Keyword.get(options, :adapter, []) + |> Keyword.put(:conn, conn) + + Keyword.put(options, :adapter, adapter_opts) + end + end + defp process_sni_options(options, nil), do: options defp process_sni_options(options, url) do |