logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

gun.ex (2182B)


  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. defmodule Pleroma.HTTP.AdapterHelper.Gun do
  5. @behaviour Pleroma.HTTP.AdapterHelper
  6. alias Pleroma.Config
  7. alias Pleroma.HTTP.AdapterHelper
  8. require Logger
  9. @defaults [
  10. retry: 1,
  11. retry_timeout: 1_000
  12. ]
  13. @type pool() :: :federation | :upload | :media | :default
  14. @spec options(keyword(), URI.t()) :: keyword()
  15. def options(incoming_opts \\ [], %URI{} = uri) do
  16. proxy =
  17. [:http, :proxy_url]
  18. |> Config.get()
  19. |> AdapterHelper.format_proxy()
  20. config_opts = Config.get([:http, :adapter], [])
  21. @defaults
  22. |> Keyword.merge(config_opts)
  23. |> add_scheme_opts(uri)
  24. |> AdapterHelper.maybe_add_proxy(proxy)
  25. |> Keyword.merge(incoming_opts)
  26. |> put_timeout()
  27. end
  28. defp add_scheme_opts(opts, %{scheme: "http"}), do: opts
  29. defp add_scheme_opts(opts, %{scheme: "https"}) do
  30. Keyword.put(opts, :certificates_verification, true)
  31. end
  32. defp put_timeout(opts) do
  33. {recv_timeout, opts} = Keyword.pop(opts, :recv_timeout, pool_timeout(opts[:pool]))
  34. # this is the timeout to receive a message from Gun
  35. # `:timeout` key is used in Tesla
  36. Keyword.put(opts, :timeout, recv_timeout)
  37. end
  38. @spec pool_timeout(pool()) :: non_neg_integer()
  39. def pool_timeout(pool) do
  40. default = Config.get([:pools, :default, :recv_timeout], 5_000)
  41. Config.get([:pools, pool, :recv_timeout], default)
  42. end
  43. def limiter_setup do
  44. prefix = Pleroma.Gun.ConnectionPool
  45. wait = Config.get([:connections_pool, :connection_acquisition_wait])
  46. retries = Config.get([:connections_pool, :connection_acquisition_retries])
  47. :pools
  48. |> Config.get([])
  49. |> Enum.each(fn {name, opts} ->
  50. max_running = Keyword.get(opts, :size, 50)
  51. max_waiting = Keyword.get(opts, :max_waiting, 10)
  52. result =
  53. ConcurrentLimiter.new(:"#{prefix}.#{name}", max_running, max_waiting,
  54. wait: wait,
  55. max_retries: retries
  56. )
  57. case result do
  58. :ok -> :ok
  59. {:error, :existing} -> :ok
  60. end
  61. end)
  62. :ok
  63. end
  64. end