1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
14 connect_timeout: 5_000,
15 domain_lookup_timeout: 5_000,
16 tls_handshake_timeout: 5_000,
19 await_up_timeout: 5_000
22 @type pool() :: :federation | :upload | :media | :default
24 @spec options(keyword(), URI.t()) :: keyword()
25 def options(incoming_opts \\ [], %URI{} = uri) do
29 |> AdapterHelper.format_proxy()
31 config_opts = Config.get([:http, :adapter], [])
34 |> Keyword.merge(config_opts)
35 |> add_scheme_opts(uri)
36 |> AdapterHelper.maybe_add_proxy(proxy)
37 |> Keyword.merge(incoming_opts)
41 defp add_scheme_opts(opts, %{scheme: "http"}), do: opts
43 defp add_scheme_opts(opts, %{scheme: "https"}) do
44 Keyword.put(opts, :certificates_verification, true)
47 defp put_timeout(opts) do
48 # this is the timeout to receive a message from Gun
49 Keyword.put_new(opts, :timeout, pool_timeout(opts[:pool]))
52 @spec pool_timeout(pool()) :: non_neg_integer()
53 def pool_timeout(pool) do
54 default = Config.get([:pools, :default, :timeout], 5_000)
56 Config.get([:pools, pool, :timeout], default)
59 @prefix Pleroma.Gun.ConnectionPool
61 wait = Config.get([:connections_pool, :connection_acquisition_wait])
62 retries = Config.get([:connections_pool, :connection_acquisition_retries])
66 |> Enum.each(fn {name, opts} ->
67 max_running = Keyword.get(opts, :size, 50)
68 max_waiting = Keyword.get(opts, :max_waiting, 10)
71 ConcurrentLimiter.new(:"#{@prefix}.#{name}", max_running, max_waiting,
78 {:error, :existing} -> :ok