Fl4m3Ph03n1x

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:

  1. They use flush to show the request was completed. I can’t loggin into the app and manually flush to see how the requests are going, that would be insane.
  2. They don’t show any notifications mechanism for when we get the responses or errors.

So, I have a simple question:

  1. 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

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

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 :smiley:

Thank you everyone!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement