2020-10-12 17:00:50 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-10-12 17:00:50 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-07-08 11:22:42 +00:00
|
|
|
defmodule Pleroma.Gun.ConnectionPool.Reclaimer do
|
|
|
|
use GenServer, restart: :temporary
|
|
|
|
|
2021-05-21 17:31:28 +00:00
|
|
|
defp registry, do: Pleroma.Gun.ConnectionPool
|
2020-07-08 11:22:42 +00:00
|
|
|
|
2020-07-08 11:58:38 +00:00
|
|
|
def start_monitor do
|
2020-07-08 11:22:42 +00:00
|
|
|
pid =
|
Pleroma.Gun.ConnectionPool.Reclaimer: dialyzer error
lib/pleroma/gun/connection_pool/reclaimer.ex:12:call
The function call will not succeed.
:gen_server.start(Pleroma.Gun.ConnectionPool.Reclaimer, [], [
{:name, {:via, Registry, {Pleroma.Gun.ConnectionPool, <<_::72>>}}},
...
])
will never return since the success typing is:
(atom(), any(), [
{:debug, [:log | :statistics | :trace | {_, _}]}
| {:hibernate_after, timeout()}
| {:spawn_opt, [:link | :monitor | {_, _}]}
| {:timeout, timeout()}
]) :: :ignore | {:error, _} | {:ok, pid() | {pid(), reference()}}
and the contract is
(Module :: module(), Args :: term(), Options :: [start_opt()]) :: start_ret()
2024-01-27 19:25:16 +00:00
|
|
|
case GenServer.start_link(__MODULE__, [], name: {:via, Registry, {registry(), "reclaimer"}}) do
|
2020-07-08 11:22:42 +00:00
|
|
|
{:ok, pid} ->
|
|
|
|
pid
|
|
|
|
|
|
|
|
{:error, {:already_registered, pid}} ->
|
|
|
|
pid
|
|
|
|
end
|
|
|
|
|
|
|
|
{pid, Process.monitor(pid)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def init(_) do
|
|
|
|
{:ok, nil, {:continue, :reclaim}}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_continue(:reclaim, _) do
|
|
|
|
max_connections = Pleroma.Config.get([:connections_pool, :max_connections])
|
|
|
|
|
|
|
|
reclaim_max =
|
|
|
|
[:connections_pool, :reclaim_multiplier]
|
|
|
|
|> Pleroma.Config.get()
|
|
|
|
|> Kernel.*(max_connections)
|
|
|
|
|> round
|
|
|
|
|> max(1)
|
|
|
|
|
|
|
|
:telemetry.execute([:pleroma, :connection_pool, :reclaim, :start], %{}, %{
|
|
|
|
max_connections: max_connections,
|
|
|
|
reclaim_max: reclaim_max
|
|
|
|
})
|
|
|
|
|
|
|
|
# :ets.fun2ms(
|
|
|
|
# fn {_, {worker_pid, {_, used_by, crf, last_reference}}} when used_by == [] ->
|
|
|
|
# {worker_pid, crf, last_reference} end)
|
|
|
|
unused_conns =
|
|
|
|
Registry.select(
|
2021-05-21 17:31:28 +00:00
|
|
|
registry(),
|
2020-07-08 11:22:42 +00:00
|
|
|
[
|
|
|
|
{{:_, :"$1", {:_, :"$2", :"$3", :"$4"}}, [{:==, :"$2", []}], [{{:"$1", :"$3", :"$4"}}]}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
case unused_conns do
|
|
|
|
[] ->
|
|
|
|
:telemetry.execute(
|
|
|
|
[:pleroma, :connection_pool, :reclaim, :stop],
|
|
|
|
%{reclaimed_count: 0},
|
|
|
|
%{
|
|
|
|
max_connections: max_connections
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
{:stop, :no_unused_conns, nil}
|
|
|
|
|
|
|
|
unused_conns ->
|
|
|
|
reclaimed =
|
|
|
|
unused_conns
|
|
|
|
|> Enum.sort(fn {_pid1, crf1, last_reference1}, {_pid2, crf2, last_reference2} ->
|
|
|
|
crf1 <= crf2 and last_reference1 <= last_reference2
|
|
|
|
end)
|
|
|
|
|> Enum.take(reclaim_max)
|
|
|
|
|
|
|
|
reclaimed
|
|
|
|
|> Enum.each(fn {pid, _, _} ->
|
|
|
|
DynamicSupervisor.terminate_child(Pleroma.Gun.ConnectionPool.WorkerSupervisor, pid)
|
|
|
|
end)
|
|
|
|
|
|
|
|
:telemetry.execute(
|
|
|
|
[:pleroma, :connection_pool, :reclaim, :stop],
|
|
|
|
%{reclaimed_count: Enum.count(reclaimed)},
|
|
|
|
%{max_connections: max_connections}
|
|
|
|
)
|
|
|
|
|
|
|
|
{:stop, :normal, nil}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|