naming
[akkoma] / lib / pleroma / http / adapter / gun.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.Adapter.Gun do
6 @behaviour Pleroma.HTTP.Adapter
7
8 alias Pleroma.HTTP.Adapter
9
10 require Logger
11
12 alias Pleroma.Pool.Connections
13
14 @defaults [
15 connect_timeout: 5_000,
16 domain_lookup_timeout: 5_000,
17 tls_handshake_timeout: 5_000,
18 retry: 0,
19 await_up_timeout: 5_000
20 ]
21
22 @spec options(keyword(), URI.t()) :: keyword()
23 def options(connection_opts \\ [], %URI{} = uri) do
24 proxy = Pleroma.Config.get([:http, :proxy_url], nil)
25
26 @defaults
27 |> Keyword.merge(Pleroma.Config.get([:http, :adapter], []))
28 |> add_original(uri)
29 |> add_scheme_opts(uri)
30 |> Adapter.maybe_add_proxy(Adapter.format_proxy(proxy))
31 |> maybe_get_conn(uri, connection_opts)
32 end
33
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)
39 end
40
41 :ok
42 end
43
44 defp add_original(opts, %URI{host: host, port: port}) do
45 formatted_host = format_host(host)
46
47 Keyword.put(opts, :original, "#{formatted_host}:#{port}")
48 end
49
50 defp add_scheme_opts(opts, %URI{scheme: "http"}), do: opts
51
52 defp add_scheme_opts(opts, %URI{scheme: "https", host: host, port: port}) do
53 adapter_opts = [
54 certificates_verification: true,
55 tls_opts: [
56 verify: :verify_peer,
57 cacertfile: CAStore.file_path(),
58 depth: 20,
59 reuse_sessions: false,
60 verify_fun: {&:ssl_verify_hostname.verify_fun/3, [check_hostname: format_host(host)]},
61 log_level: :warning
62 ]
63 ]
64
65 adapter_opts =
66 if port != 443 do
67 Keyword.put(adapter_opts, :transport, :tls)
68 else
69 adapter_opts
70 end
71
72 Keyword.merge(opts, adapter_opts)
73 end
74
75 defp maybe_get_conn(adapter_opts, uri, connection_opts) do
76 {receive_conn?, opts} =
77 adapter_opts
78 |> Keyword.merge(connection_opts)
79 |> Keyword.pop(:receive_conn, true)
80
81 if Connections.alive?(:gun_connections) and receive_conn? do
82 try_to_get_conn(uri, opts)
83 else
84 opts
85 end
86 end
87
88 defp try_to_get_conn(uri, opts) do
89 try do
90 case Connections.checkin(uri, :gun_connections) do
91 nil ->
92 Logger.debug(
93 "Gun connections pool checkin was not successful. Trying to open conn for next request."
94 )
95
96 Task.start(fn -> Pleroma.Gun.Conn.open(uri, :gun_connections, opts) end)
97 opts
98
99 conn when is_pid(conn) ->
100 Logger.debug("received conn #{inspect(conn)} #{Connections.compose_uri_log(uri)}")
101
102 opts
103 |> Keyword.put(:conn, conn)
104 |> Keyword.put(:close_conn, false)
105 end
106 rescue
107 error ->
108 Logger.warn(
109 "Gun connections pool checkin caused error #{Connections.compose_uri_log(uri)} #{
110 inspect(error)
111 }"
112 )
113
114 opts
115 catch
116 # TODO: here must be no timeouts
117 :exit, {:timeout, {_, operation, [_, {method, _}, _]}} ->
118 {:message_queue_len, messages_len} =
119 :gun_connections
120 |> Process.whereis()
121 |> Process.info(:message_queue_len)
122
123 Logger.warn(
124 "Gun connections pool checkin with timeout error for #{operation} #{method} #{
125 Connections.compose_uri_log(uri)
126 }. Messages length: #{messages_len}"
127 )
128
129 opts
130
131 :exit, error ->
132 Logger.warn(
133 "Gun pool checkin exited with error #{Connections.compose_uri_log(uri)} #{
134 inspect(error)
135 }"
136 )
137
138 opts
139 end
140 end
141
142 @spec format_host(String.t()) :: charlist()
143 def format_host(host) do
144 host_charlist = to_charlist(host)
145
146 case :inet.parse_address(host_charlist) do
147 {:error, :einval} ->
148 :idna.encode(host_charlist)
149
150 {:ok, _ip} ->
151 host_charlist
152 end
153 end
154 end