Is this a good use case for genserver?

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:

  1. Catch an exception (easy. but boring)
  2. Extend the timeout (easy. boring also. Not really reliable)
  3. 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.

Thanks!

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.

1 Like

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.

1 Like

Thanks for the replies, everyone!

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?

Thanks!

Put it in the children of your Application module, lib/your_app/application.ex . That Supervisor will start it up on app start.

Well, the GenServer docs cover that very well. You code it up and then include it in your Application’s children key in the init function.

That way the GenServer is gonna get started with your app and persist until the app’s shutdown, ready to accept messages.

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

I have use 2 techniques, each has it’s own drawbacks:

  • Has the GenServer occasionally look at its own queue length. If the situation is dire, just drop some work on the floor.
  • ping pong a token between the producer of the works and the consumer of the work If the token hasn’t come back, throttle back from the producer side.
1 Like

I am gonna set up a genserver. I haven’t set it up yet, as I am not hitting this issue until later today.

Thanks for all your help!