X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fpool%2Fconnections.ex;h=7529e9240f33ca4ea03ca374c5679c4d55555661;hb=b2eb1124d115beda0907121c3c6f10783b34f352;hp=c4c5fd66c20785d23bb0ed66e7e2b8ce2e6a1ecf;hpb=2c8d80dc0ad594cfe25ebadd9e7a187c95914b34;p=akkoma
diff --git a/lib/pleroma/pool/connections.ex b/lib/pleroma/pool/connections.ex
index c4c5fd66c..7529e9240 100644
--- a/lib/pleroma/pool/connections.ex
+++ b/lib/pleroma/pool/connections.ex
@@ -1,11 +1,12 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Pool.Connections do
use GenServer
alias Pleroma.Config
+ alias Pleroma.Gun
require Logger
@@ -19,8 +20,6 @@ defmodule Pleroma.Pool.Connections do
defstruct conns: %{}, opts: []
- alias Pleroma.Gun.API
-
@spec start_link({atom(), keyword()}) :: {:ok, pid()}
def start_link({name, opts}) do
GenServer.start_link(__MODULE__, opts, name: name)
@@ -34,19 +33,18 @@ defmodule Pleroma.Pool.Connections do
def checkin(url, name) when is_binary(url), do: checkin(URI.parse(url), name)
def checkin(%URI{} = uri, name) do
- timeout = Config.get([:connections_pool, :receive_connection_timeout], 250)
+ timeout = Config.get([:connections_pool, :checkin_timeout], 250)
- GenServer.call(
- name,
- {:checkin, uri},
- timeout
- )
+ GenServer.call(name, {:checkin, uri}, timeout)
end
@spec alive?(atom()) :: boolean()
def alive?(name) do
- pid = Process.whereis(name)
- if pid, do: Process.alive?(pid), else: false
+ if pid = Process.whereis(name) do
+ Process.alive?(pid)
+ else
+ false
+ end
end
@spec get_state(atom()) :: t()
@@ -131,19 +129,20 @@ defmodule Pleroma.Pool.Connections do
%{conn: conn, gun_state: :up} = current_conn ->
Logger.debug("reusing conn #{key}")
- with time <- :os.system_time(:second),
- last_reference <- time - current_conn.last_reference,
- current_crf <- crf(last_reference, 100, current_conn.crf),
- state <-
- put_in(state.conns[key], %{
- current_conn
- | last_reference: time,
- crf: current_crf,
- conn_state: :active,
- used_by: [from | current_conn.used_by]
- }) do
- {:reply, conn, state}
- end
+ time = :os.system_time(:second)
+ last_reference = time - current_conn.last_reference
+ current_crf = crf(last_reference, 100, current_conn.crf)
+
+ state =
+ put_in(state.conns[key], %{
+ current_conn
+ | last_reference: time,
+ crf: current_crf,
+ conn_state: :active,
+ used_by: [from | current_conn.used_by]
+ })
+
+ {:reply, conn, state}
%{gun_state: :down} ->
{:reply, nil, state}
@@ -180,10 +179,11 @@ defmodule Pleroma.Pool.Connections do
state =
with conn_key when is_binary(conn_key) <- compose_key_gun_info(conn_pid),
{key, conn} <- find_conn(state.conns, conn_pid, conn_key),
- {true, key} <- {Process.alive?(conn_pid), key},
- time <- :os.system_time(:second),
- last_reference <- time - conn.last_reference,
- current_crf <- crf(last_reference, 100, conn.crf) do
+ {true, key} <- {Process.alive?(conn_pid), key} do
+ time = :os.system_time(:second)
+ last_reference = time - conn.last_reference
+ current_crf = crf(last_reference, 100, conn.crf)
+
put_in(state.conns[key], %{
conn
| gun_state: :up,
@@ -208,7 +208,7 @@ defmodule Pleroma.Pool.Connections do
nil ->
Logger.debug(":gun_up message for conn which is not found in state")
- :ok = API.close(conn_pid)
+ :ok = Gun.close(conn_pid)
state
end
@@ -218,14 +218,14 @@ defmodule Pleroma.Pool.Connections do
@impl true
def handle_info({:gun_down, conn_pid, _protocol, _reason, _killed}, state) do
- retries = Config.get([:connections_pool, :retry], 0)
+ retries = Config.get([:connections_pool, :retry], 1)
# we can't get info on this pid, because pid is dead
state =
with {key, conn} <- find_conn(state.conns, conn_pid),
{true, key} <- {Process.alive?(conn_pid), key} do
if conn.retries == retries do
Logger.debug("closing conn if retries is eq #{inspect(conn_pid)}")
- :ok = API.close(conn.conn)
+ :ok = Gun.close(conn.conn)
put_in(
state.conns,
@@ -251,7 +251,7 @@ defmodule Pleroma.Pool.Connections do
nil ->
Logger.debug(":gun_down message for conn which is not found in state")
- :ok = API.close(conn_pid)
+ :ok = Gun.close(conn_pid)
state
end
@@ -284,20 +284,15 @@ defmodule Pleroma.Pool.Connections do
end
defp compose_key_gun_info(pid) do
- try do
- # sometimes :gun.info can raise MatchError, which lead to pool terminate
- %{origin_host: origin_host, origin_scheme: scheme, origin_port: port} = API.info(pid)
-
- host =
- case :inet.ntoa(origin_host) do
- {:error, :einval} -> origin_host
- ip -> ip
- end
+ %{origin_host: origin_host, origin_scheme: scheme, origin_port: port} = Gun.info(pid)
- "#{scheme}:#{host}:#{port}"
- rescue
- _ -> :error_gun_info
- end
+ host =
+ case :inet.ntoa(origin_host) do
+ {:error, :einval} -> origin_host
+ ip -> ip
+ end
+
+ "#{scheme}:#{host}:#{port}"
end
defp find_conn(conns, conn_pid) do