forked from AkkomaGang/akkoma
removing unnecessary with
comment in tesla client impovement
This commit is contained in:
parent
24d1ac125c
commit
d0e4d3ca3b
2 changed files with 25 additions and 23 deletions
|
@ -36,17 +36,16 @@ def checkin(url, name) when is_binary(url), do: checkin(URI.parse(url), name)
|
||||||
def checkin(%URI{} = uri, name) do
|
def checkin(%URI{} = uri, name) do
|
||||||
timeout = Config.get([:connections_pool, :checkin_timeout], 250)
|
timeout = Config.get([:connections_pool, :checkin_timeout], 250)
|
||||||
|
|
||||||
GenServer.call(
|
GenServer.call(name, {:checkin, uri}, timeout)
|
||||||
name,
|
|
||||||
{:checkin, uri},
|
|
||||||
timeout
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec alive?(atom()) :: boolean()
|
@spec alive?(atom()) :: boolean()
|
||||||
def alive?(name) do
|
def alive?(name) do
|
||||||
pid = Process.whereis(name)
|
if pid = Process.whereis(name) do
|
||||||
if pid, do: Process.alive?(pid), else: false
|
Process.alive?(pid)
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec get_state(atom()) :: t()
|
@spec get_state(atom()) :: t()
|
||||||
|
@ -131,19 +130,20 @@ def handle_call({:checkin, uri}, from, state) do
|
||||||
%{conn: conn, gun_state: :up} = current_conn ->
|
%{conn: conn, gun_state: :up} = current_conn ->
|
||||||
Logger.debug("reusing conn #{key}")
|
Logger.debug("reusing conn #{key}")
|
||||||
|
|
||||||
with time <- :os.system_time(:second),
|
time = :os.system_time(:second)
|
||||||
last_reference <- time - current_conn.last_reference,
|
last_reference = time - current_conn.last_reference
|
||||||
current_crf <- crf(last_reference, 100, current_conn.crf),
|
current_crf = crf(last_reference, 100, current_conn.crf)
|
||||||
state <-
|
|
||||||
|
state =
|
||||||
put_in(state.conns[key], %{
|
put_in(state.conns[key], %{
|
||||||
current_conn
|
current_conn
|
||||||
| last_reference: time,
|
| last_reference: time,
|
||||||
crf: current_crf,
|
crf: current_crf,
|
||||||
conn_state: :active,
|
conn_state: :active,
|
||||||
used_by: [from | current_conn.used_by]
|
used_by: [from | current_conn.used_by]
|
||||||
}) do
|
})
|
||||||
|
|
||||||
{:reply, conn, state}
|
{:reply, conn, state}
|
||||||
end
|
|
||||||
|
|
||||||
%{gun_state: :down} ->
|
%{gun_state: :down} ->
|
||||||
{:reply, nil, state}
|
{:reply, nil, state}
|
||||||
|
|
|
@ -18,8 +18,9 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
|
||||||
def request(method, url, headers, body, opts \\ []) do
|
def request(method, url, headers, body, opts \\ []) do
|
||||||
check_adapter()
|
check_adapter()
|
||||||
|
|
||||||
with opts <- Keyword.merge(opts, body_as: :chunks, mode: :passive),
|
opts = Keyword.merge(opts, body_as: :chunks)
|
||||||
{:ok, response} <-
|
|
||||||
|
with {:ok, response} <-
|
||||||
Pleroma.HTTP.request(
|
Pleroma.HTTP.request(
|
||||||
method,
|
method,
|
||||||
url,
|
url,
|
||||||
|
@ -40,7 +41,8 @@ def request(method, url, headers, body, opts \\ []) do
|
||||||
@impl true
|
@impl true
|
||||||
@spec stream_body(map()) :: {:ok, binary(), map()} | {:error, atom() | String.t()} | :done
|
@spec stream_body(map()) :: {:ok, binary(), map()} | {:error, atom() | String.t()} | :done
|
||||||
def stream_body(%{pid: pid, opts: opts, fin: true}) do
|
def stream_body(%{pid: pid, opts: opts, fin: true}) do
|
||||||
# if connection was sended and there were redirects, we need to close new conn - pid manually
|
# 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 opts[:old_conn], do: Tesla.Adapter.Gun.close(pid)
|
||||||
# if there were redirects we need to checkout old conn
|
# if there were redirects we need to checkout old conn
|
||||||
conn = opts[:old_conn] || opts[:conn]
|
conn = opts[:old_conn] || opts[:conn]
|
||||||
|
|
Loading…
Reference in a new issue