From 3bc3e61dc5e7b6a10eee4c212dd113e6b2ca788a Mon Sep 17 00:00:00 2001 From: Alex S Date: Mon, 19 Aug 2019 17:43:06 +0300 Subject: [PATCH] some clean up and pleroma header --- config/config.exs | 4 ---- lib/pleroma/gun/api/gun.ex | 12 +++++++++--- lib/pleroma/gun/api/mock.ex | 24 ++++++++++++++---------- lib/pleroma/gun/connections.ex | 25 +++++-------------------- lib/pleroma/http/connection.ex | 3 ++- lib/pleroma/http/http.ex | 12 ++++++------ lib/pleroma/reverse_proxy/client/hackney.ex | 4 ++++ test/gun/connections_test.exs | 21 --------------------- test/reverse_proxy/client/hackney_test.exs | 4 ++++ test/reverse_proxy/client/tesla_test.exs | 4 ++++ test/support/reverse_proxy_client_case.ex | 4 ++++ 11 files changed, 52 insertions(+), 65 deletions(-) diff --git a/config/config.exs b/config/config.exs index e56b9730d..237c61ac9 100644 --- a/config/config.exs +++ b/config/config.exs @@ -562,10 +562,6 @@ config :pleroma, :gun_pools, upload: [ max_connections: 25, timeout: 300_000 - ], - default: [ - max_connections: 10, - timout: 20_000 ] # Import environment specific config. This must remain at the bottom diff --git a/lib/pleroma/gun/api/gun.ex b/lib/pleroma/gun/api/gun.ex index 33e7985a1..d97f5a7c9 100644 --- a/lib/pleroma/gun/api/gun.ex +++ b/lib/pleroma/gun/api/gun.ex @@ -1,6 +1,12 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Gun.API.Gun do @behaviour Pleroma.Gun.API + alias Pleroma.Gun.API + @gun_keys [ :connect_timeout, :http_opts, @@ -15,14 +21,14 @@ defmodule Pleroma.Gun.API.Gun do :ws_opts ] - @impl Pleroma.Gun.API + @impl API def open(host, port, opts) do :gun.open(host, port, Map.take(opts, @gun_keys)) end - @impl Pleroma.Gun.API + @impl API def info(pid), do: :gun.info(pid) - @impl Pleroma.Gun.API + @impl API def close(pid), do: :gun.close(pid) end diff --git a/lib/pleroma/gun/api/mock.ex b/lib/pleroma/gun/api/mock.ex index a80559f0b..b1a30a73c 100644 --- a/lib/pleroma/gun/api/mock.ex +++ b/lib/pleroma/gun/api/mock.ex @@ -4,12 +4,15 @@ defmodule Pleroma.Gun.API.Mock do @behaviour Pleroma.Gun.API - @impl Pleroma.Gun.API + + alias Pleroma.Gun.API + + @impl API def open(domain, 80, %{genserver_pid: genserver_pid}) when domain in ['another-domain.com', 'some-domain.com'] do {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end) - Registry.register(Pleroma.Gun.API.Mock, conn_pid, %{ + Registry.register(API.Mock, conn_pid, %{ origin_scheme: "http", origin_host: domain, origin_port: 80 @@ -19,10 +22,11 @@ defmodule Pleroma.Gun.API.Mock do {:ok, conn_pid} end + @impl API def open('some-domain.com', 443, %{genserver_pid: genserver_pid}) do {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end) - Registry.register(Pleroma.Gun.API.Mock, conn_pid, %{ + Registry.register(API.Mock, conn_pid, %{ origin_scheme: "https", origin_host: 'some-domain.com', origin_port: 443 @@ -32,11 +36,11 @@ defmodule Pleroma.Gun.API.Mock do {:ok, conn_pid} end - @impl Pleroma.Gun.API + @impl API def open('gun_down.com', 80, %{genserver_pid: genserver_pid}) do {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end) - Registry.register(Pleroma.Gun.API.Mock, conn_pid, %{ + Registry.register(API.Mock, conn_pid, %{ origin_scheme: "http", origin_host: 'gun_down.com', origin_port: 80 @@ -46,11 +50,11 @@ defmodule Pleroma.Gun.API.Mock do {:ok, conn_pid} end - @impl Pleroma.Gun.API + @impl API def open('gun_down_and_up.com', 80, %{genserver_pid: genserver_pid}) do {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end) - Registry.register(Pleroma.Gun.API.Mock, conn_pid, %{ + Registry.register(API.Mock, conn_pid, %{ origin_scheme: "http", origin_host: 'gun_down_and_up.com', origin_port: 80 @@ -68,12 +72,12 @@ defmodule Pleroma.Gun.API.Mock do {:ok, conn_pid} end - @impl Pleroma.Gun.API + @impl API def info(pid) do - [{_, info}] = Registry.lookup(Pleroma.Gun.API.Mock, pid) + [{_, info}] = Registry.lookup(API.Mock, pid) info end - @impl Pleroma.Gun.API + @impl API def close(_pid), do: :ok end diff --git a/lib/pleroma/gun/connections.ex b/lib/pleroma/gun/connections.ex index b6845e52d..361e8aaee 100644 --- a/lib/pleroma/gun/connections.ex +++ b/lib/pleroma/gun/connections.ex @@ -15,6 +15,8 @@ defmodule Pleroma.Gun.Connections do defstruct conns: %{}, opts: [] + alias Pleroma.Gun.API + @spec start_link({atom(), keyword()}) :: {:ok, pid()} | :ignore def start_link({name, opts}) do if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do @@ -44,21 +46,6 @@ 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 \\ :default), - 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 \\ :default) do pid = Process.whereis(name) @@ -91,7 +78,7 @@ defmodule Pleroma.Gun.Connections do else [{close_key, least_used} | _conns] = Enum.sort_by(state.conns, fn {_k, v} -> v.used end) - :ok = Pleroma.Gun.API.close(least_used.conn) + :ok = API.close(least_used.conn) state = put_in( @@ -128,12 +115,10 @@ defmodule Pleroma.Gun.Connections do end @impl true - # Do we need to do something with killed & unprocessed references? def handle_info({:gun_down, conn_pid, _protocol, _reason, _killed, _unprocessed}, state) do conn_key = compose_key_gun_info(conn_pid) {key, conn} = find_conn(state.conns, conn_pid, conn_key) - # We don't want to block requests to GenServer if gun send down message, return nil, so we can make some retries, while connection is not up Enum.each(conn.waiting_pids, fn waiting_pid -> GenServer.reply(waiting_pid, nil) end) state = put_in(state.conns[key].state, :down) @@ -143,7 +128,7 @@ defmodule Pleroma.Gun.Connections do defp compose_key(uri), do: "#{uri.scheme}:#{uri.host}:#{uri.port}" defp compose_key_gun_info(pid) do - info = Pleroma.Gun.API.info(pid) + info = API.info(pid) "#{info.origin_scheme}:#{info.origin_host}:#{info.origin_port}" end @@ -154,7 +139,7 @@ defmodule Pleroma.Gun.Connections do end defp open_conn(key, uri, from, state, opts) do - {:ok, conn} = Pleroma.Gun.API.open(to_charlist(uri.host), uri.port, opts) + {:ok, conn} = API.open(to_charlist(uri.host), uri.port, opts) state = put_in(state.conns[key], %Pleroma.Gun.Conn{ diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex index 6cb26c0fe..93e661e1b 100644 --- a/lib/pleroma/http/connection.ex +++ b/lib/pleroma/http/connection.ex @@ -9,7 +9,8 @@ defmodule Pleroma.HTTP.Connection do @options [ connect_timeout: 10_000, - timeout: 20_000 + timeout: 20_000, + pool: :federation ] @doc """ diff --git a/lib/pleroma/http/http.ex b/lib/pleroma/http/http.ex index 26ab0bb03..ab0fd55de 100644 --- a/lib/pleroma/http/http.ex +++ b/lib/pleroma/http/http.ex @@ -34,9 +34,11 @@ defmodule Pleroma.HTTP do adapter_gun? = Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun + pool = options[:adapter][:pool] + options = - if adapter_gun? and Pleroma.Gun.Connections.alive?() do - get_conn_for_gun(url, options) + if adapter_gun? and not is_nil(pool) and Pleroma.Gun.Connections.alive?(pool) do + get_conn_for_gun(url, options, pool) else options end @@ -61,10 +63,8 @@ defmodule Pleroma.HTTP do end end - defp get_conn_for_gun(url, options) do - pool = if options[:adapter][:pool], do: options[:adapter][:pool], else: :default - - case Pleroma.Gun.Connections.try_to_get_gun_conn(url, options, pool) do + defp get_conn_for_gun(url, options, pool) do + case Pleroma.Gun.Connections.get_conn(url, options, pool) do nil -> options diff --git a/lib/pleroma/reverse_proxy/client/hackney.ex b/lib/pleroma/reverse_proxy/client/hackney.ex index e6293646a..402c183af 100644 --- a/lib/pleroma/reverse_proxy/client/hackney.ex +++ b/lib/pleroma/reverse_proxy/client/hackney.ex @@ -1,3 +1,7 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.ReverseProxy.Client.Hackney do @behaviour Pleroma.ReverseProxy.Client diff --git a/test/gun/connections_test.exs b/test/gun/connections_test.exs index 8308b5f9f..4da42d854 100644 --- a/test/gun/connections_test.exs +++ b/test/gun/connections_test.exs @@ -31,27 +31,6 @@ defmodule Gun.ConnectionsTest do end end - test "try_to_get_gun_conn/1 returns conn", %{name: name, pid: pid} do - conn = Connections.try_to_get_gun_conn("http://some-domain.com", [genserver_pid: pid], name) - assert is_pid(conn) - assert Process.alive?(conn) - - reused_conn = Connections.get_conn("http://some-domain.com", [genserver_pid: pid], name) - - assert conn == reused_conn - - %Connections{ - conns: %{ - "http:some-domain.com:80" => %Conn{ - conn: ^conn, - state: :up, - waiting_pids: [], - used: 2 - } - } - } = Connections.get_state(name) - end - test "opens connection and reuse it on next request", %{name: name, pid: pid} do conn = Connections.get_conn("http://some-domain.com", [genserver_pid: pid], name) diff --git a/test/reverse_proxy/client/hackney_test.exs b/test/reverse_proxy/client/hackney_test.exs index 577e0b0b2..cf9de912a 100644 --- a/test/reverse_proxy/client/hackney_test.exs +++ b/test/reverse_proxy/client/hackney_test.exs @@ -1,3 +1,7 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.ReverseProxy.Client.HackneyTest do use Pleroma.ReverseProxyClientCase, client: Pleroma.ReverseProxy.Client.Hackney diff --git a/test/reverse_proxy/client/tesla_test.exs b/test/reverse_proxy/client/tesla_test.exs index 029a25d0f..a18d609d2 100644 --- a/test/reverse_proxy/client/tesla_test.exs +++ b/test/reverse_proxy/client/tesla_test.exs @@ -1,3 +1,7 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.ReverseProxy.Client.TeslaTest do use Pleroma.ReverseProxyClientCase, client: Pleroma.ReverseProxy.Client.Tesla diff --git a/test/support/reverse_proxy_client_case.ex b/test/support/reverse_proxy_client_case.ex index 40cd59ea2..16bc2803b 100644 --- a/test/support/reverse_proxy_client_case.ex +++ b/test/support/reverse_proxy_client_case.ex @@ -1,3 +1,7 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.ReverseProxyClientCase do defmacro __using__(client: client) do quote do