1 defmodule Pleroma.Gun.ConnectionPool.WorkerSupervisor do
2 @doc "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)
39 max_connections = Pleroma.Config.get([:connections_pool, :max_connections])
42 [:connections_pool, :reclaim_multiplier]
43 |> Pleroma.Config.get()
44 |> Kernel.*(max_connections)
52 {{:_, :"$1", {:_, :"$2", :"$3", :"$4"}}, [{:==, :"$2", []}],
53 [{{:"$1", :"$3", :"$4"}}]}
59 exit(:no_unused_conns)
63 |> Enum.sort(fn {_pid1, crf1, last_reference1}, {_pid2, crf2, last_reference2} ->
64 crf1 <= crf2 and last_reference1 <= last_reference2
66 |> Enum.take(reclaim_max)
67 |> Enum.each(fn {pid, _, _} ->
68 DynamicSupervisor.terminate_child(__MODULE__, pid)
73 wait_for_enforcer_finish(pid)
76 wait_for_enforcer_finish(pid)
80 defp wait_for_enforcer_finish(pid) do
81 ref = Process.monitor(pid)
84 {:DOWN, ^ref, :process, ^pid, :no_unused_conns} ->
87 {:DOWN, ^ref, :process, ^pid, :normal} ->