more clean up

This commit is contained in:
Alexander Strizhakov 2020-03-13 09:37:57 +03:00
parent 39ed608b13
commit 98ed0d1c4b
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
7 changed files with 21 additions and 22 deletions

View file

@ -22,7 +22,7 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
@spec options(keyword(), URI.t()) :: keyword() @spec options(keyword(), URI.t()) :: keyword()
def options(incoming_opts \\ [], %URI{} = uri) do def options(incoming_opts \\ [], %URI{} = uri) do
proxy = proxy =
Pleroma.Config.get([:http, :proxy_url], nil) Pleroma.Config.get([:http, :proxy_url])
|> AdapterHelper.format_proxy() |> AdapterHelper.format_proxy()
config_opts = Pleroma.Config.get([:http, :adapter], []) config_opts = Pleroma.Config.get([:http, :adapter], [])

View file

@ -11,7 +11,7 @@ defmodule Pleroma.HTTP.AdapterHelper.Hackney do
@spec options(keyword(), URI.t()) :: keyword() @spec options(keyword(), URI.t()) :: keyword()
def options(connection_opts \\ [], %URI{} = uri) do def options(connection_opts \\ [], %URI{} = uri) do
proxy = Pleroma.Config.get([:http, :proxy_url], nil) proxy = Pleroma.Config.get([:http, :proxy_url])
config_opts = Pleroma.Config.get([:http, :adapter], []) config_opts = Pleroma.Config.get([:http, :adapter], [])

View file

@ -30,12 +30,12 @@ def options(%URI{} = uri, opts \\ []) do
@defaults @defaults
|> pool_timeout() |> pool_timeout()
|> Keyword.merge(opts) |> Keyword.merge(opts)
|> adapter().options(uri) |> adapter_helper().options(uri)
end end
defp pool_timeout(opts) do defp pool_timeout(opts) do
{config_key, default} = {config_key, default} =
if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do if adapter() == Tesla.Adapter.Gun do
{:pools, Config.get([:pools, :default, :timeout])} {:pools, Config.get([:pools, :default, :timeout])}
else else
{:hackney_pools, 10_000} {:hackney_pools, 10_000}
@ -47,10 +47,12 @@ defp pool_timeout(opts) do
end end
@spec after_request(keyword()) :: :ok @spec after_request(keyword()) :: :ok
def after_request(opts), do: adapter().after_request(opts) def after_request(opts), do: adapter_helper().after_request(opts)
defp adapter do defp adapter, do: Application.get_env(:tesla, :adapter)
case Application.get_env(:tesla, :adapter) do
defp adapter_helper do
case adapter() do
Tesla.Adapter.Gun -> AdapterHelper.Gun Tesla.Adapter.Gun -> AdapterHelper.Gun
Tesla.Adapter.Hackney -> AdapterHelper.Hackney Tesla.Adapter.Hackney -> AdapterHelper.Hackney
_ -> AdapterHelper _ -> AdapterHelper

View file

@ -39,7 +39,6 @@ def handle_info({:gun_up, _conn, _protocol}, state) do
@impl true @impl true
def handle_info({:gun_down, _conn, _protocol, _reason, _killed}, state) do def handle_info({:gun_down, _conn, _protocol, _reason, _killed}, state) do
# don't flush messages here, because gun can reconnect
{:noreply, state} {:noreply, state}
end end

View file

@ -13,16 +13,13 @@ def start_link(args) do
end end
def init(_) do def init(_) do
children = conns_child = %{
[
%{
id: Pool.Connections, id: Pool.Connections,
start: start:
{Pool.Connections, :start_link, [{:gun_connections, Config.get([:connections_pool])}]} {Pool.Connections, :start_link, [{:gun_connections, Config.get([:connections_pool])}]}
} }
] ++ pools()
Supervisor.init(children, strategy: :one_for_one) Supervisor.init([conns_child | pools()], strategy: :one_for_one)
end end
defp pools do defp pools do

View file

@ -3,11 +3,11 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ReverseProxy.Client.Tesla do defmodule Pleroma.ReverseProxy.Client.Tesla do
@behaviour Pleroma.ReverseProxy.Client
@type headers() :: [{String.t(), String.t()}] @type headers() :: [{String.t(), String.t()}]
@type status() :: pos_integer() @type status() :: pos_integer()
@behaviour Pleroma.ReverseProxy.Client
@spec request(atom(), String.t(), headers(), String.t(), keyword()) :: @spec request(atom(), String.t(), headers(), String.t(), keyword()) ::
{:ok, status(), headers} {:ok, status(), headers}
| {:ok, status(), headers, map()} | {:ok, status(), headers, map()}
@ -18,7 +18,7 @@ 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()
opts = Keyword.merge(opts, body_as: :chunks) opts = Keyword.put(opts, :body_as, :chunks)
with {:ok, response} <- with {:ok, response} <-
Pleroma.HTTP.request( Pleroma.HTTP.request(
@ -39,7 +39,8 @@ def request(method, url, headers, body, opts \\ []) do
end end
@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 | no_return()
def stream_body(%{pid: pid, opts: opts, fin: true}) do def stream_body(%{pid: pid, opts: opts, fin: true}) do
# if connection was reused, but in tesla were redirects, # if connection was reused, but in tesla were redirects,
# tesla returns new opened connection, which must be closed manually # tesla returns new opened connection, which must be closed manually

View file

@ -59,7 +59,7 @@ defmodule Pleroma.ReverseProxy do
* `req_headers`, `resp_headers` additional headers. * `req_headers`, `resp_headers` additional headers.
* `http`: options for [gun](https://github.com/ninenines/gun). * `http`: options for [hackney](https://github.com/benoitc/hackney) or [gun](https://github.com/ninenines/gun).
""" """
@default_options [pool: :media] @default_options [pool: :media]