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.Pool.Request do
10 def start_link(args) do
11 GenServer.start_link(__MODULE__, args)
15 def init(_), do: {:ok, []}
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)
24 def handle_call({:execute, client, request}, _from, state) do
25 response = Pleroma.HTTP.request_try(client, request)
27 {:reply, response, state}
31 def handle_info({:gun_data, _conn, stream, _, _}, state) do
32 # in some cases if we reuse conn and got {:error, :body_too_large}
33 # gun continues to send messages to this process,
34 # so we flush messages for this request
35 :ok = :gun.flush(stream)
41 def handle_info({:gun_up, _conn, _protocol}, state) do
46 def handle_info({:gun_down, _conn, _protocol, _reason, _killed}, state) do
47 # don't flush messages here, because gun can reconnect
52 def handle_info({:gun_error, _conn, stream, _error}, state) do
53 :ok = :gun.flush(stream)
58 def handle_info({:gun_push, _conn, _stream, _new_stream, _method, _uri, _headers}, state) do
63 def handle_info({:gun_response, _conn, _stream, _, _status, _headers}, state) do
68 def handle_info(msg, state) do
69 Logger.warn("Received unexpected message #{inspect(__MODULE__)} #{inspect(msg)}")