asynchronouselixirhttpoison

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.


Solution

  • This could be achieved by spawning a new process per-request. Consider something like:

    notify = fn response ->
      # Any handling logic - write do DB? Send a message to another process?
      # Here, I'll just print the result
      IO.inspect(response)
    end
    
    spawn(fn ->
      resp = HTTPoison.get("http://google.com")
      notify.(resp)
    end) # spawn will not block, so it will attempt to execute next spawn straig away
    
    spawn(fn ->
      resp = HTTPoison.get("http://yahoo.com")
      notify.(resp)
    end) # This will be executed immediately after previoius `spawn`
    

    Please take a look at the documentation of spawn/1 I've pointed out here.

    Hope that helps!