some clean up and pleroma header
This commit is contained in:
parent
05ca6a7d2c
commit
3bc3e61dc5
@ -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
|
||||
|
@ -1,6 +1,12 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# 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
|
||||
|
@ -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
|
||||
|
@ -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{
|
||||
|
@ -9,7 +9,8 @@ defmodule Pleroma.HTTP.Connection do
|
||||
|
||||
@options [
|
||||
connect_timeout: 10_000,
|
||||
timeout: 20_000
|
||||
timeout: 20_000,
|
||||
pool: :federation
|
||||
]
|
||||
|
||||
@doc """
|
||||
|
@ -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
|
||||
|
||||
|
@ -1,3 +1,7 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.ReverseProxy.Client.Hackney do
|
||||
@behaviour Pleroma.ReverseProxy.Client
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
@ -1,3 +1,7 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.ReverseProxy.Client.HackneyTest do
|
||||
use Pleroma.ReverseProxyClientCase, client: Pleroma.ReverseProxy.Client.Hackney
|
||||
|
||||
|
@ -1,3 +1,7 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.ReverseProxy.Client.TeslaTest do
|
||||
use Pleroma.ReverseProxyClientCase, client: Pleroma.ReverseProxy.Client.Tesla
|
||||
|
||||
|
@ -1,3 +1,7 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.ReverseProxyClientCase do
|
||||
defmacro __using__(client: client) do
|
||||
quote do
|
||||
|
Loading…
Reference in New Issue
Block a user