2020-02-11 07:12:57 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:16:24 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2020-02-11 07:12:57 +00:00
|
|
|
# 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
|
2020-03-05 14:31:06 +00:00
|
|
|
response = Pleroma.HTTP.request(client, request)
|
2020-02-11 07:12:57 +00:00
|
|
|
|
2020-05-08 13:09:53 +00:00
|
|
|
{:reply, response, state, :hibernate}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2020-03-04 15:13:24 +00:00
|
|
|
def handle_info({:gun_data, _conn, _stream, _, _}, state) do
|
2020-05-08 13:09:53 +00:00
|
|
|
{:noreply, state, :hibernate}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info({:gun_up, _conn, _protocol}, state) do
|
2020-05-08 13:09:53 +00:00
|
|
|
{:noreply, state, :hibernate}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info({:gun_down, _conn, _protocol, _reason, _killed}, state) do
|
2020-05-08 13:09:53 +00:00
|
|
|
{:noreply, state, :hibernate}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2020-03-04 15:13:24 +00:00
|
|
|
def handle_info({:gun_error, _conn, _stream, _error}, state) do
|
2020-05-08 13:09:53 +00:00
|
|
|
{:noreply, state, :hibernate}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info({:gun_push, _conn, _stream, _new_stream, _method, _uri, _headers}, state) do
|
2020-05-08 13:09:53 +00:00
|
|
|
{:noreply, state, :hibernate}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info({:gun_response, _conn, _stream, _, _status, _headers}, state) do
|
2020-05-08 13:09:53 +00:00
|
|
|
{:noreply, state, :hibernate}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info(msg, state) do
|
|
|
|
Logger.warn("Received unexpected message #{inspect(__MODULE__)} #{inspect(msg)}")
|
2020-05-08 13:09:53 +00:00
|
|
|
{:noreply, state, :hibernate}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
end
|