1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTTP.Adapter.Gun do
6 @behaviour Pleroma.HTTP.Adapter
8 alias Pleroma.HTTP.Adapter
12 alias Pleroma.Pool.Connections
15 connect_timeout: 20_000,
16 domain_lookup_timeout: 5_000,
17 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
24 proxy = Pleroma.Config.get([:http, :proxy_url], nil)
27 |> Keyword.merge(Pleroma.Config.get([:http, :adapter], []))
29 |> add_scheme_opts(uri)
30 |> Adapter.maybe_add_proxy(Adapter.format_proxy(proxy))
31 |> maybe_get_conn(uri, connection_opts)
34 @spec after_request(keyword()) :: :ok
35 def after_request(opts) do
36 with conn when not is_nil(conn) <- opts[:conn],
37 body_as when body_as != :chunks <- opts[:body_as] do
38 Connections.checkout(conn, self(), :gun_connections)
44 defp add_original(opts, %URI{host: host, port: port}) do
45 formatted_host = Adapter.domain_or_fallback(host)
47 Keyword.put(opts, :original, "#{formatted_host}:#{port}")
50 defp add_scheme_opts(opts, %URI{scheme: "http"}), do: opts
52 defp add_scheme_opts(opts, %URI{scheme: "https", host: host, port: port}) do
54 certificates_verification: true,
57 cacertfile: CAStore.file_path(),
59 reuse_sessions: false,
61 {&:ssl_verify_hostname.verify_fun/3, [check_hostname: Adapter.domain_or_fallback(host)]}
67 Keyword.put(adapter_opts, :transport, :tls)
72 Keyword.merge(opts, adapter_opts)
75 defp maybe_get_conn(adapter_opts, uri, connection_opts) do
76 {receive_conn?, opts} =
78 |> Keyword.merge(connection_opts)
79 |> Keyword.pop(:receive_conn, true)
81 if Connections.alive?(:gun_connections) and receive_conn? do
82 try_to_get_conn(uri, opts)
88 defp try_to_get_conn(uri, opts) do
90 case Connections.checkin(uri, :gun_connections) do
93 "Gun connections pool checkin was not successful. Trying to open conn for next request."
96 :ok = Connections.open_conn(uri, :gun_connections, opts)
99 conn when is_pid(conn) ->
101 "received conn #{inspect(conn)} #{uri.scheme}://#{Connections.compose_uri(uri)}"
105 |> Keyword.put(:conn, conn)
106 |> Keyword.put(:close_conn, false)
111 "Gun connections pool checkin caused error #{uri.scheme}://#{
112 Connections.compose_uri(uri)
118 :exit, {:timeout, _} ->
120 "Gun connections pool checkin with timeout error #{uri.scheme}://#{
121 Connections.compose_uri(uri)
129 "Gun pool checkin exited with error #{uri.scheme}://#{Connections.compose_uri(uri)} #{