X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=inline;f=lib%2Fpleroma%2Freverse_proxy%2Fclient%2Ftesla.ex;h=36a0a2060959257a4e3e7a662e6a8beec271add2;hb=ce517ff4e5a9cc144853ec8112bfac2a38a3a68a;hp=dbc6b66a3d87f791ef037df042f61eb6412fd95a;hpb=39ed608b13f9204e0503311c7394934b01501ab8;p=akkoma
diff --git a/lib/pleroma/reverse_proxy/client/tesla.ex b/lib/pleroma/reverse_proxy/client/tesla.ex
index dbc6b66a3..36a0a2060 100644
--- a/lib/pleroma/reverse_proxy/client/tesla.ex
+++ b/lib/pleroma/reverse_proxy/client/tesla.ex
@@ -1,13 +1,15 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ReverseProxy.Client.Tesla do
+ @behaviour Pleroma.ReverseProxy.Client
+
+ alias Pleroma.Gun.ConnectionPool
+
@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 +20,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,11 +28,13 @@ 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}
else
+ conn_pid = response.opts[:adapter][:conn]
+ ConnectionPool.release_conn(conn_pid)
{:ok, response.status, response.headers}
end
else
@@ -39,16 +43,10 @@ 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
+ ConnectionPool.release_conn(pid)
:done
end
@@ -73,8 +71,7 @@ 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)
+ ConnectionPool.release_conn(pid)
end
defp check_adapter do