sergio_101

sergio_101

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!

Showing Posts 1 to 9

cmo

cmo

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.

dimitarvp

dimitarvp

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.

sergio_101

sergio_101 OP

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!

JohnnyCurran

JohnnyCurran

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

dimitarvp

dimitarvp

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.

derek-zhou

derek-zhou

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.

dimitarvp

dimitarvp

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

derek-zhou

derek-zhou

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

sergio_101 OP

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!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews