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
8 alias Pleroma.HTTP.AdapterHelper
9 alias Pleroma.Pool.Connections
14 connect_timeout: 5_000,
15 domain_lookup_timeout: 5_000,
16 tls_handshake_timeout: 5_000,
19 await_up_timeout: 5_000
22 @spec options(keyword(), URI.t()) :: keyword()
23 def options(connection_opts \\ [], %URI{} = uri) do
25 Pleroma.Config.get([:http, :proxy_url], nil)
26 |> AdapterHelper.format_proxy()
28 config_opts = Pleroma.Config.get([:http, :adapter], [])
31 |> Keyword.merge(config_opts)
32 |> add_scheme_opts(uri)
33 |> AdapterHelper.maybe_add_proxy(formatted_proxy)
34 |> maybe_get_conn(uri, connection_opts)
37 @spec after_request(keyword()) :: :ok
38 def after_request(opts) do
39 if opts[:conn] && opts[:body_as] != :chunks do
40 Connections.checkout(opts[:conn], self(), :gun_connections)
46 defp add_scheme_opts(opts, %URI{scheme: "http"}), do: opts
48 defp add_scheme_opts(opts, %URI{scheme: "https"}) do
50 |> Keyword.put(:certificates_verification, true)
51 |> Keyword.put(:transport, :tls)
52 |> Keyword.put(:tls_opts, log_level: :warning)
55 defp maybe_get_conn(adapter_opts, uri, connection_opts) do
56 {receive_conn?, opts} =
58 |> Keyword.merge(connection_opts)
59 |> Keyword.pop(:receive_conn, true)
61 if Connections.alive?(:gun_connections) and receive_conn? do
62 try_to_get_conn(uri, opts)
68 defp try_to_get_conn(uri, opts) do
69 case Connections.checkin(uri, :gun_connections) do
72 "Gun connections pool checkin was not successful. Trying to open conn for next request."
75 Task.start(fn -> Pleroma.Gun.Conn.open(uri, :gun_connections, opts) end)
78 conn when is_pid(conn) ->
79 Logger.debug("received conn #{inspect(conn)} #{Connections.compose_uri_log(uri)}")
82 |> Keyword.put(:conn, conn)
83 |> Keyword.put(:close_conn, false)