# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Pool.Connections do
use GenServer
alias Pleroma.Config
+ alias Pleroma.Gun
require Logger
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)
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()
%{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: gun_state} when gun_state == :down ->
+ %{gun_state: :down} ->
{:reply, nil, state}
nil ->
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,
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
@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,
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
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