1 defmodule Pleroma.Gun.ConnectionPool.WorkerSupervisor do
2 @moduledoc "Supervisor for pool workers. Does not do anything except enforce max connection limit"
6 def start_link(opts) do
7 DynamicSupervisor.start_link(__MODULE__, opts, name: __MODULE__)
11 DynamicSupervisor.init(
12 strategy: :one_for_one,
13 max_children: Pleroma.Config.get([:connections_pool, :max_connections])
17 def start_worker(opts) do
18 case DynamicSupervisor.start_child(__MODULE__, {Pleroma.Gun.ConnectionPool.Worker, opts}) do
19 {:error, :max_children} ->
21 :ok -> start_worker(opts)
22 :error -> {:error, :pool_full}
30 @registry Pleroma.Gun.ConnectionPool
31 @enforcer_key "enforcer"
33 case Registry.lookup(@registry, @enforcer_key) do
37 {:ok, _pid} = Registry.register(@registry, @enforcer_key, nil)
38 max_connections = Pleroma.Config.get([:connections_pool, :max_connections])
41 [:connections_pool, :reclaim_multiplier]
42 |> Pleroma.Config.get()
43 |> Kernel.*(max_connections)
51 {{:_, :"$1", {:_, :"$2", :"$3", :"$4"}}, [{:==, :"$2", []}],
52 [{{:"$1", :"$3", :"$4"}}]}
58 exit(:no_unused_conns)
62 |> Enum.sort(fn {_pid1, crf1, last_reference1}, {_pid2, crf2, last_reference2} ->
63 crf1 <= crf2 and last_reference1 <= last_reference2
65 |> Enum.take(reclaim_max)
66 |> Enum.each(fn {pid, _, _} ->
67 DynamicSupervisor.terminate_child(__MODULE__, pid)
72 wait_for_enforcer_finish(pid)
75 wait_for_enforcer_finish(pid)
79 defp wait_for_enforcer_finish(pid) do
80 ref = Process.monitor(pid)
83 {:DOWN, ^ref, :process, ^pid, :no_unused_conns} ->
86 {:DOWN, ^ref, :process, ^pid, :normal} ->