aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/pool/request.ex
blob: 3fb930db72fbb51d4d7fcac9011b64b4efa03c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Pool.Request do
  use GenServer

  require Logger

  def start_link(args) do
    GenServer.start_link(__MODULE__, args)
  end

  @impl true
  def init(_), do: {:ok, []}

  @spec execute(pid() | atom(), Tesla.Client.t(), keyword(), pos_integer()) ::
          {:ok, Tesla.Env.t()} | {:error, any()}
  def execute(pid, client, request, timeout) do
    GenServer.call(pid, {:execute, client, request}, timeout)
  end

  @impl true
  def handle_call({:execute, client, request}, _from, state) do
    response = Pleroma.HTTP.request(client, request)

    {:reply, response, state}
  end

  @impl true
  def handle_info({:gun_data, _conn, _stream, _, _}, state) do
    {:noreply, state}
  end

  @impl true
  def handle_info({:gun_up, _conn, _protocol}, state) do
    {:noreply, state}
  end

  @impl true
  def handle_info({:gun_down, _conn, _protocol, _reason, _killed}, state) do
    {:noreply, state}
  end

  @impl true
  def handle_info({:gun_error, _conn, _stream, _error}, state) do
    {:noreply, state}
  end

  @impl true
  def handle_info({:gun_push, _conn, _stream, _new_stream, _method, _uri, _headers}, state) do
    {:noreply, state}
  end

  @impl true
  def handle_info({:gun_response, _conn, _stream, _, _status, _headers}, state) do
    {:noreply, state}
  end

  @impl true
  def handle_info(msg, state) do
    Logger.warn("Received unexpected message #{inspect(__MODULE__)} #{inspect(msg)}")
    {:noreply, state}
  end
end