diff --git a/README.md b/README.md index 3474d2a..4d18109 100644 --- a/README.md +++ b/README.md @@ -142,5 +142,6 @@ goes against what Farside is trying to solve. Use at your own discretion. | -- | -- | | FARSIDE_TEST | If enabled, bypasses the instance availability check and adds all instances to the pool. | | FARSIDE_PORT | The port to run Farside on (default: `4001`) | +| FARSIDE_TIMEOUT | The default timeout to wait for the url (default: `8000`) | | FARSIDE_SERVICES_JSON | The "services" JSON file to use for selecting instances (default: `services.json`) | | FARSIDE_SERVICES_JSON_DATA | The "services" JSON file to use for selecting instances base64 encoded | diff --git a/config/config.exs b/config/config.exs index 4a7043f..2cb4b05 100644 --- a/config/config.exs +++ b/config/config.exs @@ -14,4 +14,5 @@ config :farside, queries: [ "weather", "time" - ] \ No newline at end of file + ], + recv_timeout: System.get_env("FARSIDE_TIMEOUT") || "8000" \ No newline at end of file diff --git a/config/runtime.exs b/config/runtime.exs index 632a3cc..afb3b40 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -3,4 +3,4 @@ import Config config :farside, port: System.get_env("FARSIDE_PORT", nil), services_json: System.get_env("FARSIDE_SERVICES_JSON", "services.json"), - services_json_data: System.get_env("FARSIDE_SERVICES_JSON_DATA") || "" + services_json_data: System.get_env("FARSIDE_SERVICES_JSON_DATA") || "" \ No newline at end of file diff --git a/lib/farside/http.ex b/lib/farside/http.ex index 0bf4764..e4863e6 100644 --- a/lib/farside/http.ex +++ b/lib/farside/http.ex @@ -3,6 +3,7 @@ defmodule Farside.Http do @headers Application.fetch_env!(:farside, :headers) @queries Application.fetch_env!(:farside, :queries) + @recv_timeout String.to_integer(Application.fetch_env!(:farside, :recv_timeout)) def request(url) do cond do @@ -31,7 +32,7 @@ defmodule Farside.Http do :good true -> - HTTPoison.get(url, @headers) + HTTPoison.get(url, @headers, timeout: 5000, recv_timeout: @recv_timeout) |> then(&elem(&1, 1)) |> Map.get(:status_code) |> case do @@ -61,7 +62,7 @@ defmodule Farside.Http do {test_url, reply, instance} end) end) - |> Task.yield_many(5000) + |> Task.yield_many(@recv_timeout) |> Enum.map(fn {task, res} -> # Shut down the tasks that did not reply nor exit res || Task.shutdown(task, :brutal_kill)