Refactor User.post_register_action/1 emails
[akkoma] / lib / pleroma / gun / connection_pool / worker_supervisor.ex
1 defmodule Pleroma.Gun.ConnectionPool.WorkerSupervisor do
2 @moduledoc "Supervisor for pool workers. Does not do anything except enforce max connection limit"
3
4 use DynamicSupervisor
5
6 def start_link(opts) do
7 DynamicSupervisor.start_link(__MODULE__, opts, name: __MODULE__)
8 end
9
10 def init(_opts) do
11 DynamicSupervisor.init(
12 strategy: :one_for_one,
13 max_children: Pleroma.Config.get([:connections_pool, :max_connections])
14 )
15 end
16
17 def start_worker(opts, retry \\ false) do
18 case DynamicSupervisor.start_child(__MODULE__, {Pleroma.Gun.ConnectionPool.Worker, opts}) do
19 {:error, :max_children} ->
20 if retry or free_pool() == :error do
21 :telemetry.execute([:pleroma, :connection_pool, :provision_failure], %{opts: opts})
22 {:error, :pool_full}
23 else
24 start_worker(opts, true)
25 end
26
27 res ->
28 res
29 end
30 end
31
32 defp free_pool do
33 wait_for_reclaimer_finish(Pleroma.Gun.ConnectionPool.Reclaimer.start_monitor())
34 end
35
36 defp wait_for_reclaimer_finish({pid, mon}) do
37 receive do
38 {:DOWN, ^mon, :process, ^pid, :no_unused_conns} ->
39 :error
40
41 {:DOWN, ^mon, :process, ^pid, :normal} ->
42 :ok
43 end
44 end
45 end