diff --git a/README.md b/README.md index 79a2332..7b01784 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/panoramix.ex b/lib/panoramix.ex index 9f3c824..452bd88 100644 --- a/lib/panoramix.ex +++ b/lib/panoramix.ex @@ -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] || @@ -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