2020-05-05 15:51:10 -07:00
|
|
|
defmodule Pleroma.Gun.ConnectionPool.Worker do
|
|
|
|
alias Pleroma.Gun
|
2020-05-06 13:14:24 -07:00
|
|
|
use GenServer, restart: :temporary
|
2020-05-05 15:51:10 -07:00
|
|
|
|
|
|
|
@registry Pleroma.Gun.ConnectionPool
|
|
|
|
|
2020-07-15 05:24:47 -07:00
|
|
|
def start_link([key | _] = opts) do
|
|
|
|
GenServer.start_link(__MODULE__, opts, name: {:via, Registry, {@registry, key}})
|
2020-05-06 13:14:24 -07:00
|
|
|
end
|
|
|
|
|
2020-05-05 15:51:10 -07:00
|
|
|
@impl true
|
2020-07-15 05:24:47 -07:00
|
|
|
def init([key, uri, opts, client_pid]) do
|
|
|
|
with {:ok, conn_pid} <- Gun.Conn.open(uri, opts),
|
2020-05-05 15:51:10 -07:00
|
|
|
Process.link(conn_pid) do
|
2020-06-17 02:54:13 -07:00
|
|
|
time = :erlang.monotonic_time()
|
2020-07-15 05:24:47 -07:00
|
|
|
|
2020-05-05 15:51:10 -07:00
|
|
|
{_, _} =
|
2020-07-15 05:24:47 -07:00
|
|
|
Registry.update_value(@registry, key, fn _ ->
|
|
|
|
{conn_pid, [client_pid], 1, time}
|
2020-05-05 15:51:10 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
send(client_pid, {:conn_pid, conn_pid})
|
2020-05-08 08:18:59 -07:00
|
|
|
|
|
|
|
{:ok,
|
|
|
|
%{key: key, timer: nil, client_monitors: %{client_pid => Process.monitor(client_pid)}},
|
|
|
|
:hibernate}
|
2020-05-05 15:51:10 -07:00
|
|
|
else
|
|
|
|
err -> {:stop, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_cast({:add_client, client_pid, send_pid_back}, %{key: key} = state) do
|
2020-06-17 02:54:13 -07:00
|
|
|
time = :erlang.monotonic_time()
|
2020-05-05 15:51:10 -07:00
|
|
|
|
|
|
|
{{conn_pid, _, _, _}, _} =
|
|
|
|
Registry.update_value(@registry, key, fn {conn_pid, used_by, crf, last_reference} ->
|
|
|
|
{conn_pid, [client_pid | used_by], crf(time - last_reference, crf), time}
|
|
|
|
end)
|
|
|
|
|
|
|
|
if send_pid_back, do: send(client_pid, {:conn_pid, conn_pid})
|
|
|
|
|
|
|
|
state =
|
|
|
|
if state.timer != nil do
|
|
|
|
Process.cancel_timer(state[:timer])
|
|
|
|
%{state | timer: nil}
|
|
|
|
else
|
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2020-05-08 08:18:59 -07:00
|
|
|
ref = Process.monitor(client_pid)
|
|
|
|
|
|
|
|
state = put_in(state.client_monitors[client_pid], ref)
|
2020-05-05 15:51:10 -07:00
|
|
|
{:noreply, state, :hibernate}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_cast({:remove_client, client_pid}, %{key: key} = state) do
|
|
|
|
{{_conn_pid, used_by, _crf, _last_reference}, _} =
|
|
|
|
Registry.update_value(@registry, key, fn {conn_pid, used_by, crf, last_reference} ->
|
|
|
|
{conn_pid, List.delete(used_by, client_pid), crf, last_reference}
|
|
|
|
end)
|
|
|
|
|
2020-05-08 08:18:59 -07:00
|
|
|
{ref, state} = pop_in(state.client_monitors[client_pid])
|
|
|
|
Process.demonitor(ref)
|
|
|
|
|
2020-05-05 15:51:10 -07:00
|
|
|
timer =
|
|
|
|
if used_by == [] do
|
|
|
|
max_idle = Pleroma.Config.get([:connections_pool, :max_idle_time], 30_000)
|
|
|
|
Process.send_after(self(), :idle_close, max_idle)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, %{state | timer: timer}, :hibernate}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info(:idle_close, state) do
|
|
|
|
# Gun monitors the owner process, and will close the connection automatically
|
|
|
|
# when it's terminated
|
|
|
|
{:stop, :normal, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Gracefully shutdown if the connection got closed without any streams left
|
|
|
|
@impl true
|
|
|
|
def handle_info({:gun_down, _pid, _protocol, _reason, []}, state) do
|
|
|
|
{:stop, :normal, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Otherwise, shutdown with an error
|
|
|
|
@impl true
|
|
|
|
def handle_info({:gun_down, _pid, _protocol, _reason, _killed_streams} = down_message, state) do
|
|
|
|
{:stop, {:error, down_message}, state}
|
|
|
|
end
|
|
|
|
|
2020-05-08 08:18:59 -07:00
|
|
|
@impl true
|
|
|
|
def handle_info({:DOWN, _ref, :process, pid, reason}, state) do
|
|
|
|
# Sometimes the client is dead before we demonitor it in :remove_client, so the message
|
|
|
|
# arrives anyway
|
|
|
|
|
|
|
|
case state.client_monitors[pid] do
|
|
|
|
nil ->
|
|
|
|
{:noreply, state, :hibernate}
|
|
|
|
|
|
|
|
_ref ->
|
|
|
|
:telemetry.execute(
|
|
|
|
[:pleroma, :connection_pool, :client_death],
|
|
|
|
%{client_pid: pid, reason: reason},
|
|
|
|
%{key: state.key}
|
|
|
|
)
|
|
|
|
|
|
|
|
handle_cast({:remove_client, pid}, state)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-05 15:51:10 -07:00
|
|
|
# LRFU policy: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.1478
|
|
|
|
defp crf(time_delta, prev_crf) do
|
|
|
|
1 + :math.pow(0.5, time_delta / 100) * prev_crf
|
|
|
|
end
|
|
|
|
end
|