1 defmodule Pleroma.Gun.ConnectionPool.Reclaimer do
2 use GenServer, restart: :temporary
4 @registry Pleroma.Gun.ConnectionPool
8 case :gen_server.start(__MODULE__, [], name: {:via, Registry, {@registry, "reclaimer"}}) do
12 {:error, {:already_registered, pid}} ->
16 {pid, Process.monitor(pid)}
21 {:ok, nil, {:continue, :reclaim}}
25 def handle_continue(:reclaim, _) do
26 max_connections = Pleroma.Config.get([:connections_pool, :max_connections])
29 [:connections_pool, :reclaim_multiplier]
30 |> Pleroma.Config.get()
31 |> Kernel.*(max_connections)
35 :telemetry.execute([:pleroma, :connection_pool, :reclaim, :start], %{}, %{
36 max_connections: max_connections,
37 reclaim_max: reclaim_max
41 # fn {_, {worker_pid, {_, used_by, crf, last_reference}}} when used_by == [] ->
42 # {worker_pid, crf, last_reference} end)
47 {{:_, :"$1", {:_, :"$2", :"$3", :"$4"}}, [{:==, :"$2", []}], [{{:"$1", :"$3", :"$4"}}]}
54 [:pleroma, :connection_pool, :reclaim, :stop],
55 %{reclaimed_count: 0},
57 max_connections: max_connections
61 {:stop, :no_unused_conns, nil}
66 |> Enum.sort(fn {_pid1, crf1, last_reference1}, {_pid2, crf2, last_reference2} ->
67 crf1 <= crf2 and last_reference1 <= last_reference2
69 |> Enum.take(reclaim_max)
72 |> Enum.each(fn {pid, _, _} ->
73 DynamicSupervisor.terminate_child(Pleroma.Gun.ConnectionPool.WorkerSupervisor, pid)
77 [:pleroma, :connection_pool, :reclaim, :stop],
78 %{reclaimed_count: Enum.count(reclaimed)},
79 %{max_connections: max_connections}