Async POST to external API?

Hi. I need to make a POST to external API every 10s or so. I don’t necessarily we’d to wait for the response. But in case the response is not 200, I might want to do something, show error or raise exception. The request may take a few seconds sometimes, so a few requests may end up running concurrently.

What is the proper simple way, please?

I ended up with some this like Task.Supervisor.start_child but is that the proper way?

Thank you very much.

hi, yes, why not. You can create GenServer which will run Process.send_after/4 after he execute Task.start/1. You can have own TaskSupervisor who can manage running processes. But I think you can have some issues in future, but it depends on external API. I like idea of Mint. There is a pool of connections to every domain. So you can have one pool for your external API. You can use Finch or Tesla middleware for Finch.

You should think about and handle errors because you should take care of your task process so this new process will never crash. It can crash but it can be messy in logs when you have about 9000 requests per day. And in case external api is down for 10 minutes you don’t want to see 1_000 lines of errors. It depends what to you want to do when your process crash or you can handle it with just Logger.error…

I like Tesla because you can add Retry middleware where you can setup retry when you get network error. These errors happen because of ISP, clouds, etc… But it depends what do you want to achieve with this calls.

4 Likes

thank you!

1 Like