Add diagnostics http
[akkoma] / lib / pleroma / reverse_proxy / client / tesla.ex
index dbc6b66a3d87f791ef037df042f61eb6412fd95a..59fd5493cc7c1cb48f7b79ca3577cbbbd6a48b4f 100644 (file)
@@ -1,13 +1,13 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.ReverseProxy.Client.Tesla do
+  @behaviour Pleroma.ReverseProxy.Client
+
   @type headers() :: [{String.t(), String.t()}]
   @type status() :: pos_integer()
 
-  @behaviour Pleroma.ReverseProxy.Client
-
   @spec request(atom(), String.t(), headers(), String.t(), keyword()) ::
           {:ok, status(), headers}
           | {:ok, status(), headers, map()}
@@ -18,7 +18,7 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
   def request(method, url, headers, body, opts \\ []) do
     check_adapter()
 
-    opts = Keyword.merge(opts, body_as: :chunks)
+    opts = Keyword.put(opts, :body_as, :chunks)
 
     with {:ok, response} <-
            Pleroma.HTTP.request(
@@ -26,7 +26,7 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
              url,
              body,
              headers,
-             Keyword.put(opts, :adapter, opts)
+             opts
            ) do
       if is_map(response.body) and method != :head do
         {:ok, response.status, response.headers, response.body}
@@ -39,16 +39,9 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
   end
 
   @impl true
-  @spec stream_body(map()) :: {:ok, binary(), map()} | {:error, atom() | String.t()} | :done
-  def stream_body(%{pid: pid, opts: opts, fin: true}) do
-    # if connection was reused, but in tesla were redirects,
-    # tesla returns new opened connection, which must be closed manually
-    if opts[:old_conn], do: Tesla.Adapter.Gun.close(pid)
-    # if there were redirects we need to checkout old conn
-    conn = opts[:old_conn] || opts[:conn]
-
-    if conn, do: :ok = Pleroma.Pool.Connections.checkout(conn, self(), :gun_connections)
-
+  @spec stream_body(map()) ::
+          {:ok, binary(), map()} | {:error, atom() | String.t()} | :done | no_return()
+  def stream_body(%{pid: _pid, fin: true}) do
     :done
   end
 
@@ -72,18 +65,12 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
 
   @impl true
   @spec close(map) :: :ok | no_return()
-  def close(%{pid: pid}) do
-    adapter = check_adapter()
-    adapter.close(pid)
+  def close(%{pid: _pid}) do
   end
 
   defp check_adapter do
     adapter = Application.get_env(:tesla, :adapter)
 
-    unless adapter == Tesla.Adapter.Gun do
-      raise "#{adapter} doesn't support reading body in chunks"
-    end
-
     adapter
   end
 end