Skip to content

Commit

Permalink
Add option to retry requests
Browse files Browse the repository at this point in the history
Default behaviour of 1 request attempt is kept.
  • Loading branch information
aboroska committed Jun 29, 2023
1 parent 27971d0 commit 764c987
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ config :panoramix,

* `request_timeout`: Query timeout in millis to be used in [`Context`](context-druid-doc-link) of all Druid queries.
* `query_priority`: Priority to be used in [`Context`](context-druid-doc-link) of all Druid queries.
* `max_request_attempts`: Maximum number of request attempts before returning an error result. Optional, default is 1 attempt.
* `request_attempts_delay_ms`: Delay in milliseconds before attempting a new request in case of error response. Optional, default is no delay.

[context-druid-doc-link]: http://druid.io/docs/latest/querying/query-context.html

Expand Down
10 changes: 9 additions & 1 deletion lib/panoramix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ defmodule Panoramix do
end
end

defp request_and_decode(profile, method, url_path, body, headers) do
defp request_and_decode(profile, method, url_path, body, headers, attempt \\ 1) do
broker_profiles = Application.get_env(:panoramix, :broker_profiles)
max_attempts = Application.get_env(:panoramix, :max_request_attempts, 1)

broker_profile =
broker_profiles[profile] ||
Expand All @@ -187,6 +188,13 @@ defmodule Panoramix do
with {:ok, http_response} <- HTTPoison.request(method, url, body, headers, options),
{:ok, body} <- maybe_handle_druid_error(http_response) do
Jason.decode(body)
else
{:error, _reason} when attempt < max_attempts ->
delay = Application.get_env(:panoramix, :request_attempts_delay_ms, 0)
Process.sleep(delay)
request_and_decode(profile, method, url_path, body, headers, attempt + 1)
error ->
error
end
end

Expand Down

0 comments on commit 764c987

Please sign in to comment.