add env var to control timeout

This commit is contained in:
mithereal 2022-07-31 15:14:48 -07:00
parent 4930ba353e
commit 9b7c544466
4 changed files with 7 additions and 4 deletions

View File

@ -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 |

View File

@ -14,4 +14,5 @@ config :farside,
queries: [
"weather",
"time"
]
],
recv_timeout: System.get_env("FARSIDE_TIMEOUT") || "8000"

View File

@ -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") || ""

View File

@ -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)