timeout option moved to gun adapter helper

This commit is contained in:
Alexander Strizhakov 2020-09-02 10:50:51 +03:00
parent 1c57ef4498
commit 84fbf16161
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
3 changed files with 26 additions and 26 deletions

View file

@ -10,9 +10,7 @@ defmodule Pleroma.HTTP.AdapterHelper do
@type proxy_type() :: :socks4 | :socks5 @type proxy_type() :: :socks4 | :socks5
@type host() :: charlist() | :inet.ip_address() @type host() :: charlist() | :inet.ip_address()
@type pool() :: :federation | :upload | :media | :default
alias Pleroma.Config
alias Pleroma.HTTP.AdapterHelper alias Pleroma.HTTP.AdapterHelper
require Logger require Logger
@ -46,29 +44,12 @@ defmodule Pleroma.HTTP.AdapterHelper do
def options(%URI{} = uri, opts \\ []) do def options(%URI{} = uri, opts \\ []) do
@defaults @defaults
|> Keyword.merge(opts) |> Keyword.merge(opts)
|> put_timeout()
|> adapter_helper().options(uri) |> adapter_helper().options(uri)
end end
@spec pool_timeout(pool()) :: non_neg_integer() @spec get_conn(URI.t(), keyword()) :: {:ok, keyword()} | {:error, atom()}
def pool_timeout(pool) do
{config_key, default} =
if adapter() == Tesla.Adapter.Gun do
{:pools, Config.get([:pools, :default, :timeout], 5_000)}
else
{:hackney_pools, 10_000}
end
Config.get([config_key, pool, :timeout], default)
end
# For Hackney, this is the time a connection can stay idle in the pool.
# For Gun, this is the timeout to receive a message from Gun.
defp put_timeout(opts) do
Keyword.put_new(opts, :timeout, pool_timeout(opts[:pool]))
end
def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts) def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
defp adapter, do: Application.get_env(:tesla, :adapter) defp adapter, do: Application.get_env(:tesla, :adapter)
defp adapter_helper do defp adapter_helper do

View file

@ -20,6 +20,8 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
await_up_timeout: 5_000 await_up_timeout: 5_000
] ]
@type pool() :: :federation | :upload | :media | :default
@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 =
@ -34,6 +36,7 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
|> add_scheme_opts(uri) |> add_scheme_opts(uri)
|> AdapterHelper.maybe_add_proxy(proxy) |> AdapterHelper.maybe_add_proxy(proxy)
|> Keyword.merge(incoming_opts) |> Keyword.merge(incoming_opts)
|> put_timeout()
end end
defp add_scheme_opts(opts, %{scheme: "http"}), do: opts defp add_scheme_opts(opts, %{scheme: "http"}), do: opts
@ -42,6 +45,18 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
Keyword.put(opts, :certificates_verification, true) Keyword.put(opts, :certificates_verification, true)
end end
defp put_timeout(opts) do
# this is the timeout to receive a message from Gun
Keyword.put_new(opts, :timeout, pool_timeout(opts[:pool]))
end
@spec pool_timeout(pool()) :: non_neg_integer()
def pool_timeout(pool) do
default = Config.get([:pools, :default, :timeout], 5_000)
Config.get([:pools, pool, :timeout], default)
end
@spec get_conn(URI.t(), keyword()) :: {:ok, keyword()} | {:error, atom()} @spec get_conn(URI.t(), keyword()) :: {:ok, keyword()} | {:error, atom()}
def get_conn(uri, opts) do def get_conn(uri, opts) do
case ConnectionPool.get_conn(uri, opts) do case ConnectionPool.get_conn(uri, opts) do

View file

@ -54,11 +54,15 @@ defmodule Pleroma.Uploaders.S3 do
{:content_type, upload.content_type} {:content_type, upload.content_type}
]) ])
if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
# set s3 upload timeout to respect :upload pool timeout # set s3 upload timeout to respect :upload pool timeout
# timeout should be slightly larger, so s3 can retry upload on fail # timeout should be slightly larger, so s3 can retry upload on fail
timeout = Pleroma.HTTP.AdapterHelper.pool_timeout(:upload) + 1_000 timeout = Pleroma.HTTP.AdapterHelper.Gun.pool_timeout(:upload) + 1_000
opts = Keyword.put(op.opts, :timeout, timeout) opts = Keyword.put(op.opts, :timeout, timeout)
Map.put(op, :opts, opts) Map.put(op, :opts, opts)
else
op
end
else else
{:ok, file_data} = File.read(upload.tempfile) {:ok, file_data} = File.read(upload.tempfile)