2020-10-12 19:00:50 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 07:49:20 +01:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-10-12 19:00:50 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-05-06 00:51:10 +02:00
|
|
|
defmodule Pleroma.Gun.ConnectionPool.Worker do
|
|
|
|
alias Pleroma.Gun
|
2020-05-06 22:14:24 +02:00
|
|
|
use GenServer, restart: :temporary
|
2020-05-06 00:51:10 +02:00
|
|
|
|
|
|
|
@registry Pleroma.Gun.ConnectionPool
|
|
|
|
|
2020-07-15 14:24:47 +02:00
|
|
|
def start_link([key | _] = opts) do
|
|
|
|
GenServer.start_link(__MODULE__, opts, name: {:via, Registry, {@registry, key}})
|
2020-05-06 22:14:24 +02:00
|
|
|
end
|
|
|
|
|
2020-05-06 00:51:10 +02:00
|
|
|
@impl true
|
2020-07-07 18:56:17 +02:00
|
|
|
def init([_key, _uri, _opts, _client_pid] = opts) do
|
|
|
|
{:ok, nil, {:continue, {:connect, opts}}}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_continue({:connect, [key, uri, opts, client_pid]}, _) do
|
2020-09-11 13:22:54 +02:00
|
|
|
with {:ok, conn_pid, protocol} <- Gun.Conn.open(uri, opts),
|
2020-05-06 00:51:10 +02:00
|
|
|
Process.link(conn_pid) do
|
2020-07-02 00:53:27 +02:00
|
|
|
time = :erlang.monotonic_time(:millisecond)
|
2020-07-15 14:24:47 +02:00
|
|
|
|
2020-05-06 00:51:10 +02:00
|
|
|
{_, _} =
|
2020-07-15 14:24:47 +02:00
|
|
|
Registry.update_value(@registry, key, fn _ ->
|
|
|
|
{conn_pid, [client_pid], 1, time}
|
2020-05-06 00:51:10 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
send(client_pid, {:conn_pid, conn_pid})
|
2020-05-08 17:18:59 +02:00
|
|
|
|
2020-07-07 18:56:17 +02:00
|
|
|
{:noreply,
|
2020-09-11 13:22:54 +02:00
|
|
|
%{
|
|
|
|
key: key,
|
|
|
|
timer: nil,
|
|
|
|
client_monitors: %{client_pid => Process.monitor(client_pid)},
|
|
|
|
protocol: protocol
|
|
|
|
}, :hibernate}
|
2020-05-06 00:51:10 +02:00
|
|
|
else
|
2020-07-08 13:01:02 +02:00
|
|
|
err ->
|
2020-07-15 14:58:08 +02:00
|
|
|
{:stop, {:shutdown, err}, nil}
|
2020-05-06 00:51:10 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2020-07-28 22:48:41 +02:00
|
|
|
def handle_cast({:add_client, client_pid}, state) do
|
2020-07-28 19:49:48 +02:00
|
|
|
case handle_call(:add_client, {client_pid, nil}, state) do
|
|
|
|
{:reply, conn_pid, state, :hibernate} ->
|
2020-07-28 22:48:41 +02:00
|
|
|
send(client_pid, {:conn_pid, conn_pid})
|
|
|
|
{:noreply, state, :hibernate}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_cast({:remove_client, client_pid}, state) do
|
|
|
|
case handle_call(:remove_client, {client_pid, nil}, state) do
|
|
|
|
{:reply, _, state, :hibernate} ->
|
2020-07-28 19:49:48 +02:00
|
|
|
{:noreply, state, :hibernate}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2020-09-11 13:22:54 +02:00
|
|
|
def handle_call(:add_client, {client_pid, _}, %{key: key, protocol: protocol} = state) do
|
2020-07-02 00:53:27 +02:00
|
|
|
time = :erlang.monotonic_time(:millisecond)
|
2020-05-06 00:51:10 +02:00
|
|
|
|
2020-09-11 13:22:54 +02:00
|
|
|
{{conn_pid, used_by, _, _}, _} =
|
2020-05-06 00:51:10 +02:00
|
|
|
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)
|
|
|
|
|
2020-09-11 13:22:54 +02:00
|
|
|
:telemetry.execute(
|
|
|
|
[:pleroma, :connection_pool, :client, :add],
|
|
|
|
%{client_pid: client_pid, clients: used_by},
|
|
|
|
%{key: state.key, protocol: protocol}
|
|
|
|
)
|
|
|
|
|
2020-05-06 00:51:10 +02:00
|
|
|
state =
|
|
|
|
if state.timer != nil do
|
|
|
|
Process.cancel_timer(state[:timer])
|
|
|
|
%{state | timer: nil}
|
|
|
|
else
|
|
|
|
state
|
|
|
|
end
|
|
|
|
|
2020-05-08 17:18:59 +02:00
|
|
|
ref = Process.monitor(client_pid)
|
|
|
|
|
|
|
|
state = put_in(state.client_monitors[client_pid], ref)
|
2020-07-28 19:49:48 +02:00
|
|
|
{:reply, conn_pid, state, :hibernate}
|
2020-05-06 00:51:10 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2020-07-28 19:49:48 +02:00
|
|
|
def handle_call(:remove_client, {client_pid, _}, %{key: key} = state) do
|
2020-05-06 00:51:10 +02:00
|
|
|
{{_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 17:18:59 +02:00
|
|
|
{ref, state} = pop_in(state.client_monitors[client_pid])
|
2020-08-19 09:22:59 +02:00
|
|
|
|
2020-09-16 16:23:05 +02:00
|
|
|
Process.demonitor(ref, [:flush])
|
2020-08-19 09:22:59 +02:00
|
|
|
|
2020-09-16 16:23:05 +02: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
|
2020-05-06 00:51:10 +02:00
|
|
|
end
|
|
|
|
|
2020-09-16 16:23:05 +02:00
|
|
|
{:reply, :ok, %{state | timer: timer}, :hibernate}
|
2020-05-06 00:51:10 +02:00
|
|
|
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
|
|
|
|
|
2020-08-19 09:22:59 +02:00
|
|
|
@impl true
|
|
|
|
def handle_info({:gun_up, _pid, _protocol}, state) do
|
|
|
|
{:noreply, state, :hibernate}
|
|
|
|
end
|
|
|
|
|
2020-05-06 00:51:10 +02:00
|
|
|
# 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
|
|
|
|
|
2020-08-19 09:22:59 +02:00
|
|
|
# Otherwise, wait for retry
|
2020-05-06 00:51:10 +02:00
|
|
|
@impl true
|
2020-08-19 09:22:59 +02:00
|
|
|
def handle_info({:gun_down, _pid, _protocol, _reason, _killed_streams}, state) do
|
|
|
|
{:noreply, state, :hibernate}
|
2020-05-06 00:51:10 +02:00
|
|
|
end
|
|
|
|
|
2020-05-08 17:18:59 +02:00
|
|
|
@impl true
|
|
|
|
def handle_info({:DOWN, _ref, :process, pid, reason}, state) do
|
2020-07-28 19:49:48 +02:00
|
|
|
:telemetry.execute(
|
2020-09-11 13:22:54 +02:00
|
|
|
[:pleroma, :connection_pool, :client, :dead],
|
2020-07-28 19:49:48 +02:00
|
|
|
%{client_pid: pid, reason: reason},
|
|
|
|
%{key: state.key}
|
|
|
|
)
|
2020-05-08 17:18:59 +02:00
|
|
|
|
2020-07-28 22:48:41 +02:00
|
|
|
handle_cast({:remove_client, pid}, state)
|
2020-05-08 17:18:59 +02:00
|
|
|
end
|
|
|
|
|
2020-05-06 00:51:10 +02:00
|
|
|
# LRFU policy: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.1478
|
|
|
|
defp crf(time_delta, prev_crf) do
|
2020-07-02 00:53:27 +02:00
|
|
|
1 + :math.pow(0.5, 0.0001 * time_delta) * prev_crf
|
2020-05-06 00:51:10 +02:00
|
|
|
end
|
|
|
|
end
|