Why you don’t use :httpc
library from erlang ?
iex(1)> :inets.start
:ok
iex(2)> :httpc.request(:get, {'http://localhost/index', []}, [timeout: :timer.seconds(1)], [])
{:error, :timeout}
http://localhost/index has a sleep for 6 seconds
Also :http
has options for async requests and to notify the caller:
iex(1)> :inets.start
:ok
iex(2)> pid = self()
#PID<0.84.0>
iex(3)> :httpc.request(:get, {'http://localhost/index', []}, [], [sync: false, receiver: pid])
{:ok, #Reference<0.2595546524.2142502913.38782>}
iex(4)> flush()
:ok
iex(5)> flush() #after 6 seconds
{:http,
{#Reference<0.2595546524.2142502913.38782>,
{{'HTTP/1.1', 200, 'OK'},
[{'connection', 'Keep-Alive'}, {'date', 'Wed, 14 Jun 2017 10:48:44 GMT'},
{'server', 'Apache/2.4.25 (Ubuntu)'}, {'content-length', '9'},
{'content-type', 'text/html; charset=UTF-8'},
{'keep-alive', 'timeout=5, max=100'}], "index.php"}}}
:ok
make an async request and store reference in state, http will notify the gen_server by calling handle_call({:http, {:ref, result}, state)
when request is finished, after some time you can call gen_server to give the response … so the problem with http timeouts is resolved.
If you want to know how to manage exists from Task.Supervisor.async_nolink
for example you can take a look over this topic
Hope will help