Fl4m3Ph03n1x
How to make async requests using HTTPoison?
Background
We have an app that deals with a considerable amount of requests per second. This app needs to notify an external service, by making a GET call via HTTPS to one of our servers.
Objective
The objective here is to use HTTPoison to make async GET requests. I don’t really care about the response of the requests, all I care is to know if they failed or not, so I can write any possible errors into a logger.
If it succeeds I don’t want to do anything.
Research
I have checked the official documentation for HTTPoison and I see that they support async requests:
https://hexdocs.pm/httpoison/readme.html#usage
However, I have 2 issues with this approach:
- They use
flushto show the request was completed. I can’t loggin into the app and manuallyflushto see how the requests are going, that would be insane. - They don’t show any notifications mechanism for when we get the responses or errors.
So, I have a simple question:
- How do I get asynchronously notified that my request failed or succeeded?
I assume that the default HTTPoison.get is synchronous, as shown in the documentation.
Marked As Solved
outlog
Task.start(fn ->
case HTTPoison.get("httpbin.org/get", [], hackney: [pool: :first_pool]) do
...
_ -> Logger.whatever(something, request_id, user_id, etc..)
end
end)
eg you can easily log any errors. (obviously use start/3 for “real” DRY code…)
You can use the pools…
But not sure I understand all the requirements…
Also Liked
Fl4m3Ph03n1x
Thank you all for the answers and explanations. There seems to be a misunderstanding that I don’t agree with the path Elixir has taken to deal with Async requests, some even generalizing that I am not a fan of the Actor model inherent to Erlang.
Such is not true. The post that illustrates almost in perfection my reaction is in fact here:
I am not a seasoned Elixir / Erlang developer. I wish I was, but truth is I came into this strange world from a Functional Javascript background. In JS, as some of you may know, you either use Promises or (the better version) Futures (and no, I am not going to mention callbacks… leave me alone!). All libraries offer you a way to handle async behavior by default.
I thought Elixir was similar. I am happy to see it isn’t. Now that I have a better understanding on how to handle async behavior in Elixir I see how much more powerful (and dangerous :D) it is.
My quest for learning hasn’t stopped yet, but I think I got what I needed from here ![]()
Thank you everyone!
benwilson512
Javascript libraries offer an “async” mode because if they don’t, then using them blocks everything. This isn’t a problem in Elixir. When a single process does a “receive” it simply blocks that process. Every other process (like other HTTP requests) are still able to happen. It’s conceptually like doing .then in javascript, but actually even better.
This is awesome because not only does it apply to IO, it even applies to CPU bound operations. If your process starts an infinite loop or starts doing a lot of math, it won’t block other processes. In javascript it will.
datadrover
You should watch Joe Armstrong’s Kent lectures on concurrency. In particular, look out for the bit on Promises in video 2. Beautifully simple and elegant.








