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:

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

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement