Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiny improvements #12

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/hardhat/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ defmodule Hardhat.Builder do
defmacro __using__(opts \\ []) do
strategy =
case Keyword.get(opts, :strategy, :fuse) do
v when v in [:fuse, :regulator, :none] -> v
invalid -> raise "Invalid strategy #{inspect(invalid)}"
v when v in [:fuse, :regulator, :none] ->
v

invalid ->
raise CompileError,
description: "Invalid strategy #{inspect(invalid)}",
file: __CALLER__.file,
line: __CALLER__.line
end

opts = opts |> Keyword.delete(:strategy) |> Keyword.put_new(:docs, false)
Expand Down
6 changes: 3 additions & 3 deletions lib/hardhat/defaults.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Hardhat.Defaults do
end

def should_melt({:ok, %Tesla.Env{} = env}) do
env.status >= 500 || env.status == 429
env.status >= 500 or env.status == 429
end

@doc """
Expand Down Expand Up @@ -112,7 +112,7 @@ defmodule Hardhat.Defaults do
def should_retry({:error, _}), do: true

def should_retry({:ok, %Tesla.Env{} = env}) do
env.method !== :post && (env.status == 429 || env.status >= 500)
env.method != :post and (env.status == 429 or env.status >= 500)
end

@doc """
Expand Down Expand Up @@ -154,7 +154,7 @@ defmodule Hardhat.Defaults do
end

def should_regulate({:ok, %Tesla.Env{} = env}) do
env.status >= 500 || env.status == 429
env.status >= 500 or env.status == 429
end

@doc """
Expand Down
8 changes: 4 additions & 4 deletions lib/hardhat/middleware/path_params.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ defmodule Hardhat.Middleware.PathParams do
@impl Tesla.Middleware
def call(env, next, _) do
env =
if env.opts[:path_params] do
if path_params = env.opts[:path_params] do
urlsafe =
env.opts[:path_params]
|> Enum.map(fn {key, value} -> {key, URI.encode(value, &URI.char_unreserved?/1)} end)
|> Enum.to_list()
Enum.map(path_params, fn {key, value} ->
{key, URI.encode(value, &URI.char_unreserved?/1)}
end)

Tesla.put_opt(env, :path_params, urlsafe)
else
Expand Down
36 changes: 21 additions & 15 deletions lib/hardhat/middleware/timeout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ defmodule Hardhat.Middleware.Timeout do
end

task =
safe_async(fn ->
if is_deadline, do: Deadline.set(timeout)
Tesla.run(env, next)
end)
if is_deadline do
safe_async(fn ->
Deadline.set(timeout)
Tesla.run(env, next)
end)
else
safe_async(fn -> Tesla.run(env, next) end)
end

try do
task
|> Task.await(timeout)
|> repass_error
Task.await(task, timeout)
catch
:exit, {:timeout, _} ->
Task.shutdown(task, 0)
Expand All @@ -62,6 +64,18 @@ defmodule Hardhat.Middleware.Timeout do
)

{:error, :timeout}
else
{:exception, error, stacktrace} ->
reraise(error, stacktrace)

{:throw, value} ->
throw(value)

{:exit, value} ->
exit(value)

{:ok, result} ->
result
end
end

Expand All @@ -78,12 +92,4 @@ defmodule Hardhat.Middleware.Timeout do
end
end)
end

defp repass_error({:exception, error, stacktrace}), do: reraise(error, stacktrace)

defp repass_error({:throw, value}), do: throw(value)

defp repass_error({:exit, value}), do: exit(value)

defp repass_error({:ok, result}), do: result
end
Loading