1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTTP.AdapterHelper.Gun do
6 @behaviour Pleroma.HTTP.AdapterHelper
9 alias Pleroma.HTTP.AdapterHelper
18 @type pool() :: :federation | :upload | :media | :default
20 @spec options(keyword(), URI.t()) :: keyword()
21 def options(incoming_opts \\ [], %URI{} = uri) do
25 |> AdapterHelper.format_proxy()
27 config_opts = Config.get([:http, :adapter], [])
30 |> Keyword.merge(config_opts)
31 |> add_scheme_opts(uri)
32 |> AdapterHelper.maybe_add_proxy(proxy)
33 |> Keyword.merge(incoming_opts)
37 defp add_scheme_opts(opts, %{scheme: "http"}), do: opts
39 defp add_scheme_opts(opts, %{scheme: "https"}) do
40 Keyword.put(opts, :certificates_verification, true)
43 defp put_timeout(opts) do
44 {recv_timeout, opts} = Keyword.pop(opts, :recv_timeout, pool_timeout(opts[:pool]))
45 # this is the timeout to receive a message from Gun
46 # `:timeout` key is used in Tesla
47 Keyword.put(opts, :timeout, recv_timeout)
50 @spec pool_timeout(pool()) :: non_neg_integer()
51 def pool_timeout(pool) do
52 default = Config.get([:pools, :default, :recv_timeout], 5_000)
54 Config.get([:pools, pool, :recv_timeout], default)
58 prefix = Pleroma.Gun.ConnectionPool
59 wait = Config.get([:connections_pool, :connection_acquisition_wait])
60 retries = Config.get([:connections_pool, :connection_acquisition_retries])
64 |> Enum.each(fn {name, opts} ->
65 max_running = Keyword.get(opts, :size, 50)
66 max_waiting = Keyword.get(opts, :max_waiting, 10)
69 ConcurrentLimiter.new(:"#{prefix}.#{name}", max_running, max_waiting,
76 {:error, :existing} -> :ok