Fl4m3Ph03n1x

Fl4m3Ph03n1x

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.

Showing Posts 1 to 10

outlog

outlog

I would use Task.start - and then log the error from there should it fail..

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

Interesting idea, but I am a little bit interested in the result, i.e., as I explained I would like to know if the request failed so I can log it.

Furthermore using Task means I wouldn’t be able to take advantage of the hackney pools HTTPoison uses, so I would have to create a Task (process) for each request instead of taking advantage of the pools already in place, which would result in far less efficient alternative.

sanswork

sanswork

Spawn a new process with a receive block and point your stream_to at that instead of self.

NobbZ

NobbZ

use receive to receive the messages… remember to drop those you don’t want to see…

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

@sanswork
@NobbZ
So there is no way to do this asynchronously using HTTPoison?
What is the async documentation for then? How would one even use it?

I swear I am getting more confused as time goes by.

sanswork

sanswork

What we just described is asyc using httpoison.

NobbZ

NobbZ

Of course it is.

  1. send the request
  2. do whatever you want
  3. receive the payload

Thats async for me.

If you do not want to do this in the process that deals with the other stuff, you have to offload it into another process.

lpil

lpil

Creator of Gleam

Unlike in most languages in Elixir or Erlang it’s trivial for a function to be made asynchronous using processes (possibly using an abstraction such as Task). Because of this we don’t need libraries to implement async versions of the functions they offer- and often we would prefer them not to as one size does not fit all here.

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

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

The reason I mention why this is confusing is because receive is actually blocking. According to the documentation:

If there is no message in the mailbox matching any of the patterns, the current process will wait until a matching message arrives

I don’t want my process to wait for anything. I want it to go around doing it’s business and if it receives something, to act accordingly. I don’t want to block a process waiting for a response. In reality this would be a perfect case of fire and forget but since I need to know if something failed it is not 100% fire and forget.

So I hope you understand my confusion when you people tell me to use receive, which according to my understanding, actually blocks the process until it receives something.

I guess under this assumption Task.start would make more sense, since I could block the Task instead.

Not sure I agree with the philosophy, but the insight is surely appreciated! It helps me put into context the responses I have been getting thus far!

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews