total rebase
[anni] / lib / pleroma / gun / connection_pool / worker_supervisor.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Gun.ConnectionPool.WorkerSupervisor do
6   @moduledoc "Supervisor for pool workers. Does not do anything except enforce max connection limit"
7
8   use DynamicSupervisor
9
10   def start_link(opts) do
11     DynamicSupervisor.start_link(__MODULE__, opts, name: __MODULE__)
12   end
13
14   def init(_opts) do
15     DynamicSupervisor.init(
16       strategy: :one_for_one,
17       max_children: Pleroma.Config.get([:connections_pool, :max_connections])
18     )
19   end
20
21   def start_worker(opts, last_attempt \\ false) do
22     case DynamicSupervisor.start_child(__MODULE__, {Pleroma.Gun.ConnectionPool.Worker, opts}) do
23       {:error, :max_children} ->
24         funs = [fn -> last_attempt end, fn -> match?(:error, free_pool()) end]
25
26         if Enum.any?(funs, fn fun -> fun.() end) do
27           :telemetry.execute([:pleroma, :connection_pool, :provision_failure], %{opts: opts})
28           {:error, :pool_full}
29         else
30           start_worker(opts, true)
31         end
32
33       res ->
34         res
35     end
36   end
37
38   defp free_pool do
39     wait_for_reclaimer_finish(Pleroma.Gun.ConnectionPool.Reclaimer.start_monitor())
40   end
41
42   defp wait_for_reclaimer_finish({pid, mon}) do
43     receive do
44       {:DOWN, ^mon, :process, ^pid, :no_unused_conns} ->
45         :error
46
47       {:DOWN, ^mon, :process, ^pid, :normal} ->
48         :ok
49     end
50   end
51 end