Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Gun.ConnectionPool.WorkerSupervisor do
  5. @moduledoc "Supervisor for pool workers. Does not do anything except enforce max connection limit"
  6. use DynamicSupervisor
  7. def start_link(opts) do
  8. DynamicSupervisor.start_link(__MODULE__, opts, name: __MODULE__)
  9. end
  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. def start_worker(opts, retry \\ false) do
  17. case DynamicSupervisor.start_child(__MODULE__, {Pleroma.Gun.ConnectionPool.Worker, opts}) do
  18. {:error, :max_children} ->
  19. if retry or free_pool() == :error do
  20. :telemetry.execute([:pleroma, :connection_pool, :provision_failure], %{opts: opts})
  21. {:error, :pool_full}
  22. else
  23. start_worker(opts, true)
  24. end
  25. res ->
  26. res
  27. end
  28. end
  29. defp free_pool do
  30. wait_for_reclaimer_finish(Pleroma.Gun.ConnectionPool.Reclaimer.start_monitor())
  31. end
  32. defp wait_for_reclaimer_finish({pid, mon}) do
  33. receive do
  34. {:DOWN, ^mon, :process, ^pid, :no_unused_conns} ->
  35. :error
  36. {:DOWN, ^mon, :process, ^pid, :normal} ->
  37. :ok
  38. end
  39. end
  40. end