I have a situation where I am doing the following:
When a function is called, at the start and finish of the call, a call is fired off to an API.
In several cases, the API times out, and throws an exception. When this happens, the entire function fails, and causes other problems.
In reality, I don’t care if the API call happens (it’s just a call to a logger).
I figure I can do the following:
Catch an exception (easy. but boring)
Extend the timeout (easy. boring also. Not really reliable)
Create a genserver that consumes the data for the call and makes the call (this seems more elixir-ish)
My question is, is this a good use case for a genserver? I would send it the data, and it would make the call to the API, and if it fails, it fails, but my function would continue like nothing happened. or didn’t.
If you just want to fire something off and don’t care what happens, you don’t need GenServer. Task or spawn is more concise. GenServer is there when you want to manage state, lifecycle etc.
Not necessarily, GenServer would also be very useful in this situation because @sergio_101 can (1) just do GenServer.cast to it and not wait for a reply and (2) centralize access to the external logging API and not have a number of unsupervised processes sitting around.
So IMO a GenServer would handle OP’s case very well.
I have a further question. This whole thing is running in a Phoenix project. How would I go about firing off the genserver so that it is running when I need it?
Should I check to see if it’s running first, then fire it up if it’s not?
I also use cast only GenServer to do miscellaneous stuff. There is one point I want to add, you need to prevent the case of severe backlog in that GenServer, or you will has some head scratching when that occasionally happened.
How did you handle it? I haven’t come across such a case yet (at least not in my Elixir work, happened several times in my Rust and Golang work though, where we just used bounded channels with a high enough value to guarantee smooth operation 99.9% of the time and provide some backpressure in the the 0.1% cases).