Skip to content

Commit

Permalink
Merge pull request #4386 from nulib/5333-timeouts
Browse files Browse the repository at this point in the history
Add retries to HTTP timeouts in Meadow.Search.HTTP and Meadow.Utils.AWS
  • Loading branch information
mbklein authored Feb 4, 2025
2 parents 868f5cc + ae97991 commit eebdcc3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
16 changes: 11 additions & 5 deletions app/lib/meadow/search/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ defmodule Meadow.Search.HTTP do
atoms: [:retry],
rescue_only: [] do
case ElastixHTTP.request(method, url, body, headers, options) do
{:ok, %{status_code: status} = response} when status in 200..399 ->
{:ok, response}
{:ok, %{status_code: status} = response} when status in [429, 503, 504] ->
{:retry, response}

{:ok, %{status_code: 404} = response} ->
{:ok, response} ->
{:ok, response}

{:ok, %{status_code: 429} = response} ->
{:retry, response}
{:error, %HTTPoison.Error{reason: :timeout} = error} ->
{:retry, error}

{:error, error} ->
{:error, error}
Expand All @@ -80,6 +80,12 @@ defmodule Meadow.Search.HTTP do
result ->
result |> maybe_report(%{method: method, url: url, body: body})
else
{:retry, %HTTPoison.Response{} = response} ->
response |> maybe_report(%{method: method, url: url, body: body})

{:retry, reason} ->
{:error, reason} |> maybe_report(%{method: method, url: url, body: body})

error ->
error |> maybe_report(%{method: method, url: url, body: body})
end
Expand Down
28 changes: 26 additions & 2 deletions app/lib/meadow/utils/aws.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule Meadow.Utils.AWS do
alias Meadow.Utils.AWS.MultipartCopy
alias Meadow.Utils.Pairtree

use Retry

import SweetXml, only: [sigil_x: 2]

require Logger
Expand All @@ -18,14 +20,36 @@ defmodule Meadow.Utils.AWS do
Drop-in replacement for ExAws.request/2 that reports errors to Honeybadger
"""
def request(op, config_overrides \\ []) do
ExAws.request(op, config_overrides) |> handle_response()
retry with: exponential_backoff() |> randomize() |> cap(1_000) |> Stream.take(10),
atoms: [:retry],
rescue_only: [] do
case ExAws.request(op, config_overrides) |> handle_response() do
:timeout ->
{:retry, :timeout}

{:error, {:http_error, status, _}} = response when status in [429, 503, 504] ->
{:retry, response}

{:ok, result} ->
{:ok, result}

error ->
error
end
after
{:ok, result} -> {:ok, result}
{:error, error} -> {:error, error}
else
{:retry, error} -> error
error -> error
end
end

@doc """
Drop-in replacement for ExAws.request!/2 that reports errors to Honeybadger
"""
def request!(op, config_overrides \\ []) do
case ExAws.request(op, config_overrides) |> handle_response() do
case request(op, config_overrides) do
{:ok, result} ->
result

Expand Down

0 comments on commit eebdcc3

Please sign in to comment.