Merge branch 'develop' into issue/1276
[akkoma] / lib / pleroma / pool / request.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.Pool.Request do
6 use GenServer
7
8 require Logger
9
10 def start_link(args) do
11 GenServer.start_link(__MODULE__, args)
12 end
13
14 @impl true
15 def init(_), do: {:ok, []}
16
17 @spec execute(pid() | atom(), Tesla.Client.t(), keyword(), pos_integer()) ::
18 {:ok, Tesla.Env.t()} | {:error, any()}
19 def execute(pid, client, request, timeout) do
20 GenServer.call(pid, {:execute, client, request}, timeout)
21 end
22
23 @impl true
24 def handle_call({:execute, client, request}, _from, state) do
25 response = Pleroma.HTTP.request(client, request)
26
27 {:reply, response, state}
28 end
29
30 @impl true
31 def handle_info({:gun_data, _conn, _stream, _, _}, state) do
32 {:noreply, state}
33 end
34
35 @impl true
36 def handle_info({:gun_up, _conn, _protocol}, state) do
37 {:noreply, state}
38 end
39
40 @impl true
41 def handle_info({:gun_down, _conn, _protocol, _reason, _killed}, state) do
42 {:noreply, state}
43 end
44
45 @impl true
46 def handle_info({:gun_error, _conn, _stream, _error}, state) do
47 {:noreply, state}
48 end
49
50 @impl true
51 def handle_info({:gun_push, _conn, _stream, _new_stream, _method, _uri, _headers}, state) do
52 {:noreply, state}
53 end
54
55 @impl true
56 def handle_info({:gun_response, _conn, _stream, _, _status, _headers}, state) do
57 {:noreply, state}
58 end
59
60 @impl true
61 def handle_info(msg, state) do
62 Logger.warn("Received unexpected message #{inspect(__MODULE__)} #{inspect(msg)}")
63 {:noreply, state}
64 end
65 end