Skip to content

Commit

Permalink
remove retry of failed request; Req handles this
Browse files Browse the repository at this point in the history
  • Loading branch information
balexand committed May 10, 2024
1 parent 00486fd commit b8f3c06
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 52 deletions.
63 changes: 14 additions & 49 deletions lib/sanity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ defmodule Sanity do
default: [receive_timeout: 30_000],
doc: "Options to be passed to `Req.request/2`."
],
max_attempts: [
type: :pos_integer,
default: 1,
doc:
"Number of attempts to make before returning error. Requests receiving an HTTP status code of 4xx will not be retried."
],
project_id: [
type: :string,
doc: "Sanity project ID.",
Expand All @@ -57,12 +51,6 @@ defmodule Sanity do
default: Req,
doc: false
],
retry_delay: [
type: :pos_integer,
default: 1_000,
doc:
"Delay in ms to wait before retrying after an error. Applies if `max_attempts` is greater than `1`."
],
token: [
type: :string,
doc: "Sanity auth token."
Expand Down Expand Up @@ -251,42 +239,23 @@ defmodule Sanity do
) do
opts = NimbleOptions.validate!(opts, @request_options_schema)

result =
Keyword.merge(Keyword.fetch!(opts, :http_options),
body: body,
headers: headers(opts) ++ headers,
method: method,
url: "#{url_for(request, opts)}?#{URI.encode_query(query_params)}"
)
|> Keyword.fetch!(opts, :req_mod).request()

case {opts[:max_attempts], result} do
{_, {:ok, %Req.Response{body: body, headers: headers, status: status}}}
Keyword.merge(Keyword.fetch!(opts, :http_options),
body: body,
headers: headers(opts) ++ headers,
method: method,
url: "#{url_for(request, opts)}?#{URI.encode_query(query_params)}"
)
|> Keyword.fetch!(opts, :req_mod).request()
|> case do
{:ok, %Req.Response{body: body, headers: headers, status: status}}
when status in 200..299 ->
{:ok, %Response{body: body, headers: headers, status: status}}

{_, {:ok, %Req.Response{body: body, headers: headers, status: status} = resp}}
{:ok, %Req.Response{body: %{} = body, headers: headers, status: status}}
when status in 400..499 ->
case body do
%{} -> {:error, %Response{body: body, headers: headers, status: status}}
_ -> raise %Sanity.Error{source: resp}
end

{max_attempts, {_, error_or_response}} when max_attempts > 1 ->
Logger.warning(
"retrying failed request in #{opts[:retry_delay]}ms: #{inspect(error_or_response)}"
)

:timer.sleep(opts[:retry_delay])
{:error, %Response{body: body, headers: headers, status: status}}

opts =
opts
|> Keyword.update!(:max_attempts, &(&1 - 1))
|> Keyword.update!(:retry_delay, &(&1 * 2))

request(request, opts)

{_, {_, error_or_response}} ->
{_, error_or_response} ->
raise %Sanity.Error{source: error_or_response}
end
end
Expand Down Expand Up @@ -335,8 +304,7 @@ defmodule Sanity do
request_opts: [
type: :keyword_list,
required: true,
doc:
"Options to be passed to `request/2`. If `max_attempts` is omitted then it will default to `3`."
doc: "Options to be passed to `request/2`."
],
variables: [
type: {:map, {:or, [:atom, :string]}, :any},
Expand All @@ -363,10 +331,7 @@ defmodule Sanity do
@impl true
@spec stream(Keyword.t()) :: Enumerable.t()
def stream(opts) do
opts =
opts
|> NimbleOptions.validate!(@stream_options_schema)
|> Keyword.update!(:request_opts, &Keyword.put_new(&1, :max_attempts, 3))
opts = NimbleOptions.validate!(opts, @stream_options_schema)

case Map.take(opts[:variables], [:pagination_last_id, "pagination_last_id"]) |> Map.keys() do
[] -> nil
Expand Down
4 changes: 1 addition & 3 deletions test/sanity_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,13 @@ defmodule SanityTest do
end

test "query, projection, and variables options" do
Mox.expect(MockSanity, :request!, fn %Request{query_params: query_params}, request_opts ->
Mox.expect(MockSanity, :request!, fn %Request{query_params: query_params}, _request_opts ->
assert query_params == %{
"$type" => "\"page\"",
"query" =>
"*[(_type == $type) && (!(_id in path('drafts.**')))] | order(_id) [0..999] { _id, title }"
}

assert request_opts[:max_attempts] == 3

%Response{body: %{"result" => [%{"_id" => "a", "title" => "home"}]}}
end)

Expand Down

0 comments on commit b8f3c06

Please sign in to comment.