refactoring for gun api modules

This commit is contained in:
Alexander Strizhakov 2020-03-03 19:24:14 +03:00
parent 23f407bf09
commit 884d9710b2
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
12 changed files with 104 additions and 107 deletions

View file

@ -90,7 +90,7 @@
config :pleroma, :modules, runtime_dir: "test/fixtures/modules" config :pleroma, :modules, runtime_dir: "test/fixtures/modules"
config :pleroma, Pleroma.Gun.API, Pleroma.Gun.API.Mock config :pleroma, Pleroma.Gun, Pleroma.GunMock
config :pleroma, Pleroma.Emails.NewUsersDigestEmail, enabled: true config :pleroma, Pleroma.Emails.NewUsersDigestEmail, enabled: true

View file

@ -3,27 +3,43 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Gun.API do defmodule Pleroma.Gun.API do
@callback open(charlist(), pos_integer(), map()) :: {:ok, pid()} @behaviour Pleroma.Gun
@callback info(pid()) :: map()
@callback close(pid()) :: :ok
@callback await_up(pid, pos_integer()) :: {:ok, atom()} | {:error, atom()}
@callback connect(pid(), map()) :: reference()
@callback await(pid(), reference()) :: {:response, :fin, 200, []}
@callback set_owner(pid(), pid()) :: :ok
def open(host, port, opts), do: api().open(host, port, opts) alias Pleroma.Gun
def info(pid), do: api().info(pid) @gun_keys [
:connect_timeout,
:http_opts,
:http2_opts,
:protocols,
:retry,
:retry_timeout,
:trace,
:transport,
:tls_opts,
:tcp_opts,
:socks_opts,
:ws_opts
]
def close(pid), do: api().close(pid) @impl Gun
def open(host, port, opts \\ %{}), do: :gun.open(host, port, Map.take(opts, @gun_keys))
def await_up(pid, timeout \\ 5_000), do: api().await_up(pid, timeout) @impl Gun
defdelegate info(pid), to: :gun
def connect(pid, opts), do: api().connect(pid, opts) @impl Gun
defdelegate close(pid), to: :gun
def await(pid, ref), do: api().await(pid, ref) @impl Gun
defdelegate await_up(pid, timeout \\ 5_000), to: :gun
def set_owner(pid, owner), do: api().set_owner(pid, owner) @impl Gun
defdelegate connect(pid, opts), to: :gun
defp api, do: Pleroma.Config.get([Pleroma.Gun.API], Pleroma.Gun) @impl Gun
defdelegate await(pid, ref), to: :gun
@impl Gun
defdelegate set_owner(pid, owner), to: :gun
end end

View file

@ -6,7 +6,7 @@ defmodule Pleroma.Gun.Conn do
@moduledoc """ @moduledoc """
Struct for gun connection data Struct for gun connection data
""" """
alias Pleroma.Gun.API alias Pleroma.Gun
alias Pleroma.Pool.Connections alias Pleroma.Pool.Connections
require Logger require Logger
@ -65,7 +65,7 @@ def open(%URI{} = uri, name, opts) do
last_reference: :os.system_time(:second) last_reference: :os.system_time(:second)
} }
:ok = API.set_owner(conn_pid, Process.whereis(name)) :ok = Gun.set_owner(conn_pid, Process.whereis(name))
Connections.add_conn(name, key, conn) Connections.add_conn(name, key, conn)
end end
end end
@ -77,10 +77,10 @@ defp do_open(uri, %{proxy: {proxy_host, proxy_port}} = opts) do
|> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, [])) |> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
with open_opts <- Map.delete(opts, :tls_opts), with open_opts <- Map.delete(opts, :tls_opts),
{:ok, conn} <- API.open(proxy_host, proxy_port, open_opts), {:ok, conn} <- Gun.open(proxy_host, proxy_port, open_opts),
{:ok, _} <- API.await_up(conn, opts[:await_up_timeout]), {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]),
stream <- API.connect(conn, connect_opts), stream <- Gun.connect(conn, connect_opts),
{:response, :fin, 200, _} <- API.await(conn, stream) do {:response, :fin, 200, _} <- Gun.await(conn, stream) do
conn conn
else else
error -> error ->
@ -115,8 +115,8 @@ defp do_open(uri, %{proxy: {proxy_type, proxy_host, proxy_port}} = opts) do
|> Map.put(:protocols, [:socks]) |> Map.put(:protocols, [:socks])
|> Map.put(:socks_opts, socks_opts) |> Map.put(:socks_opts, socks_opts)
with {:ok, conn} <- API.open(proxy_host, proxy_port, opts), with {:ok, conn} <- Gun.open(proxy_host, proxy_port, opts),
{:ok, _} <- API.await_up(conn, opts[:await_up_timeout]) do {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]) do
conn conn
else else
error -> error ->
@ -133,8 +133,8 @@ defp do_open(uri, %{proxy: {proxy_type, proxy_host, proxy_port}} = opts) do
defp do_open(%URI{host: host, port: port} = uri, opts) do defp do_open(%URI{host: host, port: port} = uri, opts) do
host = Pleroma.HTTP.Connection.parse_host(host) host = Pleroma.HTTP.Connection.parse_host(host)
with {:ok, conn} <- API.open(host, port, opts), with {:ok, conn} <- Gun.open(host, port, opts),
{:ok, _} <- API.await_up(conn, opts[:await_up_timeout]) do {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]) do
conn conn
else else
error -> error ->
@ -164,7 +164,7 @@ defp close_least_used_and_do_open(name, uri, opts) do
with [{close_key, least_used} | _conns] <- with [{close_key, least_used} | _conns] <-
Connections.get_unused_conns(name), Connections.get_unused_conns(name),
:ok <- Pleroma.Gun.API.close(least_used.conn) do :ok <- Gun.close(least_used.conn) do
Connections.remove_conn(name, close_key) Connections.remove_conn(name, close_key)
do_open(uri, opts) do_open(uri, opts)

View file

@ -3,46 +3,27 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Gun do defmodule Pleroma.Gun do
@behaviour Pleroma.Gun.API @callback open(charlist(), pos_integer(), map()) :: {:ok, pid()}
@callback info(pid()) :: map()
@callback close(pid()) :: :ok
@callback await_up(pid, pos_integer()) :: {:ok, atom()} | {:error, atom()}
@callback connect(pid(), map()) :: reference()
@callback await(pid(), reference()) :: {:response, :fin, 200, []}
@callback set_owner(pid(), pid()) :: :ok
alias Pleroma.Gun.API def open(host, port, opts), do: api().open(host, port, opts)
@gun_keys [ def info(pid), do: api().info(pid)
:connect_timeout,
:http_opts,
:http2_opts,
:protocols,
:retry,
:retry_timeout,
:trace,
:transport,
:tls_opts,
:tcp_opts,
:socks_opts,
:ws_opts
]
@impl API def close(pid), do: api().close(pid)
def open(host, port, opts \\ %{}), do: :gun.open(host, port, Map.take(opts, @gun_keys))
@impl API def await_up(pid, timeout \\ 5_000), do: api().await_up(pid, timeout)
defdelegate info(pid), to: :gun
@impl API def connect(pid, opts), do: api().connect(pid, opts)
defdelegate close(pid), to: :gun
@impl API def await(pid, ref), do: api().await(pid, ref)
defdelegate await_up(pid, timeout \\ 5_000), to: :gun
@impl API def set_owner(pid, owner), do: api().set_owner(pid, owner)
defdelegate connect(pid, opts), to: :gun
@impl API defp api, do: Pleroma.Config.get([Pleroma.Gun], Pleroma.Gun.API)
defdelegate await(pid, ref), to: :gun
@spec flush(pid() | reference()) :: :ok
defdelegate flush(pid), to: :gun
@impl API
defdelegate set_owner(pid, owner), to: :gun
end end

View file

@ -19,7 +19,7 @@ defmodule Pleroma.Pool.Connections do
defstruct conns: %{}, opts: [] defstruct conns: %{}, opts: []
alias Pleroma.Gun.API alias Pleroma.Gun
@spec start_link({atom(), keyword()}) :: {:ok, pid()} @spec start_link({atom(), keyword()}) :: {:ok, pid()}
def start_link({name, opts}) do def start_link({name, opts}) do
@ -209,7 +209,7 @@ def handle_info({:gun_up, conn_pid, _protocol}, state) do
nil -> nil ->
Logger.debug(":gun_up message for conn which is not found in state") Logger.debug(":gun_up message for conn which is not found in state")
:ok = API.close(conn_pid) :ok = Gun.close(conn_pid)
state state
end end
@ -226,7 +226,7 @@ def handle_info({:gun_down, conn_pid, _protocol, _reason, _killed}, state) do
{true, key} <- {Process.alive?(conn_pid), key} do {true, key} <- {Process.alive?(conn_pid), key} do
if conn.retries == retries do if conn.retries == retries do
Logger.debug("closing conn if retries is eq #{inspect(conn_pid)}") Logger.debug("closing conn if retries is eq #{inspect(conn_pid)}")
:ok = API.close(conn.conn) :ok = Gun.close(conn.conn)
put_in( put_in(
state.conns, state.conns,
@ -252,7 +252,7 @@ def handle_info({:gun_down, conn_pid, _protocol, _reason, _killed}, state) do
nil -> nil ->
Logger.debug(":gun_down message for conn which is not found in state") Logger.debug(":gun_down message for conn which is not found in state")
:ok = API.close(conn_pid) :ok = Gun.close(conn_pid)
state state
end end
@ -287,7 +287,7 @@ def handle_info({:DOWN, _ref, :process, conn_pid, reason}, state) do
defp compose_key_gun_info(pid) do defp compose_key_gun_info(pid) do
try do try do
# sometimes :gun.info can raise MatchError, which lead to pool terminate # sometimes :gun.info can raise MatchError, which lead to pool terminate
%{origin_host: origin_host, origin_scheme: scheme, origin_port: port} = API.info(pid) %{origin_host: origin_host, origin_scheme: scheme, origin_port: port} = Gun.info(pid)
host = host =
case :inet.ntoa(origin_host) do case :inet.ntoa(origin_host) do

View file

@ -12,7 +12,7 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
alias Pleroma.Pool.Connections alias Pleroma.Pool.Connections
setup_all do setup_all do
{:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.Gun.API.Mock) {:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.GunMock)
:ok :ok
end end

View file

@ -10,7 +10,7 @@ defmodule Pleroma.HTTP.ConnectionTest do
alias Pleroma.HTTP.Connection alias Pleroma.HTTP.Connection
setup_all do setup_all do
{:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.Gun.API.Mock) {:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.GunMock)
:ok :ok
end end

View file

@ -61,8 +61,8 @@ test "returns successfully result" do
describe "connection pools" do describe "connection pools" do
@describetag :integration @describetag :integration
clear_config(Pleroma.Gun.API) do clear_config(Pleroma.Gun) do
Pleroma.Config.put(Pleroma.Gun.API, Pleroma.Gun) Pleroma.Config.put(Pleroma.Gun, Pleroma.Gun.API)
end end
test "gun" do test "gun" do

View file

@ -6,12 +6,11 @@ defmodule Pleroma.Pool.ConnectionsTest do
use ExUnit.Case use ExUnit.Case
use Pleroma.Tests.Helpers use Pleroma.Tests.Helpers
import ExUnit.CaptureLog import ExUnit.CaptureLog
alias Pleroma.Gun.API
alias Pleroma.Gun.Conn alias Pleroma.Gun.Conn
alias Pleroma.Pool.Connections alias Pleroma.Pool.Connections
setup_all do setup_all do
{:ok, _} = Registry.start_link(keys: :unique, name: API.Mock) {:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.GunMock)
:ok :ok
end end
@ -439,8 +438,8 @@ test "remove frequently used and idle", %{name: name} do
describe "integration test" do describe "integration test" do
@describetag :integration @describetag :integration
clear_config(API) do clear_config(Pleroma.Gun) do
Pleroma.Config.put(API, Pleroma.Gun) Pleroma.Config.put(Pleroma.Gun, Pleroma.Gun.API)
end end
test "opens connection and change owner", %{name: name} do test "opens connection and change owner", %{name: name} do

View file

@ -8,8 +8,8 @@ defmodule Pleroma.ReverseProxy.Client.TeslaTest do
alias Pleroma.ReverseProxy.Client alias Pleroma.ReverseProxy.Client
@moduletag :integration @moduletag :integration
clear_config_all(Pleroma.Gun.API) do clear_config_all(Pleroma.Gun) do
Pleroma.Config.put(Pleroma.Gun.API, Pleroma.Gun) Pleroma.Config.put(Pleroma.Gun, Pleroma.Gun.API)
end end
setup do setup do

View file

@ -349,8 +349,8 @@ test "with content-disposition header", %{conn: conn} do
Pleroma.Config.put(Pleroma.ReverseProxy.Client, Pleroma.ReverseProxy.Client.Tesla) Pleroma.Config.put(Pleroma.ReverseProxy.Client, Pleroma.ReverseProxy.Client.Tesla)
end end
clear_config(Pleroma.Gun.API) do clear_config(Pleroma.Gun) do
Pleroma.Config.put(Pleroma.Gun.API, Pleroma.Gun) Pleroma.Config.put(Pleroma.Gun, Pleroma.Gun.API)
end end
setup do setup do

View file

@ -2,16 +2,17 @@
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Gun.API.Mock do defmodule Pleroma.GunMock do
@behaviour Pleroma.Gun.API @behaviour Pleroma.Gun
alias Pleroma.Gun.API alias Pleroma.Gun
alias Pleroma.GunMock
@impl API @impl Gun
def open('some-domain.com', 443, _) do def open('some-domain.com', 443, _) do
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end) {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
Registry.register(API.Mock, conn_pid, %{ Registry.register(GunMock, conn_pid, %{
origin_scheme: "https", origin_scheme: "https",
origin_host: 'some-domain.com', origin_host: 'some-domain.com',
origin_port: 443 origin_port: 443
@ -20,7 +21,7 @@ def open('some-domain.com', 443, _) do
{:ok, conn_pid} {:ok, conn_pid}
end end
@impl API @impl Gun
def open(ip, port, _) def open(ip, port, _)
when ip in [{10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}, {127, 0, 0, 1}] and when ip in [{10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}, {127, 0, 0, 1}] and
port in [80, 443] do port in [80, 443] do
@ -28,7 +29,7 @@ def open(ip, port, _)
scheme = if port == 443, do: "https", else: "http" scheme = if port == 443, do: "https", else: "http"
Registry.register(API.Mock, conn_pid, %{ Registry.register(GunMock, conn_pid, %{
origin_scheme: scheme, origin_scheme: scheme,
origin_host: ip, origin_host: ip,
origin_port: port origin_port: port
@ -37,7 +38,7 @@ def open(ip, port, _)
{:ok, conn_pid} {:ok, conn_pid}
end end
@impl API @impl Gun
def open('localhost', 1234, %{ def open('localhost', 1234, %{
protocols: [:socks], protocols: [:socks],
proxy: {:socks5, 'localhost', 1234}, proxy: {:socks5, 'localhost', 1234},
@ -45,7 +46,7 @@ def open('localhost', 1234, %{
}) do }) do
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end) {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
Registry.register(API.Mock, conn_pid, %{ Registry.register(GunMock, conn_pid, %{
origin_scheme: "http", origin_scheme: "http",
origin_host: 'proxy-socks.com', origin_host: 'proxy-socks.com',
origin_port: 80 origin_port: 80
@ -54,7 +55,7 @@ def open('localhost', 1234, %{
{:ok, conn_pid} {:ok, conn_pid}
end end
@impl API @impl Gun
def open('localhost', 1234, %{ def open('localhost', 1234, %{
protocols: [:socks], protocols: [:socks],
proxy: {:socks4, 'localhost', 1234}, proxy: {:socks4, 'localhost', 1234},
@ -69,7 +70,7 @@ def open('localhost', 1234, %{
}) do }) do
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end) {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
Registry.register(API.Mock, conn_pid, %{ Registry.register(GunMock, conn_pid, %{
origin_scheme: "https", origin_scheme: "https",
origin_host: 'proxy-socks.com', origin_host: 'proxy-socks.com',
origin_port: 443 origin_port: 443
@ -78,14 +79,14 @@ def open('localhost', 1234, %{
{:ok, conn_pid} {:ok, conn_pid}
end end
@impl API @impl Gun
def open('gun-not-up.com', 80, _opts), do: {:error, :timeout} def open('gun-not-up.com', 80, _opts), do: {:error, :timeout}
@impl API @impl Gun
def open('example.com', port, _) when port in [443, 115] do def open('example.com', port, _) when port in [443, 115] do
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end) {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
Registry.register(API.Mock, conn_pid, %{ Registry.register(GunMock, conn_pid, %{
origin_scheme: "https", origin_scheme: "https",
origin_host: 'example.com', origin_host: 'example.com',
origin_port: 443 origin_port: 443
@ -94,11 +95,11 @@ def open('example.com', port, _) when port in [443, 115] do
{:ok, conn_pid} {:ok, conn_pid}
end end
@impl API @impl Gun
def open(domain, 80, _) do def open(domain, 80, _) do
{:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end) {:ok, conn_pid} = Task.start_link(fn -> Process.sleep(1_000) end)
Registry.register(API.Mock, conn_pid, %{ Registry.register(GunMock, conn_pid, %{
origin_scheme: "http", origin_scheme: "http",
origin_host: domain, origin_host: domain,
origin_port: 80 origin_port: 80
@ -107,48 +108,48 @@ def open(domain, 80, _) do
{:ok, conn_pid} {:ok, conn_pid}
end end
@impl API @impl Gun
def open({127, 0, 0, 1}, 8123, _) do def open({127, 0, 0, 1}, 8123, _) do
Task.start_link(fn -> Process.sleep(1_000) end) Task.start_link(fn -> Process.sleep(1_000) end)
end end
@impl API @impl Gun
def open('localhost', 9050, _) do def open('localhost', 9050, _) do
Task.start_link(fn -> Process.sleep(1_000) end) Task.start_link(fn -> Process.sleep(1_000) end)
end end
@impl API @impl Gun
def await_up(_pid, _timeout), do: {:ok, :http} def await_up(_pid, _timeout), do: {:ok, :http}
@impl API @impl Gun
def set_owner(_pid, _owner), do: :ok def set_owner(_pid, _owner), do: :ok
@impl API @impl Gun
def connect(pid, %{host: _, port: 80}) do def connect(pid, %{host: _, port: 80}) do
ref = make_ref() ref = make_ref()
Registry.register(API.Mock, ref, pid) Registry.register(GunMock, ref, pid)
ref ref
end end
@impl API @impl Gun
def connect(pid, %{host: _, port: 443, protocols: [:http2], transport: :tls}) do def connect(pid, %{host: _, port: 443, protocols: [:http2], transport: :tls}) do
ref = make_ref() ref = make_ref()
Registry.register(API.Mock, ref, pid) Registry.register(GunMock, ref, pid)
ref ref
end end
@impl API @impl Gun
def await(pid, ref) do def await(pid, ref) do
[{_, ^pid}] = Registry.lookup(API.Mock, ref) [{_, ^pid}] = Registry.lookup(GunMock, ref)
{:response, :fin, 200, []} {:response, :fin, 200, []}
end end
@impl API @impl Gun
def info(pid) do def info(pid) do
[{_, info}] = Registry.lookup(API.Mock, pid) [{_, info}] = Registry.lookup(GunMock, pid)
info info
end end
@impl API @impl Gun
def close(_pid), do: :ok def close(_pid), do: :ok
end end