diff options
author | kballou <kballou@devnulllabs.io> | 2016-07-28 16:44:32 -0600 |
---|---|---|
committer | kballou <kballou@devnulllabs.io> | 2016-07-29 11:36:04 -0600 |
commit | 1248eeb88d90f0ece972299d40b7406ef0a17cf5 (patch) | |
tree | 813c9c8c899a5752409641e47c4480f5e0308721 /lib/exping/http.ex | |
parent | 2175a2f4d9a416f4a443166a2487c8c12a3df9e2 (diff) | |
download | exping-1248eeb88d90f0ece972299d40b7406ef0a17cf5.tar.gz exping-1248eeb88d90f0ece972299d40b7406ef0a17cf5.tar.xz |
Add HTTP "Ping"
Diffstat (limited to 'lib/exping/http.ex')
-rw-r--r-- | lib/exping/http.ex | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/exping/http.ex b/lib/exping/http.ex new file mode 100644 index 0000000..576562f --- /dev/null +++ b/lib/exping/http.ex @@ -0,0 +1,45 @@ +defmodule ExPing.HTTP do + require Logger + @moduledoc """ + Provides basic HTTP client for pinging endpoints + """ + + @timeout Application.get_env(:exping, :http)[:timeout] || 5000 + + @spec head(URI.t) :: {:ok, {integer, binary}} | {:error, term} + def head(endpoint) do + ref = make_ref() + :ok = Logger.info("Sending HEAD request to #{inspect(endpoint)}") + {:ok, _} = spawn_http_task(:head, [endpoint, ref, self()]) + + receive do + {:http_task_resp, ^ref, {:ok, {_, _}} = resp} -> resp + {:http_task_resp, ^ref, {:error, _} = error} -> error + after @timeout -> + {:error, :timeout} + end + end + + @spec get(URI.t) :: {:ok, {integer, binary}} | {:error, term} + def get(endpoint) do + ref = make_ref() + :ok = Logger.info("Sending GET request to #{inspect(endpoint)}") + {:ok, _} = spawn_http_task(:get, [endpoint, ref, self()]) + + receive do + {:http_task_resp, ^ref, {:ok, _} = resp} -> resp + {:http_task_resp, ^ref, {:error, _} = error} -> error + after @timeout -> + {:error, :timeout} + end + end + + defp spawn_http_task(method, args) do + Task.Supervisor.start_child( + ExPing.Supervisor.Task, + ExPing.HTTP.Task, + method, + args) + end + +end |