Skip to content

Commit

Permalink
Set timeout in Hackney adapter as well
Browse files Browse the repository at this point in the history
  • Loading branch information
qgadrian committed Sep 24, 2020
1 parent 79f7fd4 commit f182905
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
1 change: 0 additions & 1 deletion apps/smokex_client/lib/parsers/yaml/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ defmodule SmokexClient.Parsers.Yaml.Parser do
|> Map.take(@step_opts)
|> Map.to_list()
|> Map.new(fn {key, value} ->
# TODO users can add ANYTHING and this will throw an unhandled error using `to_existing_atom`
{String.to_existing_atom(key), TypeConverter.convert(value)}
end)
|> Keyword.new()
Expand Down
19 changes: 12 additions & 7 deletions apps/smokex_client/lib/step/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule SmokexClient.Step.HttpClient do
def new(%Request{} = step) do
step
|> build_middleware()
|> Tesla.client(build_adapter())
|> Tesla.client(build_adapter(step))
end

@doc """
Expand Down Expand Up @@ -48,8 +48,15 @@ defmodule SmokexClient.Step.HttpClient do
defp get_body(_, :get), do: nil
defp get_body(body, _action), do: Jason.encode!(body)

@spec build_adapter() :: term
defp build_adapter, do: {Tesla.Adapter.Hackney, insecure: true}
@spec step_timeout(Request.t()) :: non_neg_integer()
defp step_timeout(%Request{} = step) do
step.opts[:timeout] || Application.get_env(:smokex_client, :timeout, 5_000)
end

@spec build_adapter(Request.t()) :: term
defp build_adapter(%Request{} = step) do
{Tesla.Adapter.Hackney, insecure: true, recv_timeout: step_timeout(step)}
end

@spec build_middleware(Request.t()) :: list
defp build_middleware(%Request{} = step) do
Expand All @@ -60,14 +67,12 @@ defmodule SmokexClient.Step.HttpClient do
Tesla.Middleware.JSON
end

timeout = step.opts[:timeout] || Application.get_env(:smokex_client, :timeout)

[
maybe_json_middleware,
{Tesla.Middleware.Logger, debug: true, log_level: :info},
Tesla.Middleware.Logger,
{Tesla.Middleware.Query, Map.to_list(step.query)},
{Tesla.Middleware.Headers, Map.to_list(step.headers)},
{Tesla.Middleware.Timeout, timeout: timeout},
{Tesla.Middleware.Timeout, timeout: step_timeout(step)},
Tesla.Middleware.Telemetry
]
|> Enum.reject(&is_nil/1)
Expand Down

0 comments on commit f182905

Please sign in to comment.